Sum of Two Numbers Program in Java
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.
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
▪ 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.
Output
▪ 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.
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
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");
}
}
}
▪ 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.
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);
}
}
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);
}
}
}
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);
}
}
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 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");
}
}
}
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();
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
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);
}
}
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.
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);
}
}
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;
import java.util.Scanner;
class Celsius_to_Fahrenheit
{
public static void main(String[] args)
{
float cel, far;
Output
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;
import java.util.Scanner;
class Fahrenheit_to_Celsius
{
public static void main(String[] args)
{
float cel, far;
c = (f - 32) * 5/9;
Output
[math]145 = 1! + 4! + 5![/math]
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.}
Input : 235
Output : NO
Explanation: 2! + 3! + 5! =
2 + 6 + 120 = 128, which is not equal to input,
hence NO.
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;
}
int temp = n;
while (temp != 0) {
// calculate factorial of last digit
// of temp and add it to sum
sum += factorial(temp % 10);
// Driver code
public static void main(String[] args)
{
int n = 145;
if (isKrishnamurthy(n))
System.out.println("YES");
else
System.out.println("NO");
}
}