Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Display the sum of first 10 natural numbers

C# Sharp For Loop: Exercise-2 with Solution

Write a C# Sharp program to find the sum of first 10 natural numbers.

Pictorial Presentation:

C# Sharp Exercises: Display the sum of first 10 natural numbers

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise2  
{  
    public static void Main() 
{
    int  j, sum = 0;

	Console.Write("\n\n");
    Console.Write("Find the sum of first 10 natural numbers:\n");
    Console.Write("----------------------------------------------");
    Console.Write("\n\n");
	
    Console.Write("The first 10 natural number are :\n");
     for (j = 1; j <= 10; j++)
    {
        sum = sum + j;
        Console.Write("{0} ",j);    
    }
    Console.Write("\nThe Sum is : {0}\n", sum);
  }
}

Sample Output:

Find the sum of first 10 natural numbers:                                                                     
----------------------------------------------                                                                
                  
The first 10 natural number are :                                                                             
1 2 3 4 5 6 7 8 9 10                                                                                          
The Sum is : 55 

Flowchart:

Flowchart: Display the sum of first 10 natural numbers

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display the first 10 natural numbers.
Next: Write a program in C# Sharp to display n terms of natural number and their sum.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.