You are on page 1of 8

program to print numbers from 1 to N???

solution -

import java.util.Scanner;

public class Numbers

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

int n=sc.nextInt();

for(int i = 1; i <= n; i++)

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

inupt - 10

output- 1 2 3 4 5 6 7 8 9 10

2.Program to print numbers from n to 1???

solution-

import java.util.Scanner;
public class RevNumber

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

int n=sc.nextInt();

for (int i = n; i >=1 ; i--)

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

input - 10

output - 10 9 8 7 6 5 4 3 2 1

3. write a program to check the given digit is odd or even???

solution-

import java.util.Scanner;

public class ToCheckOdd_Even

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


System.out.println("enter the number");

int n=sc.nextInt();

if(n%2 == 0)

System.out.println("given digit is even ");

else

System.out.println("given digit is odd ");

input - 10

output - given digit is even

4. write a program to print factorial of given number??

solution-

import java.util.Scanner;

public class Factorial_Program {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

int n=sc.nextInt();

int fact=1;

for (int i = 1; i <= n; i++)

{
fact=fact*i;

System.out.println(fact);

input = 5

output= 120

Write a program to find the FACTORIAL of a given RANGE of numbers??

solution

import java.util.Scanner;

public class Factorial_range

static int fact(int n)

int fact=1;

while(n>0)

fact=fact*n;

n--;

return fact;
}

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the range of factorial number");

int n=sc.nextInt();

for (int i = 1; i <= n; i++)

System.out.println(i+"!--->"+fact(i));

input - 5

output-

5!--->120

4!--->24

3!--->6

2!--->2

1!--->1

Write a program to check whether the given number is PRIME or not?


import java.util.Scanner;

public class ToCheckPrime {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

int n=sc.nextInt();

int m=n/2;

int flag=0;

if(n==0||n==1)

System.out.println(n+" is not a prime number");

else

for (int i = 2; i <= m; i++)

if(n%i==0)

flag=1;
System.out.println(n+" is not a prime number");

break;

if(flag==0)

System.out.println(n+" is a prime number");

enter the number

13

13 is a prime number

Assignment Programs

Write a program to display PRIME NUMBERS from 1 to n????

Write a program to display Multiplication Table for given number???

Write a program to check given number is palindrome or not???

input- 353
output- 353 --> palindrome

You might also like