Question 46 : S = 1 + 12 + 123 + 1234 + ..... + n terms.
Print and find sum of the series.
Java Program :
class series_2
{
int n,sum=1;
series_2(int a)
{
n=a;
cal();
}
void cal()
{
int s=1;
System.out.print("The series : 1+");
for(int i=2;i<=n;i++)
{
s=(s*10)+i;
sum=sum+s;
if(i==n)
{
System.out.print(s);
break;
}
System.out.print(s+"+");
}
System.out.print("\nSum of the series : "+sum);
}
}
this might work better:
ReplyDeleteclass asn25b
{
public static void main(int n)
{
int sum=1;
int s=1;
for(int i=2;i<=n;i++)
{
s=(s*10)+i;
sum=sum+s;
if(i==n)
{
break;
}
}
System.out.println("Sum of the series : "+sum);
}
}
class SumSeries
ReplyDelete{
void main(int n)
{
int i;long a=0,s=0;
for(i=1;i<=n;i++)
{
a=10*a+i;
s+=a;
}
System.out.println("The Required Output = \t\t"+s);
}
}
how to do this program using for loop
ReplyDeleteThanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
import java.util.*
ReplyDeleteclass prg
{
public static void main (String args[])
{
int n,i,s=0;
Scanner SC =new Scanner(System.in);
System.out.println("Enter the limit");
n=SC.nextInt();
for(i=1;i<=n;i++)
{
s=s*10+i;
}
System.out.println("Sum of the series="+s);
}
}
Thanks for your informative article,Your post helped me to understand the future and career prospects &Keep on updating your blog with such awesome article.
ReplyDeletehardware and networking training in chennai
hardware and networking training in porur
xamarin training in chennai
xamarin training in porur
ios training in chennai
ios training in porur
iot training in chennai
iot training in porur