Java Da-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

18BCI0002

T.Guna Sekhar

1. Write a Java program to perform basic arithmetic operations of two numbers.

Ans:

import java.util.Scanner;S

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\n0.Exit");

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;
18BCI0002
T.Guna Sekhar

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

}
18BCI0002
T.Guna Sekhar

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


without using third variable.

Ans:

import java.util.Scanner;
18BCI0002
T.Guna Sekhar

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


18BCI0002
T.Guna Sekhar

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

}
18BCI0002
T.Guna Sekhar

3. Write a Java program to perform Multiplication of two numbers without using * operator.

Ans:

import java.util.Scanner;

class mulwithoutoperator

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int a,b,res=0;

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

a=sc.nextInt();

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

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){


18BCI0002
T.Guna Sekhar

for (int i=0;i<b;i++){

a++;

return a;

4. Write a Java program to check the year is leap year or not.

Ans:

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

}
18BCI0002
T.Guna Sekhar

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).

Ans
18BCI0002
T.Guna Sekhar

import java.util.*;

public class table

public static void main(String args[])

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

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

System.out.print(+i+"*"+j+"="+(i*j));

System.out.print("\t");

System.out.println("\n");

6. Write a Java Program to print ASCII Table.


18BCI0002
T.Guna Sekhar

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.

Ans:

import java.util.*;
18BCI0002
T.Guna Sekhar

class sumofdigits

public static void main(String args[])

int sum=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Four digit number");

int no=sc.nextInt();

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

int x=no%10;

sum=sum+x;

no=no/10;

System.out.println("The sum of four digit number is "+sum);

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

Ans:

import java.util.Scanner;

class sumoffirstandlast

public static void main(String args[])

Scanner sc = new Scanner(System.in);


18BCI0002
T.Guna Sekhar

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

}
18BCI0002
T.Guna Sekhar

9. Write a Java program to check whether given number is Armstrong or not.

Ans:

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

}
18BCI0002
T.Guna Sekhar

10. Write a Java program to print Fibonacci Series.

Ans:

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++)

{
18BCI0002
T.Guna Sekhar

n3=n1+n2;

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

n1=n2;

n2=n3;

11. Write a Java program to print Factorial of Number.

Ans:

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


18BCI0002
T.Guna Sekhar

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

Ans:

import java.util.Scanner;

class swapeiththird

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

}
18BCI0002
T.Guna Sekhar

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

Ans:

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.

Ans:

import java.util.Scanner;

class power

public static void main(String args[])

{
18BCI0002
T.Guna Sekhar

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.

Ans:

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;

}
18BCI0002
T.Guna Sekhar

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.

Ans:

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.

Ans:

import java.util.Scanner;
18BCI0002
T.Guna Sekhar

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

}
18BCI0002
T.Guna Sekhar

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

}
18BCI0002
T.Guna Sekhar

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.

Ans:

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.\n605.");

ch=sc.nextInt();

switch(ch)

case 604:

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

break;

case 605:

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

}
18BCI0002
T.Guna Sekhar

break;

default:

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

}}}

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.

Ans:

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

}
18BCI0002
T.Guna Sekhar

20. Write a Java Program to print the sum of the series 1^2 +2^2 +3^2 up to n terms

Ans:

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

}
18BCI0002
T.Guna Sekhar

21. Write a Java Program to print the multiplication table by getting the n from the user.

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

}
18BCI0002
T.Guna Sekhar

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

Ans:

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();
18BCI0002
T.Guna Sekhar

You might also like