Q:

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

0

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

Sample Output:

Display the sum of n odd natural number:                                                                      
------------------------------------------                                                                    
                                                                                                              
Input number of terms : 5                                                                                     
                                                                                                              
The odd numbers are :1 3 5 7 9                                                                                
The Sum of odd Natural Number upto 5 terms : 25

All Answers

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

using System;  
public class Exercise8  
{  
    public static void Main() 
{
   int i,n,sum=0;
	Console.Write("\n\n");
    Console.Write("Display the sum of n odd natural number:\n");
    Console.Write("------------------------------------------");
    Console.Write("\n\n");   

   Console.Write("Input number of terms : ");
   n= Convert.ToInt32(Console.ReadLine());   
   Console.Write("\nThe odd numbers are :");
   for(i=1;i<=n;i++)
   {
     Console.Write("{0} ",2*i-1);
     sum+=2*i-1;
   }
   Console.Write("\nThe Sum of odd 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