Q:

Write a program in C# Sharp to display n terms of natural number and their sum

0

Write a program in C# Sharp to display n terms of natural number and their sum

Sample Output:

Display n terms of natural number and their sum:                                                              
----------------------------------------------                                                                
                                                         
Input Value of terms : 5                                                                                      
                                                                                                              
The first 5 natural number are :                                                                              
1 2 3 4 5                                                                                                     
The Sum of Natural Number upto 5 terms : 15

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;  
public class Exercise3  
{  
    public static void Main() 
{
   int i,n,sum=0;
   
	Console.Write("\n\n");
    Console.Write("Display n terms of natural number and their sum:\n");
    Console.Write("----------------------------------------------");
    Console.Write("\n\n");
	
   Console.Write("Input Value of terms : ");
   n= Convert.ToInt32(Console.ReadLine());   
   Console.Write("\nThe first {0} natural number are :\n",n);
   for(i=1;i<=n;i++)
   {
     Console.Write("{0} ",i);
     sum+=i;
   }
   Console.Write("\nThe Sum of Natural Number upto {0} terms : {1} \n",n,sum);
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now