0% found this document useful (0 votes)
17 views

Java Programs 1

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)
17 views

Java Programs 1

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
You are on page 1/ 24

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 java.io.*;

class oddeven

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int n;

System.out.println("Enter a Number ");

n =Integer.parseInt(br.readLine());

if(n%2==0)

System.out.println(n +" is Even Number");

else

System.out.println(n +" is Odd Number");

Output :

Enter a Number

10 is Even Number

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


import java.io.*;

class largest

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int a,b,c;

System.out.println("Enter 3 Numbers");

a =Integer.parseInt(br.readLine());

b =Integer.parseInt(br.readLine());

c =Integer.parseInt(br.readLine());

if(a>b && a>c)

System.out.println(a+" is Largest amoung 3 Numbers");

if(b>a && b>c)

System.out.println(b+" is Largest amoung 3 Numbers");

if(c>a && c>b)

System.out.println(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)

System.out.print(i*-1 +" ");

else

System.out.print(i +" ");

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

import java.io.*;

class fact

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int n,f=1,i=1;

System.out.println("Enter a Number");

n =Integer.parseInt(br.readLine());

do

f=f*i;

i++;

}while(i<=n);

System.out.println(" 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 java.io.*;

class feb

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

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

System.out.println("Enter the number upto Fibonacci series you want");

n =Integer.parseInt(br.readLine());

System.out.println(a);

System.out.println(b);

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

c=a+b;

System.out.println(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 java.io.*;

class Prime

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int count=0,num,i;

System.out.println("Enter a number");

num =Integer.parseInt(br.readLine());

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

if(num%i==0)

count++;

if(count==2)

System.out.println(num +" is a Prime Number");

else

System.out.println(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 java.io.*;

class perfect

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int i,n,sum=0;

System.out.println("Enter a Number");

n =Integer.parseInt(br.readLine());

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

if(n%i==0)

sum=sum+i;

if(sum==n)

System.out.println(n+" is a Perfect Number");

else

System.out.println(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 java.io.*;

class rev

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int r=0,y,n;

System.out.println("Enter a Number");

n =Integer.parseInt(br.readLine());

while(n!=0)

y=n%10;

r=(r*10)+y;

n=n/10;

System.out.println("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 java.io.*;

class pall

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int r=0,y,n,num;

System.out.println("Enter a Number");

n =Integer.parseInt(br.readLine());

num=n;

while(n!=0)

y=n%10;

r=(r*10)+y;

n=n/10;

if(num==r)

System.out.println(num+ " is a Palindrome No");

else

System.out.println(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 java.io.*;

class weekday

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

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

int w;

System.out.println("Enter Weekday 1-7 (1 for Sunday ....");

w =Integer.parseInt(br.readLine());

switch(w)

case 1:

System.out.println("Sunday");

break;

case 2:

System.out.println("Monday");

break;

case 3:

System.out.println("Tuesday");

break;

case 4:

System.out.println("Wednesday");

break;

case 5:

System.out.println("Thursday");
break;

case 6:

System.out.println("Friday");

break;

case 7:

System.out.println("Satuday");

break;

default:

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

Output :

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

Monday
Q11 Wap program to swap two numbers.

import java.util.*;

class SwapTwoNumbers

public static void main(String []s)

int a,b;

Scanner sc=new Scanner(System.in);

System.out.print("Enter value of a: ");

a=sc.nextInt();

System.out.print("Enter value of a: ");

b=sc.nextInt();

System.out.println("Before swapping - a: "+ a +", b: " + b);

int temp;

temp=a;

a=b;

b=temp;

System.out.println("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 java.util.*;

class SwapTwoNumbers

public static void main(String []s)

int a,b;

Scanner sc=new Scanner(System.in);

System.out.print("Enter value of a: ");

a=sc.nextInt();

System.out.print("Enter value of a: ");

b=sc.nextInt();

System.out.println("Before swapping - a: "+ a +", b: " + b);

a=a+b;

b=a-b;

a=a-b;

System.out.println("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 java.util.Scanner;

public class AreaCircle {

public static void main(String[] args) {

double radius;

Scanner sc=new Scanner(System.in);

System.out.print("Enter the Radius of Circle : ");

radius=sc.nextDouble();

double area=3.14*radius*radius;

System.out.print("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 java.util.*;

public class AreaTriangle{

public static void main(String []args)

double base,height,area;

Scanner sc=new Scanner(System.in);

System.out.print("Enter Base Widht: ");

base=sc.nextDouble();

System.out.print("Enter Height: ");

height=sc.nextDouble();

area = (base*height)/2;

System.out.println("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 java.util.*;

public class Even{

public static void main(String []args)

int n=0,i=0;

Scanner X = new Scanner(System.in);

System.out.print("Enter value n : ");

n = X.nextInt();

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

if(i%2==0)

System.out.print(i+" ");

System.out.println();

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(System.in);
System.out.println("Enter a character : ");
char ch=sc.next( ).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)
System.out.println("Entered character "+ch+" is Vowel");
else
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println("Entered character "+ch+" is Consonent");
else
System.out.println("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(System.in);
System.out.print("enter any calendar year :");
y=sc.nextLong();
if(y!=0)
{
a=(y%400==0)?(c=1):((y%100==0)?(c=0):((y%4==0)?(c=1):(c=0)));

if(a==1)
System.out.println(y+" is a leap year");
else
System.out.println(y+" is not a leap year");
}
else
System.out.println("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(System.in);

System.out.println("Enter a number ");

n=sc.nextLong();

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

sum+=n%10;

System.out.println("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(System.in);

System.out.println("enter how many numbers you want sum");


n=sc.nextInt();
System.out.println("enter the "+n+" numbers ");
for(int i=0;i<n;i++)
{
System.out.println("enter number "+(i+1)+":");
a=sc.nextInt();
sum+=a;
}
System.out.println("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(System.in));

System.out.println("Enter a number");

n = Integer.parseInt(in.readLine());

temp=n;

while(n!=0)
{

a=n%10;

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

n=n/10;
}

if(arm==temp)

System.out.println(temp+" is a armstrong number ");

else

System.out.println(temp+" is not a armstrong number ");


}
}

Output :

Enter a Number

153

Number is Armstrong.

You might also like