Simple C# Programs for Beginners

Simple C# Programs for Beginners 


C# Program to Generate Random Numbers
This C# Program Generates Random Numbers. Here random numbers are generated using the Random class and the next() in it.
Here is source code of the C# Program to Generate Random Numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Generate Random Numbers
3.  */
4. using System;
5. class Program
6. {
7.     static void Main()
8.     {
9.         Console.WriteLine("Some Random Numbers that are generated are : ");
10.         for (int i = 1; i < 10; i++)
11.         {
12.             Randfunc();
13.         }
14.     }
15.     static Random r = new Random();
16.     static void Randfunc()
17.     {
18.         int n = r.Next();
19.         Console.WriteLine(n);
20.         Console.ReadLine();
21.     }
22. }
Here is the output of the C# Program:
Some Random Numbers that are generated are :
1234567
8754352
9864930
8352048
1920472
2846104
7649207
4928756
9261746


C# Program to Check whether the Entered Number is Even or Odd
This C# Program checks if a given integer is Odd or Even. Here if a given number is divisible by 2 with the remainder 0 then the number is an Even number. If the number is not divisible by 2 then that number will be an Odd number.
Here is source code of the C# program which checks a given integer is odd or even. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Check whether the Entered Number is Even or Odd
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.  
9. namespace check1
10. {
11.     class Program
12.     {
13.         static void Main(string[] args)
14.         {
15.             int i;
16.             Console.Write("Enter a Number : ");
17.             i = int.Parse(Console.ReadLine());
18.             if (i % 2 == 0)
19.             {
20.                 Console.Write("Entered Number is an Even Number");
21.                 Console.Read();
22.             }
23.             else
24.             {
25.                 Console.Write("Entered Number is an Odd Number");
26.                 Console.Read();
27.             }
28.         }
29.     }
30. }
Here is the output of the C# Program:
Enter a Number : 25
Entered Number is an Odd Number

C# Program to Swap 2 Numbers
This C# Program Swaps 2 Numbers.It obtains two numbers from the user and swaps the numbers using a temporary variable.
Here is source code of the C# program that swaps two numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Swap two Numbers
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8. namespace Program
9. {
10.     class Program
11.     {
12.         static void Main(string[] args)
13.         {
14.             int num1, num2, temp;
15.             Console.Write("\nEnter the First Number : ");
16.             num1 = int.Parse(Console.ReadLine());
17.             Console.Write("\nEnter the Second Number : ");
18.             num2 = int.Parse(Console.ReadLine());
19.             temp = num1;
20.             num1 = num2;
21.             num2 = temp;
22.             Console.Write("\nAfter Swapping : ");
23.             Console.Write("\nFirst Number : "+num1);
24.             Console.Write("\nSecond Number : "+num2);
25.             Console.Read();
26.         }
27.     }
28. }
Here is the output of the C# Program:
Enter the First Number : 23
Enter the Second Number : 25
After Swapping :
First Number : 25
Second Number : 23

C# Program to Get a Number and Display the Number with its Reverse
This C# Program Gets a Number and Display the Number with its Reverse. Here we obtain a number from the user and display the digits in the reverse order.
Here is source code of the C# Program to Get a Number and Display the Number with its Reverse. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Get a Number and Display the Number with its Reverse
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.  
9. namespace Program
10. {
11.     class Program
12.     {
13.         static void Main(string[] args)
14.         {
15.             int num, reverse = 0;
16.             Console.WriteLine("Enter a Number : ");
17.             num = int.Parse(Console.ReadLine());
18.             while (num != 0)
19.             {
20.                 reverse = reverse * 10;
21.                 reverse = reverse + num % 10;
22.                 num = num / 10;
23.             }
24.             Console.WriteLine("Reverse of Entered Number is : "+reverse);
25.             Console.ReadLine();
26.  
27.         }
28.     }
29. }
Here is the output of the C# Program:
Enter a Number : 123
Reverse of Entered Number : 321
C# Program to Get a Number and Display the Sum of the Digits
This C# Program Gets a Number and Display the Sum of the Digits.The digit sum of a given integer is the sum of all its digits.
Here is source code of the C# Program to Get a Number and Display the Sum of the Digits . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Get a Number and Display the Sum of the Digits
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.  
9. namespace Program
10. {
11.     class Program
12.     {
13.         static void Main(string[] args)
14.         {
15.             int num, sum = 0, r;
16.             Console.WriteLine("Enter a Number : ");
17.             num = int.Parse(Console.ReadLine());
18.             while (num != 0)
19.             {
20.                 r = num % 10;
21.                 num = num / 10;
22.                 sum = sum + r;
23.             }
24.             Console.WriteLine("Sum of Digits of the Number : "+sum);
25.             Console.ReadLine();
26.  
27.         }
28.     }
29. }
Here is the output of the C# Program:
Enter a Number : 123
Sum of Digits of the Number : 6
C# Program to Count the Number of 1’s in the Entered Number
This C# Program Counts the Number of 1’s in the Entered Number. Here the array of numbers are obtained with its limit and number of 1’s in it is counted and displayed.
Here is source code of the C# Program to Count the Number of 1’s in the Entered Number. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Count the Number of 1's in the Entered Number
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.  
9. namespace ConsoleApplication16
10. {
11.     class Program
12.     {
13.         static void Main(string[] args)
14.         {
15.             int m, count = 0;
16.             Console.WriteLine("Enter the Limit : ");
17.             m = int.Parse(Console.ReadLine());
18.             int[] a = new int[m];
19.             Console.WriteLine("Enter the Numbers :");
20.             for (int i = 0; i < m; i++)
21.             {
22.                 a[i] = Convert.ToInt32(Console.ReadLine());
23.             }
24.             foreach (int o in a)
25.             {
26.                 if (o == 1)
27.                 {
28.                     count++;
29.                 }
30.             }
31.             Console.WriteLine("Number of 1's in the Entered Number : ");
32.             Console.WriteLine(count);
33.             Console.ReadLine();
34.         }
35.     }
36. }
Here is the output of the C# Program:
Enter the Limit : 5
Enter the Numbers :
1
2
1
4
1
Number of 1's in the Entered Number : 3
C# Program to Check Whether the Entered Year is a Leap Year or Not
This C# Program Checks Whether the Entered Year is a Leap Year or Not.When A year is divided by 4. If remainder becomes 0 then the year is called a leap year..
Here is source code of the C# Program to Check Whether the Entered Year is a Leap Year or Not. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Check Whether the Entered Year is a Leap Year or Not
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.  
9. namespace Program
10. {
11.     class leapyear
12.     {
13.         static void Main(string[] args)
14.         {
15.             leapyear obj = new leapyear();
16.             obj.readdata();
17.             obj.leap();
18.         }
19.         int y;
20.         public void readdata()
21.         {
22.             Console.WriteLine("Enter the Year in Four Digits : ");
23.             y = Convert.ToInt32(Console.ReadLine());
24.         }
25.         public void leap()
26.         {
27.             if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
28.             {
29.                 Console.WriteLine("{0} is a Leap Year", y);
30.             }
31.             else
32.             {
33.                 Console.WriteLine("{0} is not a Leap Year", y);
34.             }
35.             Console.ReadLine();
36.         }
37.     }
38. }
Here is the output of the C# Program:
Enter the Year in Four Digits : 1004
1004 is a Leap Year
C# Program to Display All the Prime Numbers Between 1 to 100
This C# Program Displays All the Prime Numbers Between 1 to 100. Here prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Here is source code of the C# Program to Display All the Prime Numbers Between 1 to 100. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
1. /*
2.  * C# Program to Display All the Prime Numbers Between 1 to 100
3.  */
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8. namespace PrimeNumber
9. {
10.     class Program
11.     {
12.         static void Main(string[] args)
13.         {
14.             bool isPrime = true;
15.             Console.WriteLine("Prime Numbers : ");
16.             for (int i = 2; i <= 100; i++)
17.             {
18.                 for (int j = 2; j <= 100; j++)
19.                 {
20.  
21.                     if (i != j && i % j == 0)
22.                     {
23.                         isPrime = false;
24.                         break;
25.                     }
26.  
27.                 }
28.                 if (isPrime)
29.                 {
30.                     Console.Write("\t" +i);
31.                 }
32.                 isPrime = true;
33.             }
34.             Console.ReadKey();
35.         }
36.     }
37. }
Here is the output of the C# Program:
Prime Numbers :
      2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97


Comments

Popular posts from this blog

Visual Basic 6 (VB6) Operators

Control Structures In Visual Basic 6.0

C# Delegates simple example in .NET