0% found this document useful (0 votes)
58 views27 pages

Java Da-1

The document provides Java code solutions to 17 programming problems. The problems cover basic arithmetic operations, operations without third variables, multiplication without *, checking leap years, printing tables, ASCII tables, calculating digit sums, checking Armstrong numbers, Fibonacci series, factorials, swapping with and without third variables, power calculations, summing digits between ranges divisible by 3, odd numbers between 200-800 divisible by 5, and checking divisibility by 3 and 5.

Uploaded by

viswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views27 pages

Java Da-1

The document provides Java code solutions to 17 programming problems. The problems cover basic arithmetic operations, operations without third variables, multiplication without *, checking leap years, printing tables, ASCII tables, calculating digit sums, checking Armstrong numbers, Fibonacci series, factorials, swapping with and without third variables, power calculations, summing digits between ranges divisible by 3, odd numbers between 200-800 divisible by 5, and checking divisibility by 3 and 5.

Uploaded by

viswa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

18BCI0002

[Link] Sekhar

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

Ans:

import [Link];S

public class arthematic

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int a,b,c;

[Link]("enter first number:");

a=[Link]();

[Link]("enter second number:");

b=[Link]();

[Link]("enter the choice\[Link]\[Link]\[Link]\[Link]\[Link]");

[Link]();

int ch;

ch=[Link]();

while (ch!=0)

switch(ch)

case 1:

c=a+b;

[Link]("Addition of " +a+ " and "+b+ " is " +c);

break;

case 2:

c=a-b;

[Link]("subraction of " +a+ " and "+b+ " is " +c);

break;
18BCI0002
[Link] Sekhar

case 3:

c=a*b;

[Link]("multiplication of " +a+ " and "+b+ " is " +c);

break;

case 4:

c=a/b;

[Link]("division of " +a+ " and "+b+ " is " +c);

break;

default:

[Link]("enter valid choice");

[Link]();

[Link]("enter the choice\[Link]\[Link]\[Link]\[Link]");

[Link]();

ch=[Link]();

}
18BCI0002
[Link] Sekhar

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


without using third variable.

Ans:

import [Link];
18BCI0002
[Link] Sekhar

public class arthematic1

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int a,b;

[Link]("enter first number:");

a=[Link]();

[Link]("enter second number:");

b=[Link]();

[Link]("enter the choice\[Link]\[Link]\[Link]\[Link]");

[Link]();

int ch;

ch=[Link]();

while (ch!=0)

switch(ch)

case 1:

[Link]("addition of " +a+ " and "+b+ " is" +(a+b));

break;

case 2:

[Link]("subraction of " +a+ " and "+b+ " is" +(a-b));

break;

case 3:

[Link]("multiplication of " +a+ " and "+b+ " is" +(a*b));

break;

case 4:

[Link]("division of " +a+ " and "+b+ " is" +(a/b));


18BCI0002
[Link] Sekhar

break;

default:

[Link]("enter valid choice");

[Link]();

[Link]("enter the choice\[Link]\[Link]\[Link]\[Link]");

[Link]();

ch=[Link]();

}
18BCI0002
[Link] Sekhar

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

Ans:

import [Link];

class mulwithoutoperator

public static void main(String args[])

Scanner sc = new Scanner([Link]);

int a,b,res=0;

[Link]("enter first number: ");

a=[Link]();

[Link]("enter second number: ");

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

res=add(res,a);

[Link]("The product of " +a+ " and " +b+ " is "+res);

static int add(int a,int b){


18BCI0002
[Link] 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 [Link];

class leapyear

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int year;

year=[Link]();

if(year%100==0){

if(year%400==0){

[Link]("The year " +year+ " is a leap year");

else

[Link]("The year " +year+ " is not a leap year");

}
18BCI0002
[Link] Sekhar

else if ((year%4==0) && (year%100!=0))

[Link]("The year " +year+ " is a leap year");

else

[Link]("The year " +year+ " is not a leap year");

5. Write a Java program to print multiplication Table (1 to 15).

Ans
18BCI0002
[Link] Sekhar

import [Link].*;

public class table

public static void main(String args[])

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

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

[Link](+i+"*"+j+"="+(i*j));

[Link]("\t");

[Link]("\n");

6. Write a Java Program to print ASCII Table.


18BCI0002
[Link] Sekhar

Ans:

class ascii

public static void main(String args[])

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

char ch=(char)i;

[Link](i+"\t"+ch);

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

Ans:

import [Link].*;
18BCI0002
[Link] Sekhar

class sumofdigits

public static void main(String args[])

int sum=0;

Scanner sc=new Scanner([Link]);

[Link]("Enter the Four digit number");

int no=[Link]();

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

int x=no%10;

sum=sum+x;

no=no/10;

[Link]("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 [Link];

class sumoffirstandlast

public static void main(String args[])

Scanner sc = new Scanner([Link]);


18BCI0002
[Link] Sekhar

int n,s=0;

[Link]("Enter four digit number: ");

n=[Link]();

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

s=s+(n%10);

s=s+(n/1000);

[Link]("the sum is :" +s);

else

[Link]("enter the four digited number");

}
18BCI0002
[Link] Sekhar

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

Ans:

import [Link];

class armstrong

public static void main(String args[])

Scanner sc= new Scanner([Link]);

int n,r,s=0;

[Link]("Enter the number: ");

n=[Link]();

int x=n;

while (n!=0)

r=n%10;

n=n/10;

s=s+r*r*r;

if(s==x){

[Link]("The number "+x+" is Armstrong number");

else{

[Link]("it is not an armstrong number");

}
18BCI0002
[Link] Sekhar

10. Write a Java program to print Fibonacci Series.

Ans:

import [Link];

class fibonacci

public static void main(String args[])

Scanner sc = new Scanner([Link]);

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

[Link]("Enter integer :");

n=[Link]();

if(n==1)

[Link](0);

else if(n==2){

[Link](0+","+1);

else if(n>2)

[Link](0+","+1);

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

{
18BCI0002
[Link] Sekhar

n3=n1+n2;

[Link](","+n3);

n1=n2;

n2=n3;

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

Ans:

import [Link];

class factorial

public static void main(String args[])

Scanner sc = new Scanner([Link]);

int i,n,r=1;

[Link]("Enter integer :");

n=[Link]();

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

r=r*i;

[Link]("The factorial of "+n+" is "+r);


18BCI0002
[Link] Sekhar

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

Ans:

import [Link];

class swapeiththird

public static void main(String args[])

Scanner sc = new Scanner([Link]);

int n1,n2,n3;

n1=[Link]();

n2=[Link]();

[Link]("before swaping: "+ n1 +" " +n2);

n3=n1;

n1=n2;

n2=n3;

[Link]("After swaping: "+ n1 +" " +n2);

}
18BCI0002
[Link] Sekhar

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

Ans:

import [Link];

class swapwithoutthird

public static void main(String args[])

Scanner sc = new Scanner([Link]);

int n1,n2;

n1=[Link]();

n2=[Link]();

[Link]("before swaping: "+ n1 +" " +n2);

n1=n1+n2;

n2=n1-n2;

n1=n1-n2;

[Link]("After swaping: "+ n1 +" " +n2);

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

Ans:

import [Link];

class power

public static void main(String args[])

{
18BCI0002
[Link] Sekhar

Scanner sc=new Scanner([Link]);

int base,power;

[Link]("enter base: ");

base=[Link]();

[Link]("enter power: ");

power = [Link]();

double res;

res=[Link](base,power);

[Link]("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
[Link] Sekhar

[Link]("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;

[Link]("The odd numbers from 200 to 800 divisible by 5 are: ");

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

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

[Link](i+",");

17. Write a Java Program to read the number and check whether it is divisible by 3 and 5.

Ans:

import [Link];
18BCI0002
[Link] Sekhar

class divisibleby3and5

public static void main(String args[])

int ch,i,n;

Scanner sc=new Scanner([Link]);

[Link]("enter choice\[Link]\[Link] by 3\[Link] by 5\[Link] by 3 and 5");

ch=[Link]();

while(ch!=0)

[Link]("Enter number: ");

n=[Link]();

switch (ch)

case 1:

if(n%3==0)

[Link](n+" is divisible by 3");

else

[Link](n+" is not divisible by 3");

break;

case 2:

if(n%5==0)

[Link](n+" is divisible by 5");

}
18BCI0002
[Link] Sekhar

else

[Link](n+" is not divisible by 5");

break;

case 3:

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

[Link](n+" is divisible by 3 and 5");

else

[Link](n+" is not divisible by 3 and 5");

break;

default:

[Link]("enter valid choice");

[Link]();

[Link]("enter choice\[Link]\[Link] by 3\[Link] by 5\[Link] by 3 and 5");

ch=[Link]();

}
18BCI0002
[Link] 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 [Link];

class subnamebasedonroom

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int n,i,ch;

[Link]("enter your room number\[Link]\n604.\n605.");

ch=[Link]();

switch(ch)

case 604:

[Link]("java programming");

break;

case 605:

[Link]("python programming");

}
18BCI0002
[Link] Sekhar

break;

default:

[Link]("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 [Link];

class sumofn

public static void main(String args[])

int n,i,s=0;

[Link]("Enter number: ");

Scanner sc=new Scanner([Link]);

n=[Link]();

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

s=s+i;

[Link]("the sum of "+n+" integers is : "+s);

}
18BCI0002
[Link] 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 [Link];

class sumofn

public static void main(String args[])

int n,i,s=0;

[Link]("Enter number: ");

Scanner sc=new Scanner([Link]);

n=[Link]();

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

s=s+i*i;

[Link]("the sum of "+n+" integers is : "+s);

}
18BCI0002
[Link] Sekhar

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

import [Link];

class table

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int n,c;

[Link]("Enter the integer u want to print table");

n=[Link]();

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

c=n*i;

[Link](n + " * " +i+ "=" +c);

[Link]();

}
18BCI0002
[Link] Sekhar

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

Ans:

import [Link];

class addingnumbers

public static void main(String args[])

Scanner sc=new Scanner([Link]);

int a,b,ch;

ch=1;

while(ch!=0)

[Link]("enter first number: ");

a=[Link]();

[Link]("enter second number: ");

b=[Link]();

[Link]("the addition of "+a+" and " +b+ " is "+(a+b));

[Link]("enter your choice\[Link]\[Link]\n");

ch=[Link]();
18BCI0002
[Link] Sekhar

You might also like