Print Series 1, 11, 111, 1111……..n terms in Java.
import java.util.Scanner;
public class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int s = 0, c; // s for terms of series, c for n terms
for (c = 1; c <= n; c++) // To generate n terms
{
s = s * 10 + 1;
System.out.print(s + " ");
} //for ends
}
}
Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111......
BUILD SUCCESSFUL (total time: 3 seconds)
public class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int s = 0, c; // s for terms of series, c for n terms
for (c = 1; c <= n; c++) // To generate n terms
{
s = s * 10 + 1;
System.out.print(s + " ");
} //for ends
}
}
Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111......
BUILD SUCCESSFUL (total time: 3 seconds)
Java Resources are available for Download from below
Category: Core Java, Interview Question, Programs, Series, Written Test
Thnx (tomorrow I have no exam!😜)
ReplyDeleteBut I have
DeletePlease do the reverse of the program
ReplyDeleteEvery step will be same. Except
ReplyDeletefor (c=n; c>=1; c--)
{
s = s / 10; // / will give only the quotient so if n = 11111 s will be 1111.
S.o.p(s + " ");
}
It helped me a lot thanks...
ReplyDeleteHelped me alot thanks!!!
ReplyDeleteIt's not printing the in that output format. I want that if n =5 then the output must be like:-
ReplyDelete1,11,111,1111,11111.
A comma and a full stop must also be printed. Pls help.
s = s * 10 + 1;
DeleteSystem.out.print(s + " ");
Change the last line to
s = s * 10 + 1;
System.out.print(s + " , ");
This comment has been removed by the author.
DeleteJava Program......
Deleteint i=1;
int sum=0;
int j=1;
while (i<=5) {
sum=sum+j;
j=j*10;
i++;
}
System.out.println(sum+" + ");
It's not printing the in that output format. I want that if n =5 then the output must be like:-
ReplyDelete1,11,111,1111,11111.
A comma and a full stop must also be printed. Pls help.
How will I get it's sum? Can you please help?
ReplyDeletes is variable where its value is 0.
DeleteIts is not sum
0,1,2,3,6....
ReplyDeletecan you solve this one by using while loop
ReplyDeletewriting this in my online exam
ReplyDeleteI have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. 333 angel number
ReplyDelete