0% found this document useful (0 votes)
38 views8 pages

Chapter 6 Methods Solutions

Uploaded by

kusamrani3319
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)
38 views8 pages

Chapter 6 Methods Solutions

Uploaded by

kusamrani3319
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/ 8

Question7 –

import java.util.Scanner;

public class Maximum


{
static int max(int a, int b, int c)
{
if(a>b && a>c)
return a;
else if(b>a && b>c)
return b;
else
return c;
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers");
int a1, b1, c1;
a1=sc.nextInt();
b1=sc.nextInt();
c1=sc.nextInt();

int d = max(a1,b1,c1);
System.out.println("The maximum of three numbers = "+d);
}

Output –
Enter three numbers
5
6
4
The maximum of three numbers = 6

Question8 –

public class ResultCard


{

static int total(int m, int e, int h, int p, int s)


{
return m+e+h+p+s;
}

static double percentage(int t)


{
double p = (t/500.0)*100;
return p;
}
static char grade(double p)
{
if(p>90)
return 'A';
else if(p>75 && p<=90)
return 'B';
else if(p>60 && p<=75)
return 'C';
else if(p>=40 && p<=60)
return 'D';
else
return 'F';
}

public static void main(String[] args)


{
String name = "Laksh";
int rollno=121;
int standard = 9;
int maths=78, eng=89, hindi=76, punjabi=65, science=87;

int tot = total(maths,eng,hindi,punjabi,science);


double percent = percentage(tot);
char grade = grade(percent);

System.out.println("= = = = = = = = = = RESULT CARD = = = = = = = = = =


= =");
System.out.println("Name: "+name);
System.out.println("Roll Number: "+rollno);
System.out.println("Marks Obtained");
System.out.println("Maths: "+maths);
System.out.println("English: "+eng);
System.out.println("Hindi: "+hindi);
System.out.println("Punjabi: "+punjabi);
System.out.println("Science: "+science);
System.out.println("Total marks obtained: "+tot);
System.out.println("Percentage: "+percent);
System.out.println("Grade: "+grade);

}
}
Output –
= = = = = = = = = = RESULT CARD = = = = = = = = = = = =
Name: Laksh
Roll Number: 121
Marks Obtained
Maths: 78
English: 89
Hindi: 76
Punjabi: 65
Science: 87
Total marks obtained: 395
Percentage: 79.0
Grade: B

Question9 –

public class Arithmetic


{

static int add(int a,int b)


{
return a+b;
}
static int sub(int a,int b)
{
return a-b;
}
static int multi(int a,int b)
{
return a*b;
}
static int division(int a,int b)
{
return a/b;
}
static int modulus(int a,int b)
{
return a%b;
}

public static void main(String args[])


{
int a=6, b=4;
int c;
System.out.println("a="+a);
System.out.println("b="+b);
c=add(a, b);
System.out.println("The sum of two numbers="+c);

c=sub(a, b);
System.out.println("The difference of two numbers="+c);

c=multi(a, b);
System.out.println("The product of two numbers="+c);

c=division(a, b);
System.out.println("The quotient of two numbers="+c);

c=modulus(a, b);
System.out.println("The remainder obtained after dividing two numbers="+c);

Output –
a=6
b=4
The sum of two numbers=10
The difference of two numbers=2
The product of two numbers=24
The quotient of two numbers=1
The remainder obtained after dividing two numbers=2

Question10 –

public class Area {

static double area1(int b,int h)


{
double area = 1.0/2.0*b*h;
return area;
}
static double area2(int a,int b,int c)
{
double d,s,area;
s=(a+b+c)/2.0;
d=s*(s-a)*(s-b)*(s-c);
area=Math.sqrt(d);
return area;
}
public static void main(String[] args)
{
double a;
int b=12,h=5;
int l1=17,l2=8,l3=15;

System.out.println("Base="+b+", height="+h);
a=area1(b, h);
System.out.println("The area of the triangle = "+a);

System.out.println("lengths of sides = "+l1+", "+l2+", "+l3);


a=area2(l1,l2,l3);
System.out.println("The area of the triangle = "+a);

}
Output –
Base=12, height=5
The area of the triangle = 30.0
lengths of sides = 17, 8, 15
The area of the triangle = 60.0

Question11 –

public class Calculate


{

static int square(int s)


{
return s*s;
}
static int rectangle(int l, int b)
{
return l*b;
}
static double circle(int r)
{
double pi=3.14;
return pi*r*r;
}

public static void main(String[] args)


{
double area;

area=square(5); //square with side 5 units


System.out.println("The area of the square = "+area);
area=rectangle(5,6); //rectangle with length 5 and breadth 6 units
System.out.println("The area of the rectangle = "+area);

area=circle(3); //circle with radius 3 units


System.out.println("The area of the circle = "+area);

}
}

Output –
The area of the square = 25.0
The area of the rectangle = 30.0
The area of the circle = 28.259999999999998

Question12 –

import java.util.Scanner;

public class Check


{

static void prime(int n)


{
int i=2;
while(true)
{
if(n%i==0)
break;
i++;
}
if(n==i)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a prime number");
}

static void evenOrOdd(int n)


{
if(n%2==0)
System.out.println(n+" is an even number");
else
System.out.println(n+" is an odd number");
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number = ");
int a = sc.nextInt();

prime(a);
evenOrOdd(a);
}
}

Output –
Enter a number = 31
31 is a prime number
31 is an odd number

Question13 –

import java.util.Scanner;

public class Fibonacci


{

static void fibo(int n)


{
int x=0, y=1, z;

while(x<n)
{
System.out.print(x+" ");
z=x+y;
x=y;
y=z;
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("enter a number to print fibonacci series less than it = ");
int a = sc.nextInt();

fibo(a);
}

}
Output –
enter a number to print fibonacci series less than it = 24
0 1 1 2 3 5 8 13 21

Question14 –

import java.util.Scanner;

public class Average {

double aver(int m, int n, int o, int p, int q)


{
double t = (m+n+o+p+q)/5.0;
return t;
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter 5 numbers:");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();

Average av = new Average();


double t = av.aver(a,b,c,d,e);
System.out.println("The average of 5 numbers: "+t);

}
}

Output –
Enter 5 numbers:
5
8
6
3
9
The average of 5 numbers: 6.2

You might also like