0% found this document useful (0 votes)
13 views11 pages

Untitled document (8)

The document contains Java programming exercises with solutions for various tasks, including calculating the area of a circle, swapping variable values, determining leap years, generating a marksheet, and checking for prime and Armstrong numbers. Each task is presented with a class and method that implements the required functionality. The exercises cover fundamental programming concepts and provide practical coding examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Untitled document (8)

The document contains Java programming exercises with solutions for various tasks, including calculating the area of a circle, swapping variable values, determining leap years, generating a marksheet, and checking for prime and Armstrong numbers. Each task is presented with a class and method that implements the required functionality. The exercises cover fundamental programming concepts and provide practical coding examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

-:Java Programming Classwork:-

1.​ Write a program to find the area of a circle.


Solution:

public class area{


public static void display(double r)
{
double area = (22/7.0)*r*r;
System.out.println("area= "+area);
}
}

2.​Write a program to swap two variable’s value.


Solution:

public class swap


{
public void display(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
System.out.println("Now a= "+a+" and b= "+b);
}

}
3.​Write a program to find whether the entered year
is a leap year or not.
Solution:

public class leap_year


{
public void display(int y){
if(y%4==0 && y%100!=0 || y%400==0)
System.out.println("The entered year is a leap
year");
else
System.out.println("The entered year is not a leap
year");

}
}

4.​Write a program to prepare a marksheet where


you have to enter the student’s name, class ,
section, Roll and 5 subjects numbers(out of 10) and
find total , average and grade in tabular format.
[Grade division should be :
90-100->A+ , 80-89->A , 70-79->B+ , 60-69->B , 50-59->C ,
40-49->D , <40->Fail]
Solution:

public class Marksheet {


public static void display(String Name,String
Class,int roll ,String sec,int s1,int s2,int s3,int s4,int s5){
int total = s1+s2+s3+s4+s5;
double avg =total/5.0;
String Grade =" ";

if(avg>=90 && avg<=100)


Grade ="A+";
if(avg>=80 && avg<=89)
Grade ="A";
if(avg>=70 && avg<=79)
Grade ="B+";
if(avg>=60 && avg<=69)
Grade ="B";
if(avg>=50 && avg<=59)
Grade ="C";
if(avg>=40 && avg<=49)
Grade ="D";
else
Grade ="Fail";

System.out.println(Name+"-"+roll+"-"+sec+"-"+Class+"-"+to
tal+Grade);

}
}

5.Write a program to find the biggest among three


integer numbers.
Solution:

public class biggest_three


{
public void display(int a,int b, int c)
{
if(a>b && a>c)
System.out.println(a+" is the greatest");
if(b>a && b>c)
System.out.println(b+" is the greatest");
else
System.out.println(c+" is the greatest");
}
}

6. Write a program to find whether the entered


number is a Buzz number or not.
Solution:

public class Buzz


{
public void display(int a)
{
if(a%7==0 || a%10==7)
System.out.println("The entered number is a buzz
number");
else
System.out.println("The entered number is not a
buzz number");
}
}

7. Write a program to find the sum of the digits of the


entered number.
Solution:

public class sumofdigits{


public static void display(int a){
int sum = 0;
int r;
int num =a;

while(a!=0)
{
r= a%10;
sum = sum+r;
a=a/10;

}
System.out.println("The sum of the digits of the
number "+num+" is = "+sum);
}
}

8. Write a program to check whether the entered


number is Armstrong or not.
Solution:

public class armstrong


{
public static void display(int a)
{
int n=a;
int c= 0;
while(n!=0){
n=n/10;
c++;
}
System.out.println("The number "+a+" have "+c+"
digits");

n=a;
int r;
int sum=0;
while(n!=0){
r=n%10;
sum=sum+(int)Math.pow(r,c);
n=n/10;
}
System.out.println("Value of sum is= "+sum);
if(sum==0)
System.out.println("The number"+a+" is a
armstrong number");
else
System.out.println("The number"+a+" is not a
armstrong number");
}
}

9. Write a program tp find whether the entered


number is a prime number or not.
Solution:

public class prime


{
public static void display(int n){
int p=0;
int a= 2;
while(a<=n/2)
{
if(n%a==0){
p=1;
break;
}
else
p=0;
a=a+1;

}
if(p==0)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a prime number");
}
}

10. Write a program to find the factorial of the entered


number.
Solution:

public class factorial


{
public static void display(int a)
{
int n=a;
double fact =1.0;
while(a!=0)
{
fact = fact*a;
a=a-1;
}
System.out.println("The factorial of the number
"+n+"is= "+fact);
}
}

11.Write a program to check whether the entered


number is a plaindrome number or not.
Solution:

public class plalindrome


{
public static void display(int n)
{
int r;
int sum=0;
int a=n;
while(n!=0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(sum==a)
System.out.println("The entered number is a
Plalindrome number");
else
System.out.println("The entered number is not a
Plalindrome number");
}
}

12. Write a program to find whether the entered


number is a krishnamurty number or not.
Solution:
public class krishnamurty
{
public static void display(int a)
{
int r;
int sum =0;
int b=a;
while(a!=0)
{
r=a%10;
int x=r;
int fact= 1;
while(x>1){
fact = fact*x;
x=x-1;
}
sum=sum+fact;
a=a/10;
}
System.out.println("Sum is = "+sum);
if(sum==b)
System.out.println(b+" is a krishnamurty number");
else
System.out.println(b+" is not a krishnamurty
number");
}
}

13. Write a program to find whether the entered


number is a Disarium number or not
Solution:
public class disarium
{
public static void display(int a)
{
int b=a;
int count=0;
while(a!=0)
{
a=a/1;
count++;
}
System.out.println("There are "+count+" digits");
a=b;
int sum=0;
while(a!=0)
{
int r=a%10;
sum=sum+(int)Math.pow(r,count--);
a=a/10;
}
System.out.println("Output is = "+sum);

if(sum==b)
System.out.println(b+"is a Disarium number");
else
System.out.println(b+"is not a Disarium number");
}
}
14.Write a program to find the sum of first 10 natural
numbers.
Solution:

public class Sum


{
public static void display()
{
int a=0;
int sum=0;
while(a!=10)
{
a=a+1;
sum=sum+a;
}
System.out.println("Hence the sum of first 10
natural numbers is = "+sum);
}
}

You might also like