w3resource

C# Sharp For Loop : Exercises, Practice, Solution

C# Sharp For Loop [83 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a program in C# Sharp to display the first 10 natural numbers.
Expected Output :
1 2 3 4 5 6 7 8 9 10
Click me to see the solution

2. Write a C# Sharp program to find the sum of the first 10 natural numbers.
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
Click me to see the solution

3. Write a C# Sharp program that displays the sum of n natural numbers.
Test Data : 7
Expected Output :
The first 7 natural number is :
1 2 3 4 5 6 7
The Sum of Natural Number upto 7 terms : 28
Click me to see the solution

4. Write a C# Sharp program to read 10 numbers and find their average and sum.
Test Data :
Input the 10 numbers :
Number-1 :2
...
Number-10 :2
Expected Output :
The sum of 10 no is : 51
The Average is : 5.100000
Click me to see the solution

5. Write a C# Sharp program to display the cube of an integer up to given number.
Test Data :
Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
Click me to see the solution

6. Write a program in C# Sharp to display the multiplication table of a given integer.
Test Data :
Input the number (Table to be calculated) : 15
Expected Output :
15 X 1 = 15
...
...
15 X 10 = 150
Click me to see the solution

7. Write a program in C# Sharp to display the multiplication table vertically from 1 to n.
Test Data :
Input upto the table number starting from 1 : 8
Expected Output :
Multiplication table from 1 to 8
1x1 = 1, 2x1 = 2, 3x1 = 3, 4x1 = 4, 5x1 = 5, 6x1 = 6, 7x1 = 7, 8x1 = 8
...
1x10 = 10, 2x10 = 20, 3x10 = 30, 4x10 = 40, 5x10 = 50, 6x10 = 60, 7x10 = 70, 8x10 = 80
Click me to see the solution

8. Write a C# Sharp program to display the n terms of odd natural numbers and their sums.
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100
Click me to see the solution

9. Write a program in C# Sharp to display a right angle triangle with an asterisk.
The pattern like :

*
**
***
****

Click me to see the solution

10. Write a program in C# Sharp to display a pattern like a right angle triangle with a number.
The pattern like :

1
12
123
1234

Click me to see the solution

11. Write a program in C# Sharp to make such a pattern like a right angle triangle with a number which repeats a number in a row.
The pattern like :

1
22
333
4444

Click me to see the solution

12. Write a C# Sharp program to make such a pattern like a right angle triangle with the number increased by 1.
The pattern like :

1
2 3
4 5 6
7 8 9 10 

Click me to see the solution

13. Write a program in C# Sharp to make such a pattern like a pyramid with numbers increasing by 1.

   1 
  2 3 
 4 5 6 
7 8 9 10

Click me to see the solution

14. Write a program in C# Sharp to make such a pattern like a pyramid with an asterisk.

   *
  * * 
 * * *
* * * *

Click me to see the solution

15. Write a C# Sharp program to calculate the factorial of a given number.
Test Data :
Input the number : 5
Expected Output :
The Factorial of 5 is: 120
Click me to see the solution

16. Write a program in C# Sharp to display the n terms of even natural number and their sum.
Test Data :
Input number of terms : 5
Expected Output :
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30
Click me to see the solution

17. Write a program in C# Sharp to make such a pattern like a pyramid with a number which will repeat the number in the same row.

   1
  2 2
 3 3 3
4 4 4 4

Click me to see the solution

18. Write a program in C# Sharp to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].
Test Data :
Input the Value of x :2
Input the number of terms : 5
Expected Output :
the sum = -0.415873
Number of terms = 5
value of x = 2.000000
Click me to see the solution

19. Write a program in C# Sharp to display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
Click me to see the solution

20. Write a program in C# Sharp to display the pattern like pyramid using an asterisk and each row contain an odd number of an asterisks.

  *
 ***
*****

Click me to see the solution

21. Write a program in C# Sharp to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Test Data :
Input the number or terms :5
Expected Output :
9 99 999 9999 99999
The sum of the series = 111105
Click me to see the solution

22. Write a program in C# Sharp to print Floyd's Triangle.

1
01 
101
0101 
10101

Click me to see the solution

23. Write a program in C# Sharp to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....].
Test Data :
Input the value of x :3
Input number of terms : 5
Expected Output :
The sum is : 16.375000
Number of terms = 5
The value of x = 3.000000
Click me to see the solution

24. Write a program in C# Sharp to find the sum of the series [ x - x^3 + x^5 - x^7 + x^9 -.....].
Test Data :
Input the value of x : 2
Input number of terms : 5
The sum = 410
Number of terms = 5
The value of x = 2
Click me to see the solution

25. Write a C# Sharp program that displays the n terms of square natural numbers and their sum.
1 4 9 16 ... n Terms
Test Data :
Input the number of terms : 5
Expected Output :
The square natural upto 5 terms are :1 4 9 16 25
The Sum of Square Natural Number upto 5 terms = 55
Click me to see the solution

26. Write a program in C# Sharp to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.
Test Data :
Input the number of terms : 5
Expected Output :
1 + 11 + 111 + 1111 + 11111 +
The Sum is : 12345
Click me to see the solution

27. Write a C# Sharp program for checking if a number is a perfect number.
Test Data :
Input the number : 56
Expected Output :
The positive divisor : 1 2 4 7 8 14 28
The sum of the divisor is : 64
So, the number is not perfect.
Click me to see the solution

28. Write a C# Sharp program to find the ‘perfect’ numbers within a given number range.
Test Data :
Input the starting range or number : 1
Input the ending range of number : 50
Expected Output :
The Perfect numbers within the given range : 6 28
Click me to see the solution

29. Write a C# Sharp program to check whether a given number is an Armstrong number or not.
Test Data :
Input a number: 153
Expected Output :
153 is an Armstrong number.
Click me to see the solution

30. Write a C# Sharp program to find the Armstrong number for a given range of numbers.
Test Data :
Input starting number of range: 1
Input ending number of range : 1000
Expected Output :
Armstrong numbers in given range are: 1 153 370 371 407
Click me to see the solution

31. Write a program in C# Sharp to display the pattern like a diamond.

    * 
   *** 
  *****
 *******
*********
 *******
  *****
   ***
    * 

Click me to see the solution

32. Write a C# Sharp program to determine whether a given number is prime or not.
 Test Data :
Input a number: 13
Expected Output :
13 is a prime number.
Click me to see the solution

33. Write a C# Sharp program to display Pascal's triangle.
 Test Data :
Input number of rows: 5
Expected Output :

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Click me to see the solution

34. Write a program in C# Sharp to find prime numbers within a range of numbers.
Test Data :
Input starting number of range: 1
Input ending number of range : 50
Expected Output :
The prime number between 1 and 50 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Click me to see the solution

35. Write a program in C# Sharp to display the first n terms of Fibonacci series.
Fibonacci series 0 1 2 3 5 8 13 .....
Test Data :
Input number of terms to display : 10
Expected Output :
Here is the Fibonacci series upto to 10 terms :
0 1 1 2 3 5 8 13 21 34
Click me to see the solution

36. Write a program in C# Sharp to display such a pattern for n number of rows using a number starting with the number 1. The first and last number of each row will be 1.

  1
 121
12321

Click me to see the solution

37. Write a program in C# Sharp to display a number in reverse order.
Test Data :
Input a number: 12345
Expected Output :
The number in reverse order is : 54321
Click me to see the solution

38. Write a program in C# Sharp to check whether a number is a palindrome or not.
Test Data :
Input a number: 121
Expected Output :
121 is a palindrome number.
Click me to see the solution

39. Write a C# Sharp program to find the number and sum of all integers between 100 and 200 divisible by 9.
Expected Output :
Numbers between 100 and 200, divisible by 9 :
108 117 126 135 144 153 162 171 180 189 198
The sum : 1683
Click me to see the solution

40. Write a C# Sharp Program to display the following pattern using the alphabet.

A
A B A 
A B C B A
A B C D C B A 

Click me to see the solution

41. Write a program in C# Sharp to convert a decimal number into binary without using an array.
Test Data :
Enter a number to convert : 25
Expected Output :
The Binary of 25 is 11001.
Click me to see the solution

42. Write a program in C# Sharp to convert a binary number into a decimal number without using array, function and while loop.
Test Data :
Input a binary number :1010101
Expected Output :
The Binary Number : 1010101
The equivalent Decimal Number : 85
Click me to see the solution

43. Write a C# Sharp program to find the HCF (Highest Common Factor) of two numbers.
Test Data :
Input 1st number for HCF: 24
Input 2nd number for HCF: 28
Expected Output :
HCF of 24 and 28 is : 4
Click me to see the solution

44. Write a program in C# Sharp to find the LCM of any two numbers using HCF.
Test Data :
Input 1st number for LCM: 15
Input 2nd number for LCM: 20
Expected Output :
The LCM of 15 and 20 is : 60
Click me to see the solution

45. Write a program in C# Sharp to find the LCM of any two numbers.
Test Data :
Input 1st number for LCM: 15
Input 2nd number for LCM: 20
Expected Output :
The LCM of 15 and 20 is : 60
Click me to see the solution

46. Write a C# Sharp program to convert a binary number into a decimal number using the math function.
Test Data :
Input the binary number :1010100
Expected Output :
The Binary Number : 1010100
The equivalent Decimal Number is : 84
Click me to see the solution

47. Write a C# Sharp program to check whether a number is a Strong Number or not.
Test Data :
Input a number to check whether it is Strong number: 15
Expected Output :
15 is not Strong number.
Click me to see the solution

48. Write a C# Sharp program to find Strong Numbers within a range of numbers.
Test Data :
Input starting range of number : 1
Input ending range of number: 200
Expected Output :
The Strong numbers are :
1 2 145
Click me to see the solution

49. Write a C# Sharp program to find out the sum of the Arithmetic Progress series.
Test Data :
Input the starting number of the A.P. series: 1
Input the number of items for the A.P. series: 10
Input the common difference of A.P. series: 4
Expected Output :
The Sum of the A.P. series are :
1 + 5 + 9 + 13 + 17 + 21 + 25 + 29 + 33 + 37 = 190
Click me to see the solution

50. Write a program in C# Sharp to convert a decimal number into octal without using an array.
Test Data :
Enter a number to convert : 79
Expected Output :
The Octal of 79 is 117.
Click me to see the solution

51. Write a program in C# Sharp to convert an octal number to decimal without using an array.
Test Data :
Input an octal number (using digit 0 - 7) :745
Expected Output :
The Octal Number : 745
The equivalent Decimal Number : 485
Click me to see the solution

52. Write a C# Sharp program to find the sum of the Geometric Progress series.
Test Data :
Input the first number of the G.P. series: 1
Input the number or terms in the G.P. series: 5
Input the common ratio of G.P. series: 2
Expected Output :
The numbers for the G.P. series:
1 2 4 8 16 32
The tn terms of G.P. : 16.000000
The Sum of the G.P. series : 63.000000
Click me to see the solution

53. Write a program in C# Sharp to convert a binary number to octal.
Test Data :
Input a binary number :1001
Expected Output :
The Binary Number : 1001
The equivalent Octal Number : 11
Click me to see the solution

54. Write a program in C# Sharp to convert an octal number into binary.
Test Data :
Input an octal number :11
Expected Output :
The Octal Number : 11
The equivalent Binary Number : 1001
Click me to see the solution

55. Write a program in C# Sharp to convert a decimal number to hexadecimal. .
Test Data :
Input any Decimal number: 79
Expected Output :
The equivalent Hexadecimal Number : 4F
Click me to see the solution

56. Write a program in C# Sharp to check whether a number can be expressed as the sum of two prime numbers.
Test Data :
Input a positive integer: 16
Expected Output :
16 = 3 + 13
16 = 5 + 11
Click me to see the solution

57. Write a program in C# Sharp to print a string in reverse order.
Test Data :
Input a string to reverse : Welcome
Expected Output :
Reversed string is: emocleW
Click me to see the solution

58. Write a C# Sharp program to display an alphabet pattern like A with an asterisk.

Reversed string is:Display the pattern like 'A' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ***                                                                            
 *   *                                                                           
 *   *                                                                           
 *****                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   * 

Click me to see the solution

59. Write a C# Sharp program to display an alphabet pattern like B with an asterisk.

Display the pattern like 'B' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 ****                                                                            
 *   *                                                                           
 *   *                                                                           
 ****                                                                            
 *   *                                                                           
 *   *                                                                           
 ****              

Click me to see the solution

60. Write a C# Sharp program to display an alphabet pattern like C with an asterisk.

Display the pattern like 'C' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ***                                                                            
 *   *                                                                           
 *                                                                               
 *                                                                               
 *                                                                               
 *   *                                                                           
  ***           
 

Click me to see the solution

61. Write a C# Sharp program to display an alphabet pattern like D with an asterisk.

Display the pattern like 'D' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 ****                                                                            
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 ****          

Click me to see the solution

62. Write a C# Sharp program to display an alphabet pattern like E with an asterisk.

Display the pattern like 'E' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *****                                                                           
 *                                                                               
 *                                                                               
 ****                                                                            
 *                                                                               
 *                                                                               
 *****     

Click me to see the solution

63. Write a C# Sharp program to display an alphabet pattern like F with an asterisk.

Display the pattern like 'F' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *****                                                                           
 *                                                                               
 *                                                                               
 ****                                                                            
 *                                                                               
 *                                                                               
 *         

Click me to see the solution

64. Write a C# Sharp program to display an alphabet pattern like G with an asterisk.

Display the pattern like 'G' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ***                                                                            
 *   *                                                                           
 *                                                                               
 * ***                                                                           
 *   *                                                                           
 *   *                                                                           
  *** 

Click me to see the solution

65. Write a C# Sharp program to display an alphabet pattern like H with an asterisk.

Display the pattern like 'H' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *****                                                                           
 *   *                                                                           
 *   *                                                                           
 *   * 

Click me to see the solution

66. Write a C# Sharp program to display an alphabet pattern like I with an asterisk.

Display the pattern like 'I' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *****                                                                           
   *                                                                             
   *                                                                             
   *                                                                             
   *                                                                             
   *                                                                             
 *****    

Click me to see the solution

67. Write a C# Sharp program to display an alphabet pattern like J with an asterisk.

Display the pattern like 'J' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
   ***                                                                           
    *                                                                            
    *                                                                            
    *                                                                            
    *                                                                            
  * *                                                                            
   *      

Click me to see the solution

68. Write a C# Sharp program to display an alphabet pattern like K with an asterisk.

Display the pattern like 'K' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *  *                                                                            
 * *                                                                             
 **                                                                              
 * *                                                                             
 *  *                                                                            
 *   *     

Click me to see the solution

69. Write a C# Sharp program to display an alphabet pattern like L with an asterisk.

Display the pattern like 'L' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *                                                                               
 *                                                                               
 *                                                                               
 *                                                                               
 *                                                                               
 *                                                                               
 *****       

Click me to see the solution

70. Write a C# Sharp program to display an alphabet pattern like M with an asterisk.

Display the pattern like 'M' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 ** **                                                                           
 * * *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *       

Click me to see the solution

71. Write a C# Sharp program to display an alphabet pattern like N with an asterisk.

Display the pattern like 'N' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 **  *                                                                           
 * * *                                                                           
 *  **                                                                           
 *   *                                                                           
 *   *      

Click me to see the solution

72. Write a C# Sharp program to display an alphabet pattern like an O with an asterisk.

Display the pattern like 'O' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ***                                                                            
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
  ***     

Click me to see the solution

73. Write a C# Sharp program to display an alphabet pattern like P with an asterisk.

Display the pattern like 'P' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 ****                                                                            
 *   *                                                                           
 *   *                                                                           
 ****                                                                            
 *                                                                               
 *                                                                               
 *                 

Click me to see the solution

74. Write a C# Sharp program to display an alphabet pattern like Q with an asterisk.

Display the pattern like 'Q' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ***                                                                            
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 * * *                                                                           
 *  *                                                                            
  ** *          

Click me to see the solution

75. Write a C# Sharp program to display an alphabet pattern like R with an asterisk.

Display the pattern like 'R' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 ****                                                                            
 *   *                                                                           
 *   *                                                                           
 ****                                                                            
 * *                                                                             
 *  *                                                                            
 *   *     

Click me to see the solution

76. Write a C# Sharp program to display an alphabet pattern like S with an asterisk.

Display the pattern like 'S' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
  ****                                                                           
 *                                                                               
 *                                                                               
  ***                                                                            
     *                                                                           
     *                                                                           
 ****    

Click me to see the solution

77. Write a C# Sharp program to display an alphabet pattern like T with an asterisk.

Display the pattern like 'T' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *****                                                                           
   *                                                                             
   *                                                                             
   *                                                                             
   *                                                                             
   *                                                                             
   *  

Click me to see the solution

78. Write a C# Sharp program to display an alphabet pattern like U with an asterisk.

Display the pattern like 'U' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           

 *   *                                                                           
 *   *                                                                           
  ***     

Click me to see the solution

79. Write a C# Sharp program to display an alphabet pattern like V with an asterisk.

Display the pattern like 'V' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
  * *                                                                            
   * 

Click me to see the solution

80. Write a C# Sharp program to display an alphabet pattern like W with an asterisk.

Display the pattern like 'W' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 *   *                                                                           
 * * *                                                                           
 * * *                                                                           
  * *        

Click me to see the solution

81. Write a C# Sharp program to display an alphabet pattern like X with an asterisk.

Display the pattern like 'X' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
  * *                                                                            
   *                                                                             
  * *                                                                            
 *   *                                                                           
 *   * 

Click me to see the solution

82. Write a C# Sharp program to display an alphabet pattern like Y with an asterisk.

Display the pattern like 'Y' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
 *   *                                                                           
 *   *                                                                           
  * *                                                                            
   *                                                                             
   *                                                                             
   *                                                                             
   *     

Click me to see the solution

83. Write a C#Sharp program to display an alphabet pattern like Z with an asterisk.

Display the pattern like 'Z' with an asterisk:                                      
---------------------------------------------                                    
                                                                                 
*******                                                                          
     *                                                                           
    *                                                                            
   *                                                                             
  *                                                                              
 *                                                                               
*******   

Click me to see the solution

C# Sharp Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.