Java Programs 1
Java Programs 1
Program Description
import java.io.*;
class oddeven
int n;
n =Integer.parseInt(br.readLine());
if(n%2==0)
else
Output :
Enter a Number
10 is Even Number
class largest
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());
Output:
Enter 3 Numbers
10
20
20
1 -4 7 -10 13 -16…………………………-40
class series
int i;
for(i=1;i<=40;i+=3)
if(i%2==0)
else
Ouput :
1 -4 7 -10 13 -16…………………………-40
Q4 : Wap to input a Number and Print its Factorial.
import java.io.*;
class fact
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);
Output :
Enter a Number
import java.io.*;
class feb
int a=0,b=1,c,i,n;
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:
01123
Q6: Wap to input a Number check whether it is Prime or Not.
import java.io.*;
class Prime
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)
else
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
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)
else
Output :
Enter a Number
6 is a Perfect Number
Q8: Wap to input a Number Print it Reverse.
import java.io.*;
class rev
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;
OutPut:
Enter a Number
123
import java.io.*;
class pall
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)
else
}}
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
int w;
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 :
Monday
Q11 Wap program to swap two numbers.
import java.util.*;
class SwapTwoNumbers
int a,b;
a=sc.nextInt();
b=sc.nextInt();
int temp;
temp=a;
a=b;
b=temp;
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
int a,b;
a=sc.nextInt();
b=sc.nextInt();
a=a+b;
b=a-b;
a=a-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;
double radius;
radius=sc.nextDouble();
double area=3.14*radius*radius;
Output:
Enter the Radius of Circle : 12.5
Area of Circle : 490.625
Q:14 Wap to find Area of Triangle.
import java.util.*;
double base,height,area;
base=sc.nextDouble();
height=sc.nextDouble();
area = (base*height)/2;
Output:
import java.util.*;
int n=0,i=0;
n = X.nextInt();
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
long n,sum;
n=sc.nextLong();
sum+=n%10;
class sum
{
public static void main(String arg[])
{
int n,sum=0;
class ArmstrongBuf
{
public static void main(String[] arg) throws IOException
{
int a,arm=0,n,temp;
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)
else
Output :
Enter a Number
153
Number is Armstrong.