You are on page 1of 47

Assignment :- 1

Q.1:- Add two numbers.

package Assignment1;

import java.util.Scanner;

public class Addno1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a = sc.nextInt();

System.out.println("enter 2nd no");

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{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

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;

public class BinaryNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st binary no");

String a= sc.next();

System.out.println("enter 2nd binary no");

String b= sc.next();

int number1 = Integer.parseInt(a);

int number2 = Integer.parseInt(b);

int add = number1+number2;

System.out.println("Addition Binary : "+Integer.toBinaryString(add));

Output :-
enter 1st binary no

23

enter 2nd binary no

24

Addition Binary : 101111


Q.4 :- java program to add two complex number.
package Assignment1;

import java.util.Scanner;

public class Complexnumner {

Scanner sc = new Scanner(System.in);

void sumofcomplex()

System.out.println("enter 1st no :");

int a = sc.nextInt();

System.out.println("enter 2sd no :");

int b = sc.nextInt();

System.out.println("enter 3rd no :");

int c = sc.nextInt();

System.out.println("enter 4rth no :");

int d = sc.nextInt();

int real = (a + c);

int imaginary = (b + d);

System.out.println("Sum of complex number is : " + real + "+" + imaginary + "i");

public static void main(String[] args) {

Complexnumner sumOfComplexNumber = new Complexnumner();

sumOfComplexNumber.sumofcomplex();
}

Output :-
enter 1st no :

10

enter 2sd no :

20

enter 3rd no :

30

enter 4rth no :

40

Sum of complex number is : 40+60i


Q.5 :- java program to Multiply numbers.
package Assignment1;

import java.util.Scanner;

public class Multiply {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a= sc.nextInt();

System.out.println("enter 2nd no");

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;

public class Leapyear2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter no");

int n = sc.nextInt();

if((n%4==0) ||(n%400==0 && n%100!=0))

System.out.println("leap year");

else

System.out.println("not leap year");

Output :-
enter no

2021

not leap year


Q.7:- java program to check weather input character is vowel or consonant.

package Assignment1;

import java.util.Scanner;

public class VowelConsotent {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter vowel");

char c = sc.next().charAt(0);

if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')

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;

public class CompundInterest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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();

double interest = (p*r*t)/100;

System.out.println("Principal : "+p);

System.out.println("Rate interest : "+r);

System.out.println("time duration : "+t);

System.out.println("Simple interest : "+interest);

Output :-
enter no principal

enter no rate

enter no time

15

Principal : 2.0

Rate interest : 5.0

time duration : 15.0

Simple interest : 1.5


Q.9:- java program to calculate simple interest.
package Assignment1;

import java.util.Scanner;

public class simpleinterrest2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int p = sc.nextInt();

System.out.println("enter 2nd no");

int r = sc.nextInt();

System.out.println("enter 3rd no");

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;

public class Remainder {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a = sc.nextInt();

System.out.println("enter 2nd no");

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;

public class CalculatorPower {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int base = sc.nextInt();

System.out.println("enter 2nd no");

int power = sc.nextInt();

int result = 1;

for(int i=1; i<=power; i++)

result = result*base;

System.out.println("Calculator power : "+result);

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;

public class PrimenoList100 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int no = sc.nextInt();

int temp=0;

for(int i=1; i<=no; i++)

for(int j=2; j<=i-1; j++) {

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;

public class Breakdigit {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int number= sc.nextInt();

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;

public class PrimeNumber2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int no = sc.nextInt();

int temp=0;

for(int i=2; i<=no-1; i++) {

if (no % i == 0) {

temp = temp + 1;

if (temp == 0) {

System.out.println("prime no");

} else {

System.out.println("not prime no");

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;

public class PerfectSquare {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int number = sc.nextInt();

int count=0;

for(int i=1; i<=number; i++) {

if (number / i == i) {

System.out.println("Perfect square " + i + " is " + number);

count = count + 1;

if(count==0)

System.out.println("Not perfect square");

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;

public class ArmStrongno2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

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;

for(int i=1; i<=leng; i++)

mul = mul*rem;
}

arm = arm+mul;

t2 = t2/10;

if(arm==no)

System.out.println("Amstrong no");

else

System.out.println("Not armstrong no");

Output :-
enter your no

153

Amstrong no
Q.15 :- java program to find sum of natural numbers.
package Assignment1;

import java.util.Scanner;

public class Sumofnaturalno {

public static void main(String[] args) {

int i=1;

int sum=0;

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int x= sc.nextInt();

while(i<=x)

sum= sum+i;

i++;

System.out.println("Sum of "+x+"is : "+sum);

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;

public class Checkpositiveornegative {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

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

Q.17:- java program to generate Random number.


package Assignment1;

import java.util.Random;

public class RandomNumber {

public static void main(String[] args) {

Random random = new Random();

int number = random.nextInt(1000);

int number1 = random.nextInt();

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;

public class ArmstrongNumber3 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

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;

for (int i = 1; i <= leng; i++) {

mul = mul * rem;

}
arm = arm + mul;

t2 = t2 / 10;

if(arm==no)

System.out.println("Armstrong no");

else

System.out.println("Not Armstrong no");

Output :-
enter your no

12

Not Armstrong no
Q.19:- java program to find GCD of two numbers.
package Assignment1;

public class GCDnumber {

public static void main(String[] args) {

int n1=7, n2=5, gcd=1;

for(int i=1; i<=n1 && i<=n2; ++i)

if(n1%i==0 && n2%i==0)

gcd = i;

System.out.printf("GCD number %d and %d of %d", n1, n2, gcd);

Output :-
GCD number 7 and 5 of 1
Q.20:- java program to find largest of three numbers.
package Assignment1;

import java.util.Scanner;

public class Largestthreeno {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a = sc.nextInt();

System.out.println("enter 2nd no");

int b= sc.nextInt();

System.out.println("enter 3rd no");

int c= sc.nextInt();

if(a>b && a>c)

System.out.println(" a is greater");

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

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;

public class SwapnoUsingBitwise {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int num1 = sc.nextInt();

System.out.println("enter 2nd no");

int num2 = sc.nextInt();

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;

public class SmallestnoUsingternaryoprator {

public static void main(String[] args) {

int result, temp;

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int num1 = sc.nextInt();

System.out.println("enter 2nd no");

int num2 = sc.nextInt();

System.out.println("enter 3rd no");

int num3 = sc.nextInt();

temp = num1<num2 ? num1:num2;

result = num3<temp ? num3:temp;

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;

public class GreatestnoUsingBitwise {

public static void main(String[] args) {

int result, temp;

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int num1 = sc.nextInt();

System.out.println("enter 2nd no");

int num2 = sc.nextInt();

System.out.println("enter 3rd no");

int num3 = sc.nextInt();

temp = num1>num2 ? num1:num2;

result = num3>temp ? num3:temp;

System.out.println("Greatest number : "+result);

Output :-
enter 1st no

10

enter 2nd no

20

enter 3rd no
30

Greatest number : 30

Q.24:- java program to display even numbers from 1 to n or 1 to 100.


package Assignment1;

import java.util.Scanner;

public class Evennoton {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int n= sc.nextInt();

for(int i=1; i<n; i++)

if(i%2==0)

System.out.println("even to n number : " +i);

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;

public class Oddnotonumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int n = sc.nextInt();

for(int i=1; i<=n; i++)

if(i%2==1)

System.out.println("Odd no to n number : "+i);

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

Q.25:-java program to print Floyds triangle.


package Assignment1;

import java.util.Scanner;

public class FyadsTriangle {

public static void main(String[] args) {

int num=1;

Scanner sc = new Scanner(System.in);

System.out.println("enter no");

int n= sc.nextInt();

for(int i=1; i<=n; i++)

for(int j=1; j<=i; j++)

System.out.print(num+" ");

num++;

System.out.println();

Output :-
enter no

23
456

7 8 9 10

11 12 13 14 15

Q.26:- java program to print pascal triangle .


Q.27:- java program to display Fibonacci series using loops.
package Assignment1;

public class FibonacciSeries {

public static void main(String[] args) {

int a=0, b=1;

System.out.println(a+" "+b);

for(int i=0; i<=10; i++)

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

Q.28:- java program to Find Factorial using loops.


package Assignment1;

import java.util.Scanner;

public class FactorialNumber2 {

public static void main(String[] args) {

int fact =1;

Scanner sc = new Scanner(System.in);

System.out.println("enter no");

int no = sc.nextInt();

for(int i=1; i<=no; i++)

fact = fact*i;

System.out.println("Factorial no "+no+" is "+fact);

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;

public class CalculatorUsingSwitch {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a= sc.nextInt();

System.out.println("enter2nd no");

int b = sc.nextInt();

System.out.println("select choice(+,-,*./)");

String sym = sc.next();

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;

public class GradeofStudents {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your marks");

int avg = sc.nextInt();

int grade;

if(avg>=90)

grade ='A';

else if(avg>=80 && avg<90)

grade = 'B';

else if(avg>=60 && avg<80)

grade = 'C';

else if(avg>=40 && avg<60)

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':

System.out.println("Bad result ! :");

break;

default:

System.out.println("Fail");

Output :-
enter your marks

90
Excellent ! :

Q.31:- Palindrome program in java .


package Assignment1;

import java.util.Scanner;

public class Palindromenumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your no :");

int no = sc.nextInt();

int temp=no;

int rev=0, rem;

while(temp!=0) {

rem = temp % 10;

rev = rev * 10 + rem;

temp = temp / 10;

if(no==rev)

System.out.println(no+" Palindrome no");

else

System.out.println(no+" Not Palindrome no");

Output :-
Enter your no :
123

123 Not Palindrome no

Q.32:- Factorial program in java.


package Assignment1;

import java.util.Scanner;

public class FactorialNumber2 {

public static void main(String[] args) {

int fact =1;

Scanner sc = new Scanner(System.in);

System.out.println("enter no");

int no = sc.nextInt();

for(int i=1; i<=no; i++)

fact = fact*i;

System.out.println("Factorial no "+no+" is "+fact);

Output :-
enter no

Factorial no 5 is 120
Q.33:- How to Generate Random number in java.
package Assignment1;

import java.util.Random;

public class RnadomNo2 {

public static void main(String[] args) {

Random sc = new Random();

System.out.println(Math.random());

int a = sc.nextInt();

Output :-
0.9581091282745225
Q.34:- How to print pattern in java.
package Assignment1;

public class PatternNo {

public static void main(String[] args) {

for(int i=1; i<=10; i++)

for(int j=1; i<=j; j--)

System.out.print("");

System.out.print("*");

System.out.println();

Output :-
**********
Q.35:- How to compare two objects in java.
package Assignment1;

public class ComparetwoObjects {

public static void main(String[] args) {

String obj1 = new String("Govind");

String obj2 = new String("Govind");

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;

public class CreationObject {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter your no");

int a = sc.nextInt(); // Create objects

Output:-
enter your no

4
Q.38:- java program to find LCM &HCF.
package Assignment1;

public class HcfLcm {

public static void main(String[] args) {

int n1=7, n2=5, gcd=1;

for(int i=1; i<=n1 && i<=n2; ++i)

if(n1%i==0 && n2%i==0)

gcd = i;

int lcm=(n1*n2)/gcd;

System.out.printf("GCD number %d and %d of %d", 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;

public class Addno1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter 1st no");

int a = sc.nextInt();

System.out.println("enter 2nd no");

int b= sc.nextInt();

System.out.println("enter 3rd no");

int c= sc.nextInt();

int d=(a+b+c)/3;

System.out.println("Average of three number : "+d);

Output :-
enter 1st no

10000

enter 2nd no

10000

enter 3rd no

10000

Average of three number : 10000

You might also like