Java Da-1
Java Da-1
Java Da-1
T.Guna Sekhar
Ans:
import java.util.Scanner;S
int a,b,c;
a=sc.nextInt();
b=sc.nextInt();
System.out.println();
int ch;
ch=sc.nextInt();
while (ch!=0)
switch(ch)
case 1:
c=a+b;
break;
case 2:
c=a-b;
break;
18BCI0002
T.Guna Sekhar
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
default:
System.out.println();
System.out.println();
ch=sc.nextInt();
}
18BCI0002
T.Guna Sekhar
Ans:
import java.util.Scanner;
18BCI0002
T.Guna Sekhar
int a,b;
a=sc.nextInt();
b=sc.nextInt();
System.out.println();
int ch;
ch=sc.nextInt();
while (ch!=0)
switch(ch)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
System.out.println();
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
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);
a++;
return a;
Ans:
import java.util.Scanner;
class leapyear
int year;
year=sc.nextInt();
if(year%100==0){
if(year%400==0){
else
}
18BCI0002
T.Guna Sekhar
else
Ans
18BCI0002
T.Guna Sekhar
import java.util.*;
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");
Ans:
class ascii
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
int sum=0;
int no=sc.nextInt();
int x=no%10;
sum=sum+x;
no=no/10;
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
int n,s=0;
n=sc.nextInt();
s=s+(n%10);
s=s+(n/1000);
else
}
18BCI0002
T.Guna Sekhar
Ans:
import java.util.Scanner;
class armstrong
int n,r,s=0;
n=sc.nextInt();
int x=n;
while (n!=0)
r=n%10;
n=n/10;
s=s+r*r*r;
if(s==x){
else{
}
18BCI0002
T.Guna Sekhar
Ans:
import java.util.Scanner;
class fibonacci
int i,n,n1=0,n2=1,n3;
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;
Ans:
import java.util.Scanner;
class factorial
int i,n,r=1;
n=sc.nextInt();
for(i=n;i>0;i--)
r=r*i;
12. Write a Java program to swap two numbers using third variable.
Ans:
import java.util.Scanner;
class swapeiththird
int n1,n2,n3;
n1=sc.nextInt();
n2=sc.nextInt();
n3=n1;
n1=n2;
n2=n3;
}
18BCI0002
T.Guna Sekhar
13. Write a Java program to swap two numbers without using third variable.
Ans:
import java.util.Scanner;
class swapwithoutthird
int n1,n2;
n1=sc.nextInt();
n2=sc.nextInt();
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
Ans:
import java.util.Scanner;
class power
{
18BCI0002
T.Guna Sekhar
int base,power;
base=sc.nextInt();
power = sc.nextInt();
double res;
res=Math.pow(base,power);
15. Write a Java program to find sum of all digits between 10 and 50, which are divisible by 3.
Ans:
class sumofdigitsbet10and50
int i,s=0;
for (i=10;i<=50;i++)
if(i%3==0)
s=s+i;
}
18BCI0002
T.Guna Sekhar
}}
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
int i,s=0;
for (i=200;i<=800;i++)
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
int ch,i,n;
ch=sc.nextInt();
while(ch!=0)
n=sc.nextInt();
switch (ch)
case 1:
if(n%3==0)
else
break;
case 2:
if(n%5==0)
}
18BCI0002
T.Guna Sekhar
else
break;
case 3:
else
break;
default:
System.out.println();
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
int n,i,ch;
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
int n,i,s=0;
n=sc.nextInt();
for(i=1;i<n+1;i++)
s=s+i;
}
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
int n,i,s=0;
n=sc.nextInt();
for(i=1;i<n+1;i++)
s=s+i*i;
}
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
int n,c;
n=sc.nextInt();
c=n*i;
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
int a,b,ch;
ch=1;
while(ch!=0)
a=sc.nextInt();
b=sc.nextInt();
ch=sc.nextInt();
18BCI0002
T.Guna Sekhar