Write a program in C# Sharp to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

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

Write a program in C# Sharp to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

Sample Output:

Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms:                                                                                               
------------------------------------------------------------------                                                                                             
Input the numbe of terms : 6                                                                                                         
1 + 11 + 111 + 1111 + 11111 + 111111 +                                                                                                         
The Sum is : 123456

الأجوبة

using System;  
public class Exercise26  
{  
    public static void Main()
{
  int n,i,sum=0;
  int t=1;
  
	Console.Write("\n\n");
    Console.Write("Calculate the sum of the series 1 +11 + 111 + 1111 + .. n terms:\n");
    Console.Write("------------------------------------------------------------------");
    Console.Write("\n\n");    
  
  Console.Write("Input the numbe of terms : ");
   n= Convert.ToInt32(Console.ReadLine());  
  for(i=1;i<=n;i++)
  {
     Console.Write("{0} + ",t);
     sum=sum+t;
     t=(t*10)+1;
  }
  Console.Write("\nThe Sum is : {0}\n",sum);
   }
}
هل كان المحتوى مفيد؟

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

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