0% found this document useful (0 votes)
30 views24 pages

Java Programming Exercises

Uploaded by

parasharaarna2
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)
30 views24 pages

Java Programming Exercises

Uploaded by

parasharaarna2
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

S. No.

Program Description

1 Wap to input a Number and check whether a Number is Odd or Even

2 Wap to input 3 Numbers and Print Largest Number

3 Wap to Print the 1 -4 7 -10 13 -16………………-40

4 Wap to input a Number and Print its Factorial.

5 Wap to Print Fibonacci series upto N Numbers.

6 Wap to input a Number check whether it is Prime or Not.

7 Wap to input a Number and check whether it is Perfect or Not.

8 Wap to input a Number Print it Reverse

9 Wap to input a Number and check Whether it is Palindrome or Not.

10 Wap to input a Number and print its corresponding Weekday .

11 Q11 Wap program to swap two numbers

Q12: Wap program to swap 2 numbers without using third variable.


12

Q:13 Wap to to Calculate Area of a Circle.


13

14 Q:14 Wap to find Area of Triangle

16 Q:16 Wap to Print Whether Alphabet is Vowel Or Consonant

Q:17 Wap to To Check Leap Year Or Not


17

Q:18 Wap to input a numbe and print sum of Digits of a Number.


18

Q:19 Wap to print Sum of N Numbers.


19
Q:20 Wap to check whether Number is Armstrong or Not.
20
Q1 : Wap to input a Number and check whether a Number is Odd or Even.

import [Link].*;

class oddeven

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int n;

[Link]("Enter a Number ");

n =[Link]([Link]());

if(n%2==0)

[Link](n +" is Even Number");

else

[Link](n +" is Odd Number");

Output :

Enter a Number

10 is Even Number

Q2 : Wap to input 3 Numbers and Print Largest Number .


import [Link].*;

class largest

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int a,b,c;

[Link]("Enter 3 Numbers");

a =[Link]([Link]());

b =[Link]([Link]());

c =[Link]([Link]());

if(a>b && a>c)

[Link](a+" is Largest amoung 3 Numbers");

if(b>a && b>c)

[Link](b+" is Largest amoung 3 Numbers");

if(c>a && c>b)

[Link](c+" is Largest amoung 3 Numbers");

Output:
Enter 3 Numbers

10

20

20

30 is Largest amoung Numbers


Q3 : Wap to Print the following Series.

1 -4 7 -10 13 -16…………………………-40

class series

public static void main(String args[])

int i;

for(i=1;i<=40;i+=3)

if(i%2==0)

[Link](i*-1 +" ");

else

[Link](i +" ");

Ouput :
1 -4 7 -10 13 -16…………………………-40
Q4 : Wap to input a Number and Print its Factorial.

import [Link].*;

class fact

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int n,f=1,i=1;

[Link]("Enter a Number");

n =[Link]([Link]());

do

f=f*i;

i++;

}while(i<=n);

[Link](" Factorial of a Number"+n +" is " + f);

Output :
Enter a Number

Factorial of a Number 5 is 120


Q5: Wap to Print Fibonacci series upto N Numbers.

import [Link].*;

class feb

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int a=0,b=1,c,i,n;

[Link]("Enter the number upto Fibonacci series you want");

n =[Link]([Link]());

[Link](a);

[Link](b);

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

c=a+b;

[Link](c);

a=b;

b=c;

}}}

Output:

Enter the number upto Fibonacci series you want

01123
Q6: Wap to input a Number check whether it is Prime or Not.

import [Link].*;

class Prime

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int count=0,num,i;

[Link]("Enter a number");

num =[Link]([Link]());

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

if(num%i==0)

count++;

if(count==2)

[Link](num +" is a Prime Number");

else

[Link](num +" is not a Prime Number");

Output :

Enter a number

7 is A Prime Number
Q7: Wap to input a Number and check whether it is Perfect or Not.

import [Link].*;

class perfect

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int i,n,sum=0;

[Link]("Enter a Number");

n =[Link]([Link]());

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

if(n%i==0)

sum=sum+i;

if(sum==n)

[Link](n+" is a Perfect Number");

else

[Link](n+" is not a Perfect Number");

Output :

Enter a Number

6 is a Perfect Number
Q8: Wap to input a Number Print it Reverse.

import [Link].*;

class rev

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int r=0,y,n;

[Link]("Enter a Number");

n =[Link]([Link]());

while(n!=0)

y=n%10;

r=(r*10)+y;

n=n/10;

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

OutPut:

Enter a Number

123

Reverse of 123 is 321


Q9: Wap to input a Number and check Whether it is Palindrome or Not.

import [Link].*;

class pall

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int r=0,y,n,num;

[Link]("Enter a Number");

n =[Link]([Link]());

num=n;

while(n!=0)

y=n%10;

r=(r*10)+y;

n=n/10;

if(num==r)

[Link](num+ " is a Palindrome No");

else

[Link](num+ " is Not a Palindrome No");

}}

Output:

Enter a Number

121

121 is a Pallindrome No
Q10 : Wap to input a Number and print its corresponding Weekday .

import [Link].*;

class weekday

public static void main(String args[]) throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader([Link]));

int w;

[Link]("Enter Weekday 1-7 (1 for Sunday ....");

w =[Link]([Link]());

switch(w)

case 1:

[Link]("Sunday");

break;

case 2:

[Link]("Monday");

break;

case 3:

[Link]("Tuesday");

break;

case 4:

[Link]("Wednesday");

break;

case 5:

[Link]("Thursday");
break;

case 6:

[Link]("Friday");

break;

case 7:

[Link]("Satuday");

break;

default:

[Link]("Invalid days");

Output :

Enter Weekday 1-7 (1 for Sunday ...

Monday
Q11 Wap program to swap two numbers.

import [Link].*;

class SwapTwoNumbers

public static void main(String []s)

int a,b;

Scanner sc=new Scanner([Link]);

[Link]("Enter value of a: ");

a=[Link]();

[Link]("Enter value of a: ");

b=[Link]();

[Link]("Before swapping - a: "+ a +", b: " + b);

int temp;

temp=a;

a=b;

b=temp;

[Link]("After swapping - a: "+ a +", b: " + b);

Enter value of a: 10
Enter value of a: 20
Before swapping - a: 10, b: 20
After swapping - a: 20, b: 10
Q12: Wap program to swap two numbers without using third variable

import [Link].*;

class SwapTwoNumbers

public static void main(String []s)

int a,b;

Scanner sc=new Scanner([Link]);

[Link]("Enter value of a: ");

a=[Link]();

[Link]("Enter value of a: ");

b=[Link]();

[Link]("Before swapping - a: "+ a +", b: " + b);

a=a+b;

b=a-b;

a=a-b;

[Link]("After swapping - a: "+ a +", b: " + b);

Output:
Enter value of a: 10
Enter value of a: 20
Before swapping - a: 10, b: 20
After swapping - a: 20, b: 10
Q:13 Wap to to Calculate Area of a Circle.

import [Link];

public class AreaCircle {

public static void main(String[] args) {

double radius;

Scanner sc=new Scanner([Link]);

[Link]("Enter the Radius of Circle : ");

radius=[Link]();

double area=3.14*radius*radius;

[Link]("Area of Circle : "+area);

Output:
Enter the Radius of Circle : 12.5
Area of Circle : 490.625
Q:14 Wap to find Area of Triangle.

import [Link].*;

public class AreaTriangle{

public static void main(String []args)

double base,height,area;

Scanner sc=new Scanner([Link]);

[Link]("Enter Base Widht: ");

base=[Link]();

[Link]("Enter Height: ");

height=[Link]();

area = (base*height)/2;

[Link]("Area of Triangle: " + area);

Output:

Enter Base Widht: 2.3


Enter Height: 32.2
Area of Triangle: 37.03
Q:15 Wap to print EVEN numbers from 1 to N.

import [Link].*;

public class Even{

public static void main(String []args)

int n=0,i=0;

Scanner X = new Scanner([Link]);

[Link]("Enter value n : ");

n = [Link]();

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

if(i%2==0)

[Link](i+" ");

[Link]();

Output:
Enter value n : 10
2 4 6 8 10
Q:16 Wap to Print Whether the given Alphabet is Vowel Or Consonant
class Char
{
public static void main(String[ ] arg)
{
int i=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter a character : ");
char ch=[Link]( ).charAt(0);

switch(ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :i++;
}
if(i==1)
[Link]("Entered character "+ch+" is Vowel");
else
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
[Link]("Entered character "+ch+" is Consonent");
else
[Link]("Not an alphabet");
}
}
Output:
Q:17 Wap to To Check Leap Year Or Not

class Leapyear
{
public static void main(String arg[])
{
long a,y,c;
Scanner sc=new Scanner([Link]);
[Link]("enter any calendar year :");
y=[Link]();
if(y!=0)
{
a=(y%400==0)?(c=1):((y%100==0)?(c=0):((y%4==0)?(c=1):(c=0)));

if(a==1)
[Link](y+" is a leap year");
else
[Link](y+" is not a leap year");
}
else
[Link]("year zero does not exist ");
}
}
Q:18 Wap to input a numbe and print sum of Digits of a Number.

class SumOfDigits

public static void main(String arg[])

long n,sum;

Scanner sc=new Scanner([Link]);

[Link]("Enter a number ");

n=[Link]();

for(sum=0 ;n!=0 ;n/=10)

sum+=n%10;

[Link]("Sum of digits of a number is "+sum);


}
}
Q:19 Wap to print Sum of N Numbers.

class sum
{
public static void main(String arg[])
{
int n,sum=0;

Scanner sc=new Scanner([Link]);

[Link]("enter how many numbers you want sum");


n=[Link]();
[Link]("enter the "+n+" numbers ");
for(int i=0;i<n;i++)
{
[Link]("enter number "+(i+1)+":");
a=[Link]();
sum+=a;
}
[Link]("sum of "+n+" numbers is ="+sum);
}
}
Q:20 Wap to check whether Number is Armstrong or Not.

class ArmstrongBuf
{
public static void main(String[] arg) throws IOException
{
int a,arm=0,n,temp;

BufferedReader in = new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter a number");

n = [Link]([Link]());

temp=n;

while(n!=0)
{

a=n%10;

arm=arm+(a*a*a);

n=n/10;
}

if(arm==temp)

[Link](temp+" is a armstrong number ");

else

[Link](temp+" is not a armstrong number ");


}
}

Output :

Enter a Number

153

Number is Armstrong.

You might also like