1. Add two numbers in Java.
import java.io.*;class add
{
public static void main(string args[]) throws IOException
{
int x,y,z;
InputStreamReader is= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader( is );
System.out.println("Enter 1st no");
x=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd no");
y=Integer.parseInt(br.readLine());
z=x+y;
System.out.println("The sum is .. "+z);
}
}
Sample Output :
Enter 1st no
1
Enter 2nd no
1
The sum is .. 2
2. Write a program to accept 3 no and display the largest no.
import java.io.*;class largest
{
public static void main(string args[]) throws IOException
{
int x,y,z;
InputStreamReader is= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader( is );
System.out.println("Enter 1st no");
x=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd no");
y=Integer.parseInt(br.readLine());
System.out.println("Enter 3rd no");
z=Integer.parseInt(br.readLine());
if((x>y)&&(x>z))
{
System.out.println("Max="+x);
}
else if(y>z)
{
System.out.println("Max="+y);
}
else
{
System.out.println("Max="+z);
}
}
}
Sample Output :
Enter 1st no
12
Enter 2nd no
11
Enter 3rd no
34
Max=34
3. Write a program to accept a no and display its table.
import java.io.*;
class table
{
public static void main(String args[])Throws IOException
{
int x,n=1;
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter a no");
x=Integer.parseInt(br.readLine());
while(n<=10)
{
System.out.println(x+"*"+n+"="+x*n);
n++;
}
}
}
Enter a no 11
11*1=11
11*2=22
11*3=33
11*4=44
11*5=55
11*6=66
11*7=77
11*8=88
11*9=99
11*10=110
4. Write a program to accept a no and calculate its factorial.
import java.io.*;class factorial
{
public static void main(String args[]) throws IOException
{
int n,c,fact=1;
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(is);
System.out.println("Enter a no");
n=Integer.parseInt(br.readLine());
if(n<0)
{
System.out.println("Number should be non negetive");
}
else
{
for(c=1;c<=n;c++)
fact=fact*c;
System.out.println("Factorial is .... "+fact);
}
}
}
Sample output :
Enter a no
4
Factorial is .... 24
5. Write a program to accept base and exponent and calculate power for the number.
import java.io.*;class power
{
public static void main(String args[])throws IOException
{
int x,y,z=1;
InputStreamReader is= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter a base");
x=Integer.parseInt(br.readLine());
System.out.println("Enter a exponent");
y=Integer.parseInt(br.readLine());
while(y>0)
{
z=z*x;
y--;
}
System.out.println("The Power is .. "+z);
}
}
Sample Output :
Enter a base
2
Enter a exponent
3
The Power is .. 8
6. To find a square root of a number.
import java.io.*;class sqrt
{
public static void main(String args[]) throws IOException
{
int x;
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(is);
System.out.println("Enter a number");
x=Integer.parseInt(br.readLine());
Syste,.out.println("SQRT="+Math.sqrt(x));
}
}
Sample Output:
Enter a number
49
SQRT=7.0
7. Java Program to check Even or Odd number
import java.util.Scanner;
class B
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
//The input provided by user is stored in num
Scanner input = new Scanner(System.in);
num = input.nextInt();
/* If number is divisible by 2 then it's an even number
* else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
Output :
Enter an Integer number :
12
Entered number is even
8. Example Java Program to check whether a given number is prime or not
import java.util.*;
public class B
public class B
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number: ");
int num = scanner.nextInt();
B primeNum = new B();
if ( primeNum.isPrime(num) ) {
System.out.printf("\n Result: The number %d is Prime", num);
} else {
System.out.printf("\n Result: The number %d is not Prime", num);
}
}
// Method to check whether the number is prime or not
public boolean isPrime(int num) {
if ( num < 2 ) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if ( num % i == 0 ) {
return false;
}
}
return true;
}
}
OUTPUT :
Please enter a number : 23
Result: The number 23 is Prime
9. Write Java program to check if a number is Armstrong number or not?
class Armstrong{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); int n = num; //use to check at last time int check=0,remainder; while(num > 0){ remainder = num % 10; check = check + (int)Math.pow(remainder,3); num = num / 10; } if(check == n) System.out.println(n+" is an Armstrong Number"); else System.out.println(n+" is not a Armstrong Number"); } }
Output :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no.
No comments:
Post a Comment