Program to count N numbers
Program to count N numbers using for loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
for(i=0; i<=n;i++) { System.out.println(i); } } } Output:-
Enter number to be count
12
0
1
2
3
4
5
6
7
8
9
10
11
12
Program to count n numbers using while loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
i=0;
while(i<=n) { System.out.println(i); i++; } } } Output:-
Enter number to be count
5
0
1
2
3
4
5
Program to count n numbers using do while loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
i=0;
do
{
System.out.println(i);
i++;
}while(i< n); } } Output:-
Enter number to be count
6
0
1
2
3
4
5
6
Program to count N numbers using for loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
for(i=0; i<=n;i++) { System.out.println(i); } } } Output:-
Enter number to be count
12
0
1
2
3
4
5
6
7
8
9
10
11
12
Program to count n numbers using while loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
i=0;
while(i<=n) { System.out.println(i); i++; } } } Output:-
Enter number to be count
5
0
1
2
3
4
5
Program to count n numbers using do while loop
Program:-
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("Enter number to be count");
n=num.nextInt();
i=0;
do
{
System.out.println(i);
i++;
}while(i< n); } } Output:-
Enter number to be count
6
0
1
2
3
4
5
6
Leave reply
Add your comments here