Sie sind auf Seite 1von 22

Sum of Two Numbers Program in Java

▪ In this program we use two variables which have already contain values
and sum of these numbers store in third variable.

Sum of Two Numbers Program in Java

class Addition
{
public static void main(String[] args)
{
int a=10, b=20, c=0;
c=a+b;
System.out.println("Sum: "+c);
}
}

Output

Sum: 30

Java Program to Find Odd or Even number

▪ In below code i will show you how to design Java Program to Find Odd
or Even number. In this program we receive input value from keyboard
and find modulo 2 (num%2) of number if reminder of number is zero then
that number is even other wise odd. Even numbers are those which are
divisible by 2, and which numbers are not divisible 2 is called odd numbers.

Java Program to Find Odd or Even


import java.util.Scanner;
class Oddeven
{
public static void main(String[] args)
{
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
if(no%2==0)
{
System.out.println("Even number");
}
else
{
System.out.println("Odd number");
}
}
}

Output

Enter any number :


10
Even number

Java Program to Swap two Numbers

▪ Swapping is the process of exchange the values of two variables with each
other. For example variable num1 contains 20 and num2 contains 40 after
swap their values are num1 contains 40 and num2 contains 20. In below
code i will show exchange values of two variable using third variable and
without sing third variable.

Java Program to Swap two Numbers using third variable

import java.util.Scanner;
class Swapnumber
{
public static void main(String[] args)
{
int a, b, c;
Scanner s=new Scanner(System.in);
System.out.println("Enter Value in a :");
a=s.nextInt();
System.out.println("Enter Value in b :");
b=s.nextInt();
c=a;
a=b;
b=c;
System.out.println("Values in a:" +a);
System.out.println("Values in b:" +b);
}
}

Output

Enter Value in a : 10
Enter Value in b : 20
Value in a : 20
Value in b : 10
Java Program to Swap two numbers without using third variable

import java.util.Scanner;
class Swapnumber
{
public static void main(String[] args)
{
int a, b;
Scanner s=new Scanner(System.in);
System.out.println("Enter Value in a :");
a=s.nextInt();
System.out.println("Enter Value in b :");
b=s.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println("Values in a:" +a);
System.out.println("Values in b:" +b);
}
}

Output

Enter Value in a :
10
Enter Value in b :
20
Value in a : 20
Value in b : 10
Prime Number Program in Java

▪ A Prime number is a natural number greater than 1 that has no positive


divisors other than 1 and itself. It means it is only divisible by 1 and itself,
and it start from 2. The smallest prime number is 2. Here i will show you
how to write this program. You can also download below code nad extract
file, after extract file you can easily run the program.

Prime Number Program in Java

import java.util.Scanner;
class Primenumber
{
public static void main(String[] args)
{
int no, i, fect=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
if(no==1)
{
System.out.println("Smallest Prime number is 2");
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
System.out.println("Not Prime");
break;
}
}
if(no==i)
{
System.out.println("Prime");
}
}
}

Java Program to Find Factorial on Number

▪ Factorial of any number is the product of an integer and all the integers
below it for example factorial of 4 is 4! = 4 * 3 * 2 * 1 = 24. In below code
i will show you how to design Java program to find factorial, using for
loop you can perform this task.

Factorial Program in Java

import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
int no, fect=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
for(int i=1; i<=no; i++)
{
fect=fect*i;
}
System.out.println("Factorial is :" +fect);
}
}

Java program to print multiplication table


The concept of generate Table of any number is :
num * 1
num * 2
num * 3
num * 4
num * 5
num * 6
num * 7
num * 8
num * 9
num * 10
Example

import java.util.Scanner;
class Table
{
public static void main(String[] args)
{
int i,no,table=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number: ");
no=s.nextInt();
for(i=1; i<=10; i++)
{
table=no*i;
System.out.println(table);
}
}
}

Java Program to Reverse Number


Reverse number means reverse the position of all digits of any number. For
example reverse of 839 is 938. For this program you need modulus operator
concept and while loop, while loop is used for check condition and modulus used
for find the remainder.

Reverse Number Program in Java

import java.util.Scanner;
class Reverse
{
public static void main(String[] args)
{
int no,rev=0,r,a;
Scanner s=new Scanner(System.in);
System.out.println("Enter any no.: ");
no=s.nextInt();
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
System.out.println("Reverse: "+rev);
}
}

Java Program to print Fibonacci Series

Fibonacci series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series


we add two previous terms/digits and get next term/number.
Fibonacci Series in Java

import java.util.Scanner;
class Fibonacci
{
public static void main(String[] args)
{
int i,no, first=0, second=1, next;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of terms for Series: ");
no=s.nextInt();
first=0;
second=1;
System.out.println("Fibonacci series are: ");
for(i=0; i<no; i++)
{
System.out.println(first);
next = first + second;
first = second;
second = next;
}
}
}

Armstrong number Program in Java

Armstrong number is a number that is the sum of its own digits each raised to
the power of the number of digits is equal to the number itself. In java
programming you can easily write Armstrong Program in Java just follow
same concept of C programming, only need to use syntax of java programming
language.
For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634
Armstrong Program in Java

import java.util.Scanner;
class Armstrong
{
public static void main(String[] args)
{
int arm=0,a,b,c,d,no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any num :");
no=s.nextInt();
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
System.out.println("Armstrong :");
}
else
{
System.out.println("not Armstrong");
}
}
}

Java Program to Find Largest Number Among Three Numbers

Input three number from user and compare these number with each others and
find largest number among these three numbers. Here i am using ternary operator
for find largest number. Read more about ternary operator Click here
Find Largest Number Program in Java
import java.util.Scanner;

class greatestnumber
{
public static void main(String[] args)
{
int a,b,c,largest;
Scanner s=new Scanner(System.in);
System.out.println("Enter any three numbers: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();

if(a>=b && a>=c)


{
System.out.println("Largest number: "+a);
}
if(b>=a && b>=c)
{
System.out.println("Largest number: "+b);
}
if(c>=a && c>=b)
{
System.out.println("Largest number: "+c);
}
}
}

Palindrome Number Program in Java

A Palindrome number is a number that remains the same when its digits are
reversed. Like 16461, for example: we take 121 and reverse it, after revers it is
same as original number.
Steps to write program

• Get the number from user.


• Reverse it.
• Compare it with the number entered by the user.
• If both are same then print palindrome
• Else print not a palindrome.
Palindrome Number Program in Java

import java.util.Scanner;
class Palindrome
{
public static void main(String[] args)
{
int a,no,b,temp=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any num: ");
no=s.nextInt();
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
System.out.println("Palindrome");
}
else
{
System.out.println("not Palindrome");
}
}
}
Java Program to Find sum of Digits

To Find sum of digits means add all the digits of any number, for example we
take any number like 358. Its sum of all digits is 3+5+8=16. Using given code we
can easily design this program.
Example

import java.util.Scanner;
class Sumofdigit
{
public static void main(String[] args)
{
int a,no,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number: ");
no=s.nextInt();
while(no>0)
{
a=no%10;
no=no/10;
sum=sum+a;
}
System.out.println("Sum of Digits :"+sum);
}
}

Find Number of digits in Given Number Program in Java

Any number is a combination of digits, like 342 have three digits. For calculating
the number of digits of any number user have three possibilities to input values.

• Enter +ve Number


• Enter -ve Number
• Enter zero
Find Number of digits in Given Number in Java

import java.util.Scanner;
class Numofdigit
{
public static void main(String[] args)
{
int no,a=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number : ");
no = s.nextInt();

if(no<0)
{
no=no * -1;
}
else if(no==0)
{
no=1;
}
while(no>0)
{
no=no/10;
a++;
}
System.out.println("Number of Digits in given number is: "+a);
}
}

Buzz Fizz Program in Java


Write a program in C which prints the numbers from 1 to 100. But, multiples of
3 should be replaced with "Fizz", multiples of 5 should be replaced with "Buzz"
and multiples of both 3 and 5 should be replaced with "FizzBuzz"?.

Buzz Fizz Program in Java

class BuzzFizz
{
public static void main(String arg[])
{
int no, i;
Scanner s=new Scanner(System.in);
System.out.println("Enter range of numbers");
no=s.next();
for(i=1; i<=no; i++)
{
if((i % (3*5)) == 0)
{
System.out.println("FizzBuzz\n");
}
else if ((i % 5) == 0)
{
System.out.println("Buzz\n");
}
else if ((i % 3) == 0)
{
System.out.println("Fizz\n");
}
else
{
System.out.println(i);
}
}
}
}
Java Program to Convert Celsius to Fahrenheit

Fahrenheit and Celsius are two unit for measure temperature. We have standard
formula to Convert Celsius to Fahrenheit using this formula you can change
temperature Celsius to Fahrenheit.
Formula

f = c * 9/5 + 32;

Convert Celsius to Fahrenheit in Java

import java.util.Scanner;
class Celsius_to_Fahrenheit
{
public static void main(String[] args)
{
float cel, far;

Scanner s=new Scanner(System.in);


System.out.println("Enter temp. in Celsius :");
cel=s.nextInt();

far = cel * 9/5 + 32;

System.out.println("Temp. in Fahrenheit: "+far);


}
}

Output

Enter temp. in Celsius: 36


Temp. in Fahrenheit: 96.000
Java Program to Convert Fahrenheit to Celsius

Fahrenheit and Celsius are two unit for measure temperature. We have standard
formula to Convert Fahrenheit to Celsius using this formula you can change
temperature Fahrenheit to Celsius.
Formula

c = (f - 32) * 5/9;

Convert Celsius to Fahrenheit in Java

import java.util.Scanner;
class Fahrenheit_to_Celsius
{
public static void main(String[] args)
{
float cel, far;

Scanner s=new Scanner(System.in);


System.out.println("Enter temp. in Fahrenheit :");
far=s.nextInt();

c = (f - 32) * 5/9;

System.out.println("Temp. in Celsius: "+cel);


}
}

Output

Enter temp. in Fahrenheit: 98


Temp. in Celsius: 36.666668
A Special Number is one whose sum of the factorial of all the digits is equal to the
number itself.
For Example,

[math]145 = 1! + 4! + 5![/math]

Here is the logic I developed.


1. import java.io.*;
2. import java.util.*;
3.
4. public class Solution {
5.
6. static long[] array = {1,1,2,6,24,120,720,5040,40320,362880};
7.
8. static boolean solve(int num) {
9. String S = String.valueOf(num); //Convert Number to String
10. long sum = 0;
11.
12. for(int i=0;i<S.length();i++) {
13. //get factorial from global array defined above
14.
15. int index =
Integer.parseInt(Character.toString(S.charAt(i)));
16. sum +=array[index];
17.
18. }
19.
20. if(sum==num) return true;
21.
22. return false;
23. }
24.
25. public static Scanner in = new Scanner(System.in);
26.
27. public static void main(String[] args) throws IOException {
28. int num = in.nextInt();
29. boolean result = solve(num);
30.
31. System.out.println(result);
32. }
33.}

How do I write a Java program, using the Scanner class, to take a number as input
and check whether it is automorphic or not?

1. import java.util.*;
2. public class Automorphic{
3. public static void main(String[] args){
4. Scanner sc = new Scanner(System.in);
5. int n = sc.nextInt();
6. int square = n * n;
7. String s1 = Integer.toString(n);
8. String s2 = Integer.toString(square);
9. if(s2.endsWith(s1))
10. System.out.print("Automorphic");
11. else
12. System.out.print("Not Automorphic");
13. sc.close();
14. }
15.}

Check if a number is a Krishnamurthy Number or not


A Krishnamurthy number is a number whose sum of the factorial of digits is equal
to the number itself. For example 145, sum of factorial of each digits:
1! + 4! + 5! = 1 + 24 + 120 = 145
Examples:
Input : 145
Output : YES
Explanation: 1! + 4! + 5! =
1 + 24 + 120 = 145, which is equal to input,
hence YES.

Input : 235
Output : NO
Explanation: 2! + 3! + 5! =
2 + 6 + 120 = 128, which is not equal to input,
hence NO.

// Java program to check if a number


// is a krishnamurthy number.
import java.util.*;
import java.io.*;

class Krishnamurthy {
// function to calculate the factorial
// of any number
static int factorial(int n)
{
int fact = 1;
while (n != 0) {
fact = fact * n;
n--;
}
return fact;
}

// function to Check if number is krishnamurthy


static boolean isKrishnamurthy(int n)
{
int sum = 0;

int temp = n;
while (temp != 0) {
// calculate factorial of last digit
// of temp and add it to sum
sum += factorial(temp % 10);

// replace value of temp by temp/10


temp = temp / 10;
}

// Check if number is krishnamurthy


return (sum == n);
}

// Driver code
public static void main(String[] args)
{
int n = 145;
if (isKrishnamurthy(n))
System.out.println("YES");
else
System.out.println("NO");
}
}

Das könnte Ihnen auch gefallen