In this c# basic programs we give the information about basic C# programs like addition of two numbers, to find odd/even number program etc. with their output.
c# basic programs:
// 1. Write a program to display We-Come massage using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
Console.WriteLine(“Wel-Come to C# Programming”);
Console.ReadLine();
}
}
}
O/P:-
Wel-Come to C# Programming
// 2. Write a program to display addition of 2 numbers using C#. (Compile time values)
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int a = 10, b = 5, sum = 0;
Console.WriteLine(“C# Programming”);
sum = a + b;
Console.WriteLine(“A + B = “+sum);
Console.ReadLine();
}
}
}
O/P:-
C# Programming
A + B = 15
// 3. Write a program to display addition of 2 numbers using C#. (Run time values)
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int a , b, sum = 0;
Console.WriteLine(“C# Programming”);
Console.Write(“Enter the First number: “);
a = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the Second number: “);
b = Convert.ToInt32(Console.ReadLine());
sum = a + b;
Console.WriteLine(“A + B = “+sum);
Console.ReadLine();
}
}
}
O/P:-
C# Programming
Enter the First number: 2
Enter the Second number: 5
A + B = 7
// 4. Write a program to display Area of Rectangle numbers using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int l , b, area = 0;
//Console.WriteLine(“C# Programming”);
Console.Write(“Enter the length of rectangle: “);
l = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the breadth of rectangle: “);
b = Convert.ToInt32(Console.ReadLine());
area = l*b;
Console.WriteLine(“Area of rectangle = “+area);
Console.ReadLine();
}
}
}
O/P:-
Enter the length of rectangle: 10
Enter the breadth of rectangle: 6
Area of rectangle = 60
c# basic programs:
// 5. Write a program to display Area of Circle numbers using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
double r, area=0;
//Console.WriteLine(“C# Programming”);
Console.Write(“Enter the radious of Circle: “);
r = Convert.ToDouble(Console.ReadLine());
area = (float)3.142*r*r;
Console.WriteLine(“Area of Circle = “+area);
Console.ReadLine();
}
}
}
O/P:-
Enter the radious of Circle: 1.0
Area of Circle = 3.14199995994568
// 6. Write a program to display swapping 2 numbers using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int a, b, t = 0;
Console.Write(“Enter the 1st number: “);
a = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the 2nd number: “);
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Before Swap: “);
Console.WriteLine(“A: “+a);
Console.WriteLine(“B “+b);
t = a;
a = b;
b = t;
Console.WriteLine(“After Swap: “);
Console.WriteLine(“A: “+a);
Console.WriteLine(“B: “+b);
Console.ReadLine();
}
}
}
O/P:-
Enter the 1st number: 10
Enter the 2nd number: 20
Before Swap:
A: 10
B: 20
After Swap:
A: 20
B: 10
// 7. Write a program to display even no and odd no using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int n;
Console.Write(“Enter the number: “);
n = Convert.ToInt32(Console.ReadLine());
if (n % 2 == 0)
Console.WriteLine(n + ” is Even number”);
else
Console.WriteLine(n + ” is Odd number”);
Console.ReadLine();
}
}
}
O/P:-
Enter the number: 10
10 is Even number
c# basic programs:
// 8. Write a program to demonstrate parameter passing mechanism and out parameter.
// Call by Value
using System;
namespace CSharpMethod
{
class MethodParaFormalValue
{
private static void Main(string[] args)
{
int value = 500;
Console.WriteLine(“Before Method Execution, value={0}”, value);
ChangePara(value);
Console.WriteLine(“After Method Execution, value={0}”, value);
Console.ReadLine();
}
static void ChangePara(int value)
{
value = 600;
Console.WriteLine(” Definition Value=” + value);
}
}
}
O/P:-
Before Method Execution, value=500
Definition Value=600
After Method Execution, value=500
// Call by Reference
using System;
namespace CSharpMethod
{
class MethodParaFormalValue
{
private static void Main(string[] args)
{
int value = 500;
Console.WriteLine(“Before Method Execution, value={0}”, value);
ChangePara(ref value);
Console.WriteLine(“After Method Execution, value={0}”, value);
Console.ReadLine();
}
static void ChangePara(ref int value)
{
value = 600;
}
}
}
O/P:-
Before Method Execution, value=500
After Method Execution, value=600
// OUT Parameter
using System;
namespace OutParaMethed
{
class Program
{
static void Main(string[] args)
{
int i = 5;
Program obj = new Program();
Console.WriteLine(“Before calling function value of i: “+i);
obj.mul(out i);
Console.WriteLine(“After calling function value of i: ” + i);
Console.ReadKey();
}
public void mul( out int i)
{
i = 2;
i = i + 10;
}
}
}
O/P:
Before calling functions value of i: 5
After calling function value of i: 12
c# basic programs:
//9. Write a program to demonstrate type casting
// Implicit Type Conversion
using System;
namespace TypeCasting
{
class Program
{
// Main Method
public static void Main(String []args)
{
int i = 150;
// automatic type conversion
long l = i;
// automatic type conversion
float f = l;
// Display Result
Console.WriteLine(“Int value ” +i);
Console.WriteLine(“Long value ” +l);
Console.WriteLine(“Float value ” +f);
Console.ReadLine();
}
}
}
O/P:-
Int value 150
Long value 150
Float value 150
// Explicit Type Conversion
using System;
namespace TypeConversion
{
class ExplicitConversion
{
static void Main(string[] args)
{
double d = 321.55;
int i;
// cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
O/P:-
321
// 10. Write a program to demonstrate partial class.
//File Name: Program.cs
using System;
namespace PartialClass
{
class Program
{
static void Main(string[] args)
{
oddeven myRecord = new oddeven();
myRecord.getRecord();
myRecord.display();
Console.ReadLine();
}
}
}
//File Name: Class1.cs
using System;
namespace PartialClass
{
public partial class oddeven
{
int n;
public void getRecord()
{
Console.WriteLine(“Enter the number: “);
n = Convert.ToInt32(Console.ReadLine());
}
}
}
//File Name: Class2.cs
using System;
namespace PartialClass
{
public partial class oddeven
{
public void display()
{
if(n % 2 ==0)
Console.WriteLine(n+” is Even number”);
else
Console.WriteLine(n + ” is Odd number”);
}
}
}
O/P:-
Enter the number:
89
89 is Odd number
c# basic programs:
// 11. Write a program to display largest number among 2 numbers using C#
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int x,y;
Console.Write(“Enter the first number: “);
x = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the second number: “);
y = Convert.ToInt32(Console.ReadLine());
if (x > y)
{
Console.WriteLine(x + ” is largest number”);
}
else
if (y > x)
{
Console.WriteLine(y + ” is largest number”);
}
else
{
Console.WriteLine(x + ” Both are equal”);
}
Console.ReadLine();
}
}
}
O/P:-
Enter the first number: 18
Enter the second number: 23
23 is largest number
// 12. Write a program to display largest number among 3 different numbers using C#
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int x,y,z;
Console.Write(“Enter the first number: “);
x = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the second number: “);
y = Convert.ToInt32(Console.ReadLine());
Console.Write(“Enter the third number: “);
z = Convert.ToInt32(Console.ReadLine());
if (x > y && x > z)
{
Console.WriteLine(x + ” is largest number”);
}
else
if (y > z)
{
Console.WriteLine(y + ” is largest number”);
}
else
{
Console.WriteLine(z + ” is largest number”);
}
Console.ReadLine();
}
}
}
O/P:-
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
30 is largest number
// 13. Write a program to display given number is +ve or -ve or natural using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int x;
Console.Write(“Enter the first number: “);
x = Convert.ToInt32(Console.ReadLine());
if (x > 0)
{
Console.WriteLine(x + ” is +ve number.”);
}
else
if(x<0)
{
Console.WriteLine(x + ” is -ve number.”);
}
else
{
Console.WriteLine(x + ” is nutral.”);
}
Console.ReadLine();
}
}
}
O/P:-
Enter the first number: 100
100 is +ve number.
// 14. Write a program to display given year is leap year or not using C#.
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int year;
Console.Write(“Enter the year: “);
year = Convert.ToInt32(Console.ReadLine());
if (year % 4 == 0 || year % 400 == 0)
{
Console.WriteLine(year + ” is leap year.”);
}
else
if (year % 100 == 0 || year % 4 != 0)
{
Console.WriteLine(year + ” is not leap year.”);
}
Console.ReadLine();
}
}
}
O/P:-
Enter the year: 2024
2024 is leap year.
// 15. Write a C# program to give 3 subject marks and show total, percentage and grade
using System;
namespace BasicConsoleApplication
{
class Addition
{
static void Main(string[] args)
{
int c, cpp, java, tot = 0;
double per = 0;
Console.WriteLine(“Enter the 3 subject Mraks: “);
c = Convert.ToInt32(Console.ReadLine());
cpp = Convert.ToInt32(Console.ReadLine());
java = Convert.ToInt32(Console.ReadLine());
tot = c + cpp + java;
per = (double)tot / 3;
Console.WriteLine(“C Prog. Marks: ” + c);
Console.WriteLine(“Cpp Prog. Marks: ” + cpp);
Console.WriteLine(“Java Prog. Marks: ” + java);
Console.WriteLine(“Total Marks: ” + tot);
Console.WriteLine(“Percentage : ” + per);
if (per >= 40 && per < 50)
{
Console.WriteLine(“Gread: C”);
}
else
if (per >= 50 && per < 60)
{
Console.WriteLine(“Gread: B”);
}
else
if (per >= 60 && per < 70)
{
Console.WriteLine(“Gread: A”);
}
else
if (per >= 70 && per < 80)
{
Console.WriteLine(“Gread: A+”);
}
else
if (per >=80 && per < 100)
{
Console.WriteLine(“Gread: O”);
}
Console.ReadLine();
}
}
}
O/P:-
Enter the 3 subject Marks:
67
80
90
C Prog. Marks: 67
Cpp Prog. Marks: 80
Java Prog. Marks: 90
Total Marks: 237
Percentage : 79
Gread: A+
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining