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

  • برمجة سي شارب
  • برمجة

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

الأجوبة

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);
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...