0% found this document useful (0 votes)
171 views

C# Source Code

source code of c# Program help and usefull codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

C# Source Code

source code of c# Program help and usefull codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Addition……………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
public class Exercise2
{
public static void Main()
{
System.Console.WriteLine(15 + 17);
Console.ReadLine();
}
}
}

Addition using function call………………………………………

using System;
public class funcexer3
{
public static int Sum(int num1, int num2)
{
int total;
total = num1 + num2;
return total;
}

public static void Main()


{
Console.Write("\n\nFunction to calculate the sum of two numbers :\n");
Console.Write("--------------------------------------------------\n");
Console.Write("Enter a number: ");
int n1= Convert.ToInt32(Console.ReadLine());
Console.Write("Enter another number: ");
int n2= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nThe sum of two numbers is : {0} \n", Sum(n1,n2) );
Console.ReadLine();
}
}
Factorial example………………………………

1. using System;
2. public class FactorialExample
3. {
4. public static void Main(string[] args)
5. {
6. int i,fact=1,number;
7. Console.Write("Enter any Number: ");
8. number= int.Parse(Console.ReadLine());
9. for(i=1;i<=number;i++){
10. fact=fact*i;
11. }
12. Console.Write("Factorial of " +number+" is: "+fact);
13. Console.ReadLine();
14.
15. }
16. }
Calculate area of rectangle……………………..

using System;

namespace TechStudyCSharp
{
class Program
{
static void Main(string[] args)
{

double length, breadth, area;


Console.WriteLine("Enter length of rectangle : ");
length = Convert.ToDouble( Console.ReadLine());

Console.WriteLine("Enter breadth of rectangle : ");


breadth = Convert.ToDouble(Console.ReadLine());
area = length * breadth;
Console.WriteLine("\nArea of rectangle: " + area);
Console.ReadKey();
}
}
}
C# Sharp Code: for average 0f 5 students…………………..

using System;

using System.IO;

public class Exercise9

public static void Main()

double number1,number2,number3,number4;

Console.Write("Enter the First number: ");

number1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the Second number: ");

number2 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the third number: ");

number3 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the fourth number: ");

number4 = Convert.ToDouble(Console.ReadLine());

double result = (number1 + number2 + number3 + number4) / 4;

Console.WriteLine("The average of {0}, {1}, {2}, {3} is: {4} ",

number1, number2, number3, number4, result);

Console.ReadLine();

}
}

* C# Program to Calculate Compound Interest


*/
using System;
namespace compund
{
class compound
{
static void Main(string[] args)
{
double Total = 0, interestRate, years, annualCompound, Amount;
Console.Write("Enter the Initial Amount : ");
Amount = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the Rate of Interest : ");
interestRate = Convert.ToDouble(Console.ReadLine()) / 100;
Console.Write("Enter the Number of Years : ");
years = Convert.ToDouble(Console.ReadLine());
Console.Write("Number of Times the Interest will be Compounded : ");
annualCompound = Convert.ToDouble(Console.ReadLine());
for (int t = 1; t < years + 1; t++)
{
Total = Amount * Math.Pow((1 + interestRate / annualCompound),
(annualCompound * t));
Console.Write("Your Total for Year {0} "
+ "is {1:F0}. \n", t, Total);

Console.ReadLine();
}
}
}
Let's see the C# example to convert decimal to binary.

using System;
public class ConversionExample
{
public static void Main(string[] args)
{
int n, i;
int[] a = new int[10];
Console.Write("Enter the number to convert: ");
n= int.Parse(Console.ReadLine());
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
Console.Write("Binary of the given number= ");
for(i=i-1 ;i>=0 ;i--)
{
Console.Write(a[i]);
}
}
}

Output:

Enter the number to convert:10


Binary of the given number= 1010
/*
* C# Program to Count number of Vowels and consonants from a given String
*/
using System;
class program
{
public static void Main()
{
char[] sentence = new char[100];

int i, vowels = 0, consonants = 0, special = 0, n;


Console.WriteLine("Enter the Length of the sentence \n");
n = int.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
sentence[i] = Convert.ToChar(Console.Read());
}
for (i = 0; sentence[i] != '\0'; i++)
{
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] == 't' || sentence[i] == '\0' || sentence[i] == ' ')
{
special = special + 1;
}
}

consonants = consonants - special;


Console.WriteLine("No. of vowels {0}", vowels);
Console.WriteLine("No. of consonants {0}", consonants);
Console.ReadLine();
Console.ReadLine();
}
}
C# Sharp Code: area of circle and circumference
using System;

public class Exercise5

public static void Main()

double r,per_cir;

double PI = 3.14;

Console.WriteLine("Input the radius of the circle : ");

r = Convert.ToDouble(Console.ReadLine());

per_cir = 2 * PI * r;

Console.WriteLine("Perimeter of Circle : {0}", per_cir);

Console.Read();

}
Reverse a Number in C#
Here is the code sample that shows you to reverse a number in C#.

1. using System;
2.
3. namespace ReverseNo
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. Console.WriteLine("Enter a No. to reverse");
10. int Number = int.Parse(Console.ReadLine()
);
11. int Reverse = 0;
12. while(Number>0)
13. {
14. int remainder = Number % 10;
15. Reverse = (Reverse * 10) + remainder;

16. Number = Number / 10;


17. }
18. Console.WriteLine("Reverse No. is {0}",Re
verse);
19. Console.ReadLine();
20. }
21. }
22. }
A Palindromic number or numeral Palindrome is a number, which remains the
same when its digits are reversed. Like 16461, for example, it is
“symmetrical”. The term palindromic is derived from Palindrome, which refers
to a word (such as rotor or racecar) whose spelling is unchanged when its
letters are reversed.

1. class Program {
2. static void Main(string[] args) {
3. int num, rem, sum = 0, temp;
4. //clrscr();
5. Console.WriteLine("\n >>>> To Find a Number is Pal
indrome or not <<<< ");
6. Console.Write("\n Enter a number: ");
7. num = Convert.ToInt32(Console.ReadLine());
8. temp = num;
9. while (num > 0) {
10. rem = num % 10; //for getting remainder b
y dividing with 10
11. num = num / 10; //for getting quotient by
dividing with 10
12. sum = sum * 10 + rem;
13. /*multiplying the sum with 10 and adding

14. remainder*/
15. }
16. Console.WriteLine("\n The Reversed Number is:
{0} \n", sum);
17. if (temp == sum) //checking whether the rever
sed number is equal to entered number
18. {
19. Console.WriteLine("\n Number is Palindrom
e \n\n");
20. } else {
21. Console.WriteLine("\n Number is not a pal
indrome \n\n");
22. }
23. Console.ReadLine();
24. }
25. }
26. C# Sharp Code:for x power n

using System;

1. public class RecExercise15

2. {

3.

4. public static void Main()

5. {

6. int bNum,pwr;

7. int result;

8. Console.Write("\n\n Recursion : Calculate power of any number :\n");

9. Console.Write("------------------------------------------------\n");

10.

11. Console.Write(" Input the base value : ");

12. bNum = Convert.ToInt32(Console.ReadLine());

13.

14. Console.Write(" Input the exponent : ");

15. pwr = Convert.ToInt32(Console.ReadLine());

16.

17. result=CalcuOfPower(bNum,pwr);//called the function CalcuOfPower


C # program for switch……………………………………………………………………..

using System;

namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/* local variable definition */
char grade = 'B';

switch (grade) {
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
case 'C':
Console.WriteLine("Well done");
break;
case 'D':
Console.WriteLine("You passed");
break;
case 'F':
Console.WriteLine("Better try again");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
Console.WriteLine("Your grade is {0}", grade);
Console.ReadLine();
}
Example for while loop………………….
Live Demo
using System;

namespace Loops {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;

/* while loop execution */


while (a < 20) {
Console.WriteLine("value of a: {0}", a);
a++;
}
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
C# Sharp Code: count number of digit………………….
using System;

class RecExercise5

static void Main(string[] args)

Console.Write("\n\n Recursion : Count the number of digits in a number :\n");

Console.Write("---------------------------------------------------------\n");

Console.Write(" Input any number : ");

int num = Convert.ToInt32(Console.ReadLine());

Console.Write("\n The number {0} contains number of digits : {1}


",num,getDigits(num, 0));

Console.ReadLine();

public static int getDigits(int n1, int nodigits)

if (n1 == 0)

return nodigits;

You might also like