1.
Write a Java program to perform basic arithmetic operations of two
numbers.
SOL:
import [Link];
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]();
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;
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]();
2. Write a Java program to perform operation (Addition, Subtraction,
Multiplication, Division) without using third variable.
Sol:
import [Link];
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));
break;
default:
[Link]("enter valid choice");
[Link]();
[Link]("enter the choice\[Link]\[Link]\[Link]\[Link]");
[Link]();
ch=[Link]();
3. Write a Java program to perform Multiplication of two numbers
without using * operator.
import [Link];
class mulwithoutoperator
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int a,b,res=0;
a=[Link]();
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){
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 [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");
}
}
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).
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]();
}
}
}
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;
[Link](i+"\t"+ch);
7. Write a Java program to Calculate and Display the sum of 4 digits
number.
import [Link];
class sumofdigits
public static void main(String args[])
int n,i;
Scanner sc=new Scanner([Link]);
[Link]("enter the number");
n=[Link]();
int x,sum1=0,r;
x=n;
while(x!=0)
r=x%10;
x=x/10;
sum1+=r;
[Link]("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 [Link];
class sumoffirstandlast
{
public static void main(String args[])
Scanner sc = new Scanner([Link]);
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");
}
9. Write a Java program to check whether given number is Armstrong or
not.
Sol :
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");
}
10. Write a Java program to print Fibonacci Series.
Sol:
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++)
n3=n1+n2;
[Link](","+n3);
n1=n2;
n2=n3;
}
11. Write a Java program to print Factorial of Number
Sol :
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);
}
12. Write a Java program to swap two numbers using third variable.
Sol:
import [Link];
class swapwithoutthird
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);
13. Write a Java program to swap two numbers without using third
variable.
Sol:
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.
Sol:
import [Link];
class power
public static void main(String args[])
{
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);
}}
[Link] 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;
[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.
Sol:
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+",");
}
}
[Link] a Java Program to read the number and check whether it is
divisible by 3 and 5.
Sol:
import [Link];
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");
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]();
[Link] 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 [Link];
class subnamebasedonroom
public static void main(String args[])
Scanner sc=new Scanner([Link]);
int n,i,ch;
[Link]("enter your room number\[Link]\[Link] programming\[Link]
programming");
ch=[Link]();
while(ch!=0)
switch(ch)
case 604:
[Link]("java programming");
break;
case 605:
[Link]("python programming");
break;
default:
[Link]("Invalid input");
[Link]();
[Link]("enter your room number\[Link]\[Link] programming\[Link]
programming");
ch=[Link]();
}}}
[Link] 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 [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);
2 2 2
[Link] a Java Program to print the sum of the series 1 +2 +3 up to n
terms
Sol:
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);
}
[Link] a Java Program to print the multiplication table by getting the n
from the user.
Sol:
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]();
}
}
}
[Link] a Java Program to provide the option of adding two numbers to
the user until the user wants to exit.
Sol:
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]();
[Link] a Java Program to print this pattern for n lines
1
12
123
1234
1234
123
12
1
Sol:
import [Link];
class pattern1
public static void main(String args[])
Scanner sc = new Scanner([Link]);
int i,j,n;
[Link]("Enter integer :");
n=[Link]();
for(i=0;i<n/2;i++)
for (j=1;j<=i+1;j++)
{
[Link](j);
[Link]();
for(i=n/2;i<n;i++)
for(j=1;j<n-i+1;j++)
[Link](j);
[Link]();
[Link] a Java Program to print half pyramid using **
*
**
***
****
Sol:
import [Link];
class halfpyramid
public static void main(String args[])
int m,n,i,j;
[Link]("Enter number: ");
Scanner sc=new Scanner([Link]);
n=[Link]();
m=n;
for(i=0;i<n;i++)
for(j=0;j<i+1;j++)
[Link]("*");
[Link]();
[Link] a Java Program to print half pyramid using alphabets
A
BB
CCC
DDDD
EEEEE
Sol:
import [Link];
class halfpyramidalpha
public static void main(String args[])
int i,j,n;
char first='A';
[Link]('E'-'A');
for(i=1;i<='E'-'A'+1;i++)
for(j=1;j<=i;j++)
{
[Link](first+" ");
first++;
[Link]();
[Link] a Java Program to Invert half pyramid using *
****
***
**
*
*
Sol:
import [Link];
class patterns2
public static void main(String args[])
int m,n,i,j;
[Link]("Enter number: ");
Scanner sc=new Scanner([Link]);
n=[Link]();
m=n;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
{
[Link]("*");
[Link]();
[Link] a Java Program to Invert half pyramid using numbers
12345
1234
123
12
1
Sol:
import [Link];
class patterns2
public static void main(String args[])
int m,n,i,j;
[Link]("Enter number: ");
Scanner sc=new Scanner([Link]);
n=[Link]();
m=n;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
[Link](j+1);
}
[Link]();
[Link] a Java Program to print full pyramid using *
*
**
****
******
********
Sol:
import [Link];
class fullpyramid
public static void main(String args[])
int m,n,i,j,k;
[Link]("Enter number: ");
Scanner sc=new Scanner([Link]);
n=[Link]();
m=n;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
[Link](" ");
for(k=0;k<=i;k++)
{
[Link]("* ");
[Link]();
[Link] a Java Program to print pyramid using numbers
1
232
34543
4567654
567898765
Sol:
import [Link];
class numpyramid
public static void main(String args[])
Scanner sc=new Scanner([Link]);
int i,j,k,p,n,m,q;
n=[Link]();
q=2*n;
for(i=1;i<=n;i++)
m=2*i-1;
for(j=0;j<=q-2*i;j++)
[Link](" ");
}
for (k=i;k<=m;k++)
[Link](k+" ");
for(p=m-1;p>=i;p--)
[Link](p+" ");
[Link]();
}}}
[Link] a Java Program to Invert full pyramid using *
********
******
****
**
*
Sol:
import [Link];
class fullpyramid
public static void main(String args[])
int m,n,i,j,k;
[Link]("Enter number: ");
Scanner sc=new Scanner([Link]);
n=[Link]();
m=n;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
[Link](" ");
for(k=0;k<n-i;k++)
[Link]("* ");
[Link]();
[Link] 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++) {
[Link](number + " ");
++number;
}
[Link]();
[Link] a Java program to check whether a number is palindrome or not
Sol:
import [Link];
import [Link];
class pallindrome
public static void main(String args[])
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter the palindrome number: ");
n=[Link]();
if(reverse(n)==n){
[Link]("The number "+n+" is a palindrome");
else
[Link]("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;
[Link] a Java program to print the odd and even values in an array
Sol:
[Link] a Java program to remove the duplicate elements of a given array
and return the new length of the array.
[Link] a Java Program to read the number and check whether it is
divisible by 3 and 5.
[Link] a Java program to print the pascal triangle
[Link] 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++) {
[Link](number + " ");
++number;
}
[Link]();
[Link] program generates random numbers within the provided range.
[Link] a Java Program to generate the random number between the
ranges.
[Link] program to reverse a number.
sol:
import [Link];
import [Link];
class pallindrome
public static void main(String args[])
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter the palindrome number: ");
n=[Link]();
if(reverse(n)==n){
[Link]("The number "+n+" is a palindrome");
}
else
[Link]("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 [Link];
class sumoftwonumberisthird {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner([Link]);
int a,b,c;
[Link]("Enter first number: ");
a=[Link]();
[Link]("Enter Second number: ");
b=[Link]();
[Link]("Enter third number: ");
c=[Link]();
[Link]("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 [Link];
public class acceptnnn {
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
int n;
[Link]("enter number");
n=[Link]();
[Link]("%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