Computer Science, asked by Harshitrandhawa, 1 month ago

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 counter to generate n terms
for (c = 1; c <= n; c++) {
s = s * 10 + c;
System.out.print(s + " ");
}
}
}
will it work to display 1 12

123 1234
no spam​

Answers

Answered by TheUntrustworthy
2

Program:

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;

for (c = 1; c <= n; c++)

{

s = s * 10 + c;

System.out.print(s + " ");

}

}

}

Yes this loop is correct if the user enters 5 the output will be 12345 and if the user enters 8 the output will be 12345678.

Similar questions