You are on page 1of 36

1.

Write a Java program to perform basic arithmetic operations of two


numbers. 
SOL:
import java.util.Scanner;

public class arthematic

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a,b,c;

System.out.println("enter first number:");

a=sc.nextInt();

System.out.println("enter second number:");

b=sc.nextInt();

System.out.println("enter the choice\n1.add\n2.sub\n3.mul\n4.div");

System.out.println();

int ch;

ch=sc.nextInt();

while (ch!=0)

switch(ch)

case 1:

c=a+b;

System.out.println("Addition of " +a+ " and "+b+ " is" +c);

break;

case 2:
c=a-b;

System.out.println("subraction of " +a+ " and "+b+ " is" +c);

break;

case 3:

c=a*b;

System.out.println("multiplication of " +a+ " and "+b+ " is" +c);

break;

case 4:

c=a/b;

System.out.println("division of " +a+ " and "+b+ " is" +c);

break;

default:

System.out.println("enter valid choice");

System.out.println();

System.out.println("enter the choice\n1.add\n2.sub\n3.mul\n4.div");

System.out.println();

ch=sc.nextInt();

2. Write a Java program to perform operation (Addition, Subtraction,


Multiplication, Division) without using third variable.
Sol:
import java.util.Scanner;

public class arthematic1

{
public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a,b;

System.out.println("enter first number:");

a=sc.nextInt();

System.out.println("enter second number:");

b=sc.nextInt();

System.out.println("enter the choice\n1.add\n2.sub\n3.mul\n4.div");

System.out.println();

int ch;

ch=sc.nextInt();

while (ch!=0)

switch(ch)

case 1:

System.out.println("addition of " +a+ " and "+b+ " is" +(a+b));

break;

case 2:

System.out.println("subraction of " +a+ " and "+b+ " is" +(a-b));

break;

case 3:

System.out.println("multiplication of " +a+ " and "+b+ " is" +(a*b));

break;

case 4:
System.out.println("division of " +a+ " and "+b+ " is" +(a/b));

break;

default:

System.out.println("enter valid choice");

System.out.println();

System.out.println("enter the choice\n1.add\n2.sub\n3.mul\n4.div");

System.out.println();

ch=sc.nextInt();

3. Write a Java program to perform Multiplication of two numbers


without using  * operator.
import java.util.Scanner;
class mulwithoutoperator
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b,res=0;
a=sc.nextInt();
b=sc.nextInt();
for(int i=0;i<b;i++){
res=add(res,a);
}
System.out.println("The product of " +a+ " and " +b+ " is "+res);
}
static int add(int a,int b){
for (int i=0;i<b;i++){
a++;
}
return a;
}
}
4. Write a Java program to check the year is leap year or not.
import java.util.Scanner;
class leapyear
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int year;
year=sc.nextInt();
if(year%100==0){
if(year%400==0){
System.out.println("The year " +year+ " is a leap year");
}
else
{
System.out.println("The year " +year+ " is not a leap year");
}
}
else if ((year%4==0) && (year%100!=0))
{
System.out.println("The year " +year+ " is a leap year");
}
else
{
System.out.println("The year " +year+ " is not a leap year");
}
}
}
5. Write a Java program to print multiplication Table (1 to 15).
import java.util.Scanner;
class table
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,c;
System.out.println("Enter the integer u want to print table");
n=sc.nextInt();
for (int i=1;i<16;i++){
c=n*i;
System.out.println(n + " * " +i+ "=" +c);
System.out.println();
}
}
}
6. Write a Java Program to print ASCII Table.

Ans:

class ascii
{

public static void main(String args[])

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

char ch=(char)i;

System.out.println(i+"\t"+ch);

7. Write a Java program to Calculate and Display the sum of 4 digits


number.

import java.util.Scanner;

class sumofdigits

public static void main(String args[])

int n,i;
Scanner sc=new Scanner(System.in);

System.out.println("enter the number");

n=sc.nextInt();

int x,sum1=0,r;

x=n;

while(x!=0)

r=x%10;

x=x/10;

sum1+=r;

System.out.println("The sum of digits of "+n+" is "+sum1);

8. Write a Java program to Obtain the sum of first and last digit of four
digit number.

Sol:

import java.util.Scanner;

class sumoffirstandlast
{

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n,s=0;

System.out.println("Enter four digit number: ");

n=sc.nextInt();

if((n/10000)==0 && (n/1000)!=0)

s=s+(n%10);

s=s+(n/1000);

System.out.println("the sum is :" +s);

else

System.out.println("enter the four digited number");

}
9. Write a Java program to check whether given number is Armstrong or
not.
Sol :
import java.util.Scanner;

class armstrong

public static void main(String args[])

Scanner sc= new Scanner(System.in);

int n,r,s=0;

System.out.println("Enter the number: ");

n=sc.nextInt();

int x=n;

while (n!=0)

r=n%10;

n=n/10;

s=s+r*r*r;

if(s==x){

System.out.println("The number "+x+" is Armstrong number");

else{

System.out.println("it is not an armstrong number");

}
10. Write a Java program to print Fibonacci Series.

Sol:
import java.util.Scanner;

class fibonacci

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,n,n1=0,n2=1,n3;

System.out.println("Enter integer :");

n=sc.nextInt();

if(n==1)

System.out.println(0);

else if(n==2){

System.out.println(0+","+1);

else if(n>2)

System.out.print(0+","+1);

for(i=0;i<n-2;i++)

n3=n1+n2;

System.out.print(","+n3);

n1=n2;

n2=n3;
}

11. Write a Java program to print Factorial of Number

Sol :

import java.util.Scanner;

class factorial

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,n,r=1;

System.out.println("Enter integer :");

n=sc.nextInt();

for(i=n;i>0;i--)

r=r*i;

System.out.println("The factorial of "+n+" is "+r);


}

12. Write a Java program to swap two numbers using third variable.

Sol:

import java.util.Scanner;

class swapwithoutthird

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n1,n2,n3;

n1=sc.nextInt();

n2=sc.nextInt();

System.out.println("before swaping: "+ n1 +" " +n2);

N3=n1;

N1=n2;

N2=n3;

System.out.println("After swaping: "+ n1 +" " +n2);

13. Write a Java program to swap two numbers without using third
variable.
Sol:
import java.util.Scanner;

class swapwithoutthird

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n1,n2;

n1=sc.nextInt();

n2=sc.nextInt();

System.out.println("before swaping: "+ n1 +" " +n2);

n1=n1+n2;

n2=n1-n2;

n1=n1-n2;

System.out.println("After swaping: "+ n1 +" " +n2);

14. Write a Java program to calculate the power of Number.

Sol:

import java.util.Scanner;

class power

public static void main(String args[])

{
Scanner sc=new Scanner(System.in);

int base,power;

System.out.print("enter base: ");

base=sc.nextInt();

System.out.print("enter power: ");

power = sc.nextInt();

double res;

res=Math.pow(base,power);

System.out.println("the "+power+ "th power of "+base+" is "+res);

}}

15.Write a Java program to find sum of all digits between 10 and 50, which
are divisible by 3.

Sol:

class sumofdigitsbet10and50

public static void main(String args[])

int i,s=0;

for (i=10;i<=50;i++)
{

if(i%3==0)

s=s+i;

System.out.println("The sum of integers between 10 to 50 that are divisible by 3 is: "+s);

}}

16. Write a Java program to find out all odd numbers divisible by 5 from
the range of integers 200 to 800.
Sol:
class odddivisibleby5

public static void main(String args[])

int i,s=0;

System.out.print("The odd numbers from 200 to 800 divisible by 5 are: ");

for (i=200;i<=800;i++)

if(i%2!=0 && i%5==0)

System.out.print(i+",");

}
}

17.Write a Java Program to read the number and check whether it is


divisible by 3 and 5.
Sol:
import java.util.Scanner;

class divisibleby3and5

public static void main(String args[])

int ch,i,n;

Scanner sc=new Scanner(System.in);

System.out.println("enter choice\n0.Exit\n1.divisible by 3\n2.divisible by 5\n3.divisible by 3 and 5");

ch=sc.nextInt();

while(ch!=0)

System.out.println("Enter number: ");

n=sc.nextInt();

switch (ch)

case 1:

if(n%3==0)

System.out.println(n+" is divisible by 3");

else

System.out.println(n+" is not divisible by 3");

}
break;

case 2:

if(n%5==0)

System.out.println(n+" is divisible by 5");

else

System.out.println(n+" is not divisible by 5");

break;

case 3:

if(n%3==0 && n%5==0)

System.out.println(n+" is divisible by 3 and 5");

else

System.out.println(n+" is not divisible by 3 and 5");

break;

default:

System.out.println("enter valid choice");

System.out.println();
System.out.println("enter choice\n0.Exit\n1.divisible by 3\n2.divisible by 5\n3.divisible by 3 and 5");

ch=sc.nextInt();

18.Write a Java Program to display Subject Name based on room number.


If the user enters 604 then display Java Programming, If the user enters
605 then display Python programming for any other input display
Invalid input to the user
Sol:
import java.util.Scanner;

class subnamebasedonroom

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int n,i,ch;

System.out.println("enter your room number\n0.Exit\n604.java programming\n605.python


programming");

ch=sc.nextInt();

while(ch!=0)

switch(ch)

case 604:

System.out.println("java programming");

break;
case 605:

System.out.println("python programming");

break;

default:

System.out.println("Invalid input");

System.out.println();

System.out.println("enter your room number\n0.Exit\n604.java programming\n605.python


programming");

ch=sc.nextInt();

}}}

19.Write a Java Program to print the sum of first n numbers. If n is 3 then


print the sum of 1+2+3 to the user. Get n from the user
Sol:
import java.util.Scanner;

class sumofn

public static void main(String args[])

int n,i,s=0;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();
for(i=1;i<n+1;i++)

s=s+i;

System.out.println("the sum of "+n+" integers is : "+s);

2 2 2
20.Write a Java Program to print the sum of the series 1 +2 +3 up to n
terms
Sol:
import java.util.Scanner;

class sumofn

public static void main(String args[])

int n,i,s=0;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

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

s=s+i*i;

System.out.println("the sum of "+n+" integers is : "+s);

}
21.Write a Java Program to print the multiplication table by getting the n
from the user.
Sol:
import java.util.Scanner;
class table
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,c;
System.out.println("Enter the integer u want to print table");
n=sc.nextInt();
for (int i=1;i<16;i++){
c=n*i;
System.out.println(n + " * " +i+ "=" +c);
System.out.println();
}
}
}

22.Write a Java Program to provide the option of adding two numbers to


the user until the user wants to exit.
Sol:
import java.util.Scanner;

class addingnumbers

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a,b,ch;

ch=1;

while(ch!=0)

System.out.println("enter first number: ");

a=sc.nextInt();
System.out.println("enter second number: ");

b=sc.nextInt();

System.out.println("the addition of "+a+" and " +b+ " is "+(a+b));

System.out.println("enter your choice\n0.Exit\n1.continue\n");

ch=sc.nextInt();

23.Write a Java Program to print this pattern for n lines


1
12
123
1234
1234
123
12
1
Sol:
import java.util.Scanner;

class pattern1

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int i,j,n;

System.out.println("Enter integer :");

n=sc.nextInt();

for(i=0;i<n/2;i++)

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

{
System.out.print(j);

System.out.println();

for(i=n/2;i<n;i++)

for(j=1;j<n-i+1;j++)

System.out.print(j);

System.out.println();

24.Write a Java Program to print half pyramid using **

 *
 **
 ***
 ****
Sol:
import java.util.Scanner;

class halfpyramid

public static void main(String args[])

int m,n,i,j;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();
m=n;

for(i=0;i<n;i++)

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

System.out.print("*");

System.out.println();

25.Write a Java Program to print half pyramid using alphabets

A
BB
CCC
DDDD
EEEEE
Sol:
import java.util.Scanner;

class halfpyramidalpha

public static void main(String args[])

int i,j,n;

char first='A';

System.out.println('E'-'A');

for(i=1;i<='E'-'A'+1;i++)

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

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

first++;

System.out.println();

26.Write a Java Program to Invert half pyramid using *

 ****
 ***
 **
 *
 *
Sol:
import java.util.Scanner;

class patterns2

public static void main(String args[])

int m,n,i,j;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

m=n;

for(i=0;i<n;i++)

for(j=0;j<n-i;j++)

{
System.out.print("*");

System.out.println();

27.Write a Java Program to Invert half pyramid using numbers

12345
1234
123
12
1
Sol:
import java.util.Scanner;

class patterns2

public static void main(String args[])

int m,n,i,j;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

m=n;

for(i=0;i<n;i++)

for(j=0;j<n-i;j++)

System.out.print(j+1);
}

System.out.println();

28.Write a Java Program to print full pyramid using *

*
**
****
******
********
Sol:
import java.util.Scanner;

class fullpyramid

public static void main(String args[])

int m,n,i,j,k;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

m=n;

for(i=0;i<n;i++)

for(j=0;j<n-i;j++)

System.out.print(" ");

for(k=0;k<=i;k++)
{

System.out.print("* ");

System.out.println();

29.Write a Java Program to print pyramid using numbers

1
232
34543
4567654
567898765

Sol:
import java.util.Scanner;

class numpyramid

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int i,j,k,p,n,m,q;

n=sc.nextInt();

q=2*n;

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

m=2*i-1;

for(j=0;j<=q-2*i;j++)

System.out.print(" ");

}
for (k=i;k<=m;k++)

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

for(p=m-1;p>=i;p--)

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

System.out.println();

}}}

30.Write a Java Program to Invert full pyramid using *

 ********
 ******
 ****
 **
 *
Sol:
import java.util.Scanner;

class fullpyramid

public static void main(String args[])

int m,n,i,j,k;

System.out.println("Enter number: ");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

m=n;

for(i=0;i<n;i++)
{

for(j=0;j<=i;j++)

System.out.print(" ");

for(k=0;k<n-i;k++)

System.out.print("* ");

System.out.println();

31.Write a Java Program to Print Floyd's Triangle.

1
23
456
7 8 9 10
Sol:
class Main {

public static void main(String[] args) {

int rows = 4, number = 1;

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

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

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

++number;

}
System.out.println();

32.Write a Java program to check whether a number is palindrome or not


Sol:
import java.util.Scanner;

import java.lang.Math;

class pallindrome

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n;

System.out.println("Enter the palindrome number: ");

n=sc.nextInt();

if(reverse(n)==n){

System.out.println("The number "+n+" is a palindrome");

else

System.out.println("The number "+n+" is not a palindrome");

static int reverse(int n)

int i=0,q,res=0;
while(n!=0)

q=n%10;

n=n/10;

res=res*10+q;

i++;

return res;

33.Write a Java program to print the odd and even values in an array
Sol:

34.Write a Java program to remove the duplicate elements of a given array


and return the new length of the array.
35.Write a Java Program to read the number and check whether it is
divisible by 3 and 5.
36.Write a Java program to print the pascal triangle
37.Write a Java program to print the floyds triangle
Sol:
class Main {

public static void main(String[] args) {

int rows = 4, number = 1;

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

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

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

++number;

}
System.out.println();

38.Java program generates random numbers within the provided range.


39.Write a Java Program to generate the random number between the
ranges.
40.Java program to reverse a number.

sol:

import java.util.Scanner;

import java.lang.Math;

class pallindrome

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n;

System.out.println("Enter the palindrome number: ");

n=sc.nextInt();

if(reverse(n)==n){

System.out.println("The number "+n+" is a palindrome");

}
else

System.out.println("The number "+n+" is not a palindrome");

static int reverse(int n)

int i=0,q,res=0;

while(n!=0)

q=n%10;

n=n/10;

res=res*10+q;

i++;

return res;

41. Write a Java program to create and display unique three-digit


number using 1, 2, 3, 4. Also count how many three-digit
numbers are there.
Sol:
import java.util.Scanner;

class sumoftwonumberisthird {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Scanner sc=new Scanner(System.in);

int a,b,c;

System.out.println("Enter first number: ");

a=sc.nextInt();

System.out.println("Enter Second number: ");

b=sc.nextInt();

System.out.println("Enter third number: ");

c=sc.nextInt();

System.out.println("the sum of "+a+","+b+","+c+ " numbers is equal to third number is


"+sumoftwo(a,b,c));

public static boolean sumoftwo(int a,int b,int c){

return(a+b==c||b+c==a||c+a==b);

42. Write a Java program that accepts an integer (n) and


computes the value of n+nn+nnn.
Sol:
import java.util.Scanner;

public class acceptnnn {

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int n;

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

n=sc.nextInt();

System.out.printf("%d+%d%d+%d%d%d",n,n,n,n,n,n);

43. Write a Java program to display the system time.

44. Write a Java program to calculate the sum of two integers and
return true if the sum is equal to a third integer. 

45. Sample Output:


Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true

46. Write a Java program that accepts three integers from the
user and return true if the second number is greater than first
number and third number is greater than second number.

Sample Output:
Input the first number : 5
Input the second number: 10
Input the third number : 15
The result is: true

You might also like