Write a program in C# Sharp to display the sum of the series [ 9 + 99 + 999 + 9999 ...]

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

Write a program in C# Sharp to display the sum of the series [ 9 + 99 + 999 + 9999 ...]

Sample Output:

                                  
Display the sum of the series [ 9 + 99 + 999 + 9999 ...]:                                                                                                      
-----------------------------------------------------------                            
Input the number or terms :5                                                                                                         
9   99   999   9999   99999                                                                                                         
The sum of the saries = 111105

الأجوبة

using System;  
public class Exercise21 
{  
    public static void Main()
{   
    int n,i,t=9;
	int sum =0;
	
	Console.Write("\n\n");
    Console.Write("Display the sum of the series [ 9 + 99 + 999 + 9999 ...]:\n");
    Console.Write("-----------------------------------------------------------");
    Console.Write("\n\n");  	
	
	Console.Write("Input the number or terms :");
    n= Convert.ToInt32(Console.ReadLine());  	
	for (i=1;i<=n;i++)
	{ sum +=t;
	  Console.Write("{0}   ",t);
	  t=t*10+9;
	}
	Console.Write("\nThe sum of the saries = {0} \n",sum);
  }	
}  
هل كان المحتوى مفيد؟

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

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