0% found this document useful (0 votes)
18 views7 pages

Javi

The document contains multiple Java programs that perform various mathematical operations and checks. These include addition, subtraction, multiplication, division, and remainder of two numbers; finding the greatest of three numbers; checking for palindrome and Armstrong numbers; determining if a number is prime; printing even numbers up to a given number; and calculating the factorial of a number. Each program utilizes the Scanner class for user input and implements specific logic to achieve the desired functionality.

Uploaded by

Om Mundlik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Javi

The document contains multiple Java programs that perform various mathematical operations and checks. These include addition, subtraction, multiplication, division, and remainder of two numbers; finding the greatest of three numbers; checking for palindrome and Armstrong numbers; determining if a number is prime; printing even numbers up to a given number; and calculating the factorial of a number. Each program utilizes the Scanner class for user input and implements specific logic to achieve the desired functionality.

Uploaded by

Om Mundlik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

 Write a program that asks user to enter 2 numbers and print their addition,

subtraction, multiplication, division and remainder.


import java.util.*;
class A
{
public static void main (String args[])
{
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
int num1,num2,add,sub,mul,rem;
double div;
System.out.println("Enter First Number:");
num1= sc1.nextInt();
System.out.println("Enter Second Number:");
num2= sc2.nextInt();
add=num1+num2;
System.out.println("Addition : " +add);
sub=num1-num2;
System.out.println("Substraction : " +sub);
mul=num1*num2;
System.out.println("Multiplication : " +mul);
div=num1/num2;
System.out.println("Division : " +div);
rem=num1%num2;
System.out.println("Remainder : " +rem);

}
 Write a program that asks the user to enter 3 numbers and prints the
greatest number.

import java.util.*;
class B
{

public static void main (String args[])


{
Scanner sc1= new Scanner(System.in);
Scanner sc2= new Scanner(System.in);
Scanner sc3= new Scanner(System.in);
int a,b,c;
System.out.println("Enter A:");
a = sc1.nextInt();
System.out.println("Enter B:");
b = sc2.nextInt();
System.out.println("Enter C:");
c = sc3.nextInt();

System.out.println("****Program of Greatest Number From Three


Number****");

if(a>b)
{
if(a>c)
{
System.out.println("A= "+a+" is Greatest");
}
else
{
System.out.println("C= "+c+" is Greatest");
}
}
else
{
if(b>c)
{
System.out.println("B= "+b+" is Greatest");
}
else
{
System.out.println("C= "+c+" is Greatest");
}
}
}
}
 Write a program that accepts a number from the user and checks
whether it is a Palindrome number.

import java.util.*;
class C
{
public static void main(String args [])
{
Scanner sc = new Scanner(System.in);
int num,rev=0,org,remainder;
System.out.println("Enter Number:");
num= sc.nextInt();
org=num;
while(num>0)
{

remainder=num%10;
rev=rev*10+remainder;
num=num/10;

}
if(org==rev)
{
System.out.println("Number is Pallindrome!");
}
else
{
System.out.println("Number is Not Pallindrome!");
}
}

}
 Write a program that accepts a number from the user and checks
whether it is an Armstrong number.
import java.util.Scanner;

public class D {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

if(isArmstrong(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}

scanner.close();
}

public static boolean isArmstrong(int number) {


int originalNumber = number;
int numberOfDigits = String.valueOf(number).length();
int sum = 0;

while(number > 0) {
int digit = number % 10;
sum += Math.pow(digit, numberOfDigits);
number /= 10;
}

return originalNumber == sum;


}
}
 Write a program that ask user to enter a number and checks whether
it is prime or not.

import java.util.Scanner;

public class PrimeNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}

scanner.close();
}

public static boolean isPrime(int number) {


if (number <= 1) {
return false; // 0 and 1 are not prime numbers
}

if (number <= 3) {
return true; // 2 and 3 are prime numbers
}

if (number % 2 == 0 || number % 3 == 0) {
return false; // numbers divisible by 2 or 3 are not prime
}

// check for divisibility by numbers other than 2 and 3


for (int i = 5; i * i <= number; i += 6) {
if (number % i == 0 || number % (i + 2) == 0) {
return false;
}
}

return true;
}
}
 Write a program that ask user to enter a number and prints even
numbers from 0 upto that number.

import java.util.Scanner;

public class F {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

System.out.println("Even numbers from 0 to " + number + " are:");


for (int i = 0; i <= number; i += 2) {
System.out.print(i + " ");
}

scanner.close();
}
}
 Write a program that ask user to enter a number and prints the
factorial of that number.

import java.util.Scanner;

public class G {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

long factorial = Fact(number);


System.out.println("Factorial of " + number + " is: " + factorial);

scanner.close();
}

public static long Fact(int number) {


if (number < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
}

if (number == 0 || number == 1) {
return 1;
}

return number * Fact(number - 1);


}
}

You might also like