Write a program to print 1 to 100 number in java.
If you want to become a part of step-by-step to become a logical programmer. Then this video and website for you. So join this website and my youtube channel and become a part of this event. I am going to discuss Java programming in java. become a java logical programmer in java. Software having knowledge about this logic and standard coding practice of these videos.
How to become a logical programmer series.
Question 3: Write a program to print 1 to 100 number in java. Take one value from the user and print the 1 to n number natural number in java.
Code:
import java.util.Scanner;
class Numberp
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//System.out.println("Enter the Number:");don't used
int n=sc.nextInt();
//Validate -ve,+ve,0
if(n<1){
System.out.println("Please enter Valid Number");
System.exit(0);
}
for(int i=1;i<=n;i++){
System.out.print(i+" ");
if(i<n)
System.out.print(",");
}
System.out.println(".");
}
}
Output:
First-1:
n=- -5
Please enter the valid number
Second:
n=10
1,2,3,4,5,6,7,8,9,10.
Third:
n=5
1,2,3,4,5.
Comments
Post a Comment