Professional Documents
Culture Documents
package Assignment1;
import java.util.Scanner;
int a = sc.nextInt();
int b= sc.nextInt();
int c=a+b;
System.out.println("Addition : "+c);
Output :-
enter 1st no
enter 2nd no
Addition : 5
Q.2:- java program to check even or odd number.
package Assignment1;
import java.util.Scanner;
class OddEven4{
int a= sc.nextInt();
if(a%2==0)
System.out.println("no is even");
else
System.out.println("no is odd");
Output :-
enter 1st no
23
no is odd
Q.3:-java program to add two binary number.
package Assignment1;
import java.util.Scanner;
String a= sc.next();
String b= sc.next();
Output :-
enter 1st binary no
23
24
import java.util.Scanner;
void sumofcomplex()
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
sumOfComplexNumber.sumofcomplex();
}
Output :-
enter 1st no :
10
enter 2sd no :
20
enter 3rd no :
30
enter 4rth no :
40
import java.util.Scanner;
int a= sc.nextInt();
int b = sc.nextInt();
int c=a*b;
System.out.println("Multiply :"+c);
Output :-
enter 1st no
enter 2nd no
Multiply :6
Q.6:- java program to check leap year.
package Assignment1;
import java.util.Scanner;
System.out.println("enter no");
int n = sc.nextInt();
System.out.println("leap year");
else
Output :-
enter no
2021
package Assignment1;
import java.util.Scanner;
System.out.println("enter vowel");
char c = sc.next().charAt(0);
System.out.println("vowel");
else
System.out.println("consotent");
Output :-
enter vowel
vowel
Q.8 :- java program to calculate compound interest.
package Assignment1;
import java.util.Scanner;
System.out.println("enter no principal");
double p = sc.nextDouble();
System.out.println("enter no rate");
double r = sc.nextDouble();
System.out.println("enter no time");
double t = sc.nextDouble();
System.out.println("Principal : "+p);
Output :-
enter no principal
enter no rate
enter no time
15
Principal : 2.0
import java.util.Scanner;
int p = sc.nextInt();
int r = sc.nextInt();
int n = sc.nextInt();
int c = p*r*n/100;
System.out.println("Simple Interest"+c);
Output :-
enter 1st no
200
enter 2nd no
100
enter 3rd no
300
Simple Interest60000
Q.10 :- java program to find quotient and remainder.
package Assignment1;
import java.util.Scanner;
int a = sc.nextInt();
int b= sc.nextInt();
int c=a%b;
System.out.println("quotient : "+c);
Output :-
enter 1st no
enter 2nd no
quotient : 2
Q.10:- java program to calculate power of number.
package Assignment1;
import java.util.Scanner;
int result = 1;
result = result*base;
Output :-
enter 1st no
enter 2nd no
Calculator power : 4
Q.11 : - java program to display first 100 prime number.
package Assignment1;
import java.util.Scanner;
int no = sc.nextInt();
int temp=0;
if (i % j == 0) {
temp = temp + 1;
if(temp==0)
System.out.println(i);
temp=0;
}}
Output :-
enter your no
10
7
Q.12 :- java program to break integer into digits.
package Assignment1;
import java.util.Scanner;
int digit = 0;
while(number!=0)
digit = number%10;
number = number/10;
System.out.println(" result"+digit);
Output :-
enter your no
result2
Q.13:- java program to check prime number.
package Assignment1;
import java.util.Scanner;
int no = sc.nextInt();
int temp=0;
if (no % i == 0) {
temp = temp + 1;
if (temp == 0) {
System.out.println("prime no");
} else {
Output :-
enter your no
13
prime no
Q.14 : - java program to check if a given number is perfect square.
package Assignment1;
import java.util.Scanner;
int count=0;
if (number / i == i) {
count = count + 1;
if(count==0)
Output :-
enter your no
16
Perfect square 4 is 16
Q.15 :- java program to print Armstrong numbers between a given range.
package Assignment1;
import java.util.Scanner;
int no = sc.nextInt();
int t1 = no;
int leng = 0;
while(t1!=0)
leng = leng+1;
t1= t1/10;
int t2 = no;
int arm = 0;
int rem;
while(t2!=0)
int mul=1;
rem = t2%10;
mul = mul*rem;
}
arm = arm+mul;
t2 = t2/10;
if(arm==no)
System.out.println("Amstrong no");
else
Output :-
enter your no
153
Amstrong no
Q.15 :- java program to find sum of natural numbers.
package Assignment1;
import java.util.Scanner;
int i=1;
int sum=0;
int x= sc.nextInt();
while(i<=x)
sum= sum+i;
i++;
Output :-
enter your no
Sum of 3 is : 6
Q.16:- java program to check if a number is positive or Negative.
package Assignment1;
import java.util.Scanner;
int a = sc.nextInt();
if(a>0)
System.out.println("positive no");
else if(a<0)
System.out.println("Negative no");
else
System.out.println("Zero");
Output :-
enter your no
25
positive no
import java.util.Random;
System.out.println(number);
System.out.println(number1);
Output :-
603
461902347
Q.18:- java program to check Armstrong number.
package Assignment1;
import java.util.Scanner;
int no = sc.nextInt();
int t1=no;
int leng=0;
while(t1!=0)
t1 = t1/10;
leng = leng+1;
int t2 = no;
int arm=0;
int rem;
while(t2!=0) {
int mul = 1;
rem = t2 % 10;
}
arm = arm + mul;
t2 = t2 / 10;
if(arm==no)
System.out.println("Armstrong no");
else
Output :-
enter your no
12
Not Armstrong no
Q.19:- java program to find GCD of two numbers.
package Assignment1;
gcd = i;
Output :-
GCD number 7 and 5 of 1
Q.20:- java program to find largest of three numbers.
package Assignment1;
import java.util.Scanner;
int a = sc.nextInt();
int b= sc.nextInt();
int c= sc.nextInt();
System.out.println(" a is greater");
System.out.println("b is greater");
else
System.out.println("c is greater");
}
Output :-
enter 1st no
enter 2nd no
enter 3rd no
c is greater
Q.21:- java program to swap two numbers using bitwise operator.
package Assignment1;
import java.util.Scanner;
num1 = num1^num2;
num2 = num1^num2;
num1 = num1^num2;
System.out.println("1st no : "+num1);
System.out.println("2nd no : "+num2);
Output :-
enter 1st no
10
enter 2nd no
20
1st no : 20
2nd no : 10
Q.22: - java program to find smallest of three numbers using ternary operator.
package Assignment1;
import java.sql.SQLOutput;
import java.util.Scanner;
System.out.println("Smallest no "+result);
Output :-
enter 1st no
10
enter 2nd no
20
enter 3rd no
30
Smallest no 10
Q.23:- java program to find largest of three numbers using ternary operator.
package Assignment1;
import java.util.Scanner;
Output :-
enter 1st no
10
enter 2nd no
20
enter 3rd no
30
Greatest number : 30
import java.util.Scanner;
int n= sc.nextInt();
if(i%2==0)
Output :-
enter your no
10
even to n number : 2
even to n number : 4
even to n number : 6
even to n number : 8
Q.25:- java program to display odd numbers from 1 to n or 1 to 100.
package Assignment1;
import java.util.Scanner;
int n = sc.nextInt();
if(i%2==1)
Output :-
enter your no
10
Odd no to n number : 1
Odd no to n number : 3
Odd no to n number : 5
Odd no to n number : 7
Odd no to n number : 9
import java.util.Scanner;
int num=1;
System.out.println("enter no");
int n= sc.nextInt();
System.out.print(num+" ");
num++;
System.out.println();
Output :-
enter no
23
456
7 8 9 10
11 12 13 14 15
System.out.println(a+" "+b);
int c;
c=a+b;
System.out.println("FibonacciSeries :"+c);
a=b;
b=c;
Output :-
01
FibonacciSeries :1
FibonacciSeries :2
FibonacciSeries :3
FibonacciSeries :5
FibonacciSeries :8
FibonacciSeries :13
FibonacciSeries :21
FibonacciSeries :34
FibonacciSeries :55
FibonacciSeries :89
FibonacciSeries :144
import java.util.Scanner;
System.out.println("enter no");
int no = sc.nextInt();
fact = fact*i;
Output :-
enter no
Factorial no 5 is 120
Q.29:- java program to make a calculator using switch case.
package Assignment1;
import java.util.Scanner;
int a= sc.nextInt();
System.out.println("enter2nd no");
int b = sc.nextInt();
System.out.println("select choice(+,-,*./)");
int res;
switch (sym)
case"+": res=a+b;
System.out.println("Addition no : "+res);
break;
case"-": res=a-b;
System.out.println("Subtraction no : "+res);
break;
case"*": res=a*b;
System.out.println("Multiply no : "+res);
break;
case"/": res=a/b;
System.out.println("Division no : "+res);
break;
default :
System.out.println("invalid no");
break;
Output :-
enter 1st no
enter2nd no
select choice(+,-,*./)
Addition no : 5
Q.30:- java program to calculate grades of Student.
package Assignment1;
import java.util.Scanner;
int grade;
if(avg>=90)
grade ='A';
grade = 'B';
grade = 'C';
grade = 'D';
}
else
grade = 'E';
switch(grade)
case'A':
System.out.println("Excellent ! :");
break;
case'B':
System.out.println("Welldone! :");
break;
case'C':
System.out.println("Congratulation ! :");
break;
case'D':
System.out.println("Good ! :");
break;
case'E':
break;
default:
System.out.println("Fail");
Output :-
enter your marks
90
Excellent ! :
import java.util.Scanner;
int no = sc.nextInt();
int temp=no;
while(temp!=0) {
if(no==rev)
else
Output :-
Enter your no :
123
import java.util.Scanner;
System.out.println("enter no");
int no = sc.nextInt();
fact = fact*i;
Output :-
enter no
Factorial no 5 is 120
Q.33:- How to Generate Random number in java.
package Assignment1;
import java.util.Random;
System.out.println(Math.random());
int a = sc.nextInt();
Output :-
0.9581091282745225
Q.34:- How to print pattern in java.
package Assignment1;
System.out.print("");
System.out.print("*");
System.out.println();
Output :-
**********
Q.35:- How to compare two objects in java.
package Assignment1;
System.out.println(obj1==obj2);
System.out.println(obj1.equals(obj2));
Output:-
false
true
Q.36:- How to create object in java.
package Assignment1;
import java.util.Scanner;
Output:-
enter your no
4
Q.38:- java program to find LCM &HCF.
package Assignment1;
gcd = i;
int lcm=(n1*n2)/gcd;
System.out.println("Lcm="+lcm);
Output :-
GCD number 7 and 5 of 1Lcm=35
Q.39:- java program to find three number of Average.
package Assignment1;
import java.util.Scanner;
int a = sc.nextInt();
int b= sc.nextInt();
int c= sc.nextInt();
int d=(a+b+c)/3;
Output :-
enter 1st no
10000
enter 2nd no
10000
enter 3rd no
10000