Write a program in C# Sharp to find the Sum of GP series

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

Write a program in C# Sharp to find the Sum of GP series

Sample Output:

Find the Sum of GP series:                                                                                  
----------------------------                                                                                    
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: 10                                                                   
The numbers for the G.P. series:                                                                            
 1  10  100  1000  10000  100000                                                                            
The tn terms of G.P. : 10000                                                                                
The Sum of the G.P. series : 111111

الأجوبة

using System;  
public class Exercise52
{  
    public static void Main()
{

    int g1,cr,j;
    int ntrm;
    double sum=0,tn,gpn;
	
	Console.Write("\n\n");
    Console.Write("Find the Sum of GP series:\n");
    Console.Write("----------------------------");
    Console.Write("\n\n");	


    Console.Write("Input the first number of the G.P. series: ");
    g1 = Convert.ToInt32(Console.ReadLine());	

    Console.Write("Input the number or terms in the G.P. series: ");
    ntrm = Convert.ToInt32(Console.ReadLine());	

    Console.Write("Input the common ratio of G.P. series: ");
    cr = Convert.ToInt32(Console.ReadLine());	

/*-------- generate G.P. series ---------------*/
     Console.Write("\nThe numbers for the G.P. series:\n ");
     Console.Write("1  ");

     for(j=1;j<=ntrm;j++)
       {
        gpn=Math.Pow(cr,j);
        Console.Write("{0}  ",gpn);
       }  
/*-------- End of G.P. series generate ---------------*/


    sum = (g1*(1 - (Math.Pow(cr,ntrm+1))))/(1-cr);
       tn = g1 * (Math.Pow(cr,ntrm-1));

    Console.Write("\nThe tn terms of G.P. : {0}\n\n",tn);
    Console.Write("\nThe Sum of the G.P. series : {0}\n\n",sum);
}
}
هل كان المحتوى مفيد؟

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

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