L12: Methods-I

Q1. Write a program to find the maximum of two integers using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter two integers: ");
    int n1=ip.nextInt();
    int n2=ip.nextInt();
    int result=max(n1,n2);
    System.out.println("The maximum number is: "+result);
  }
  public static int max(int a, int b){
    if(a>b)
      return a;
    else
      return b;
  }
}

Q2. Write a program to find the area of a circle using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter the radius: ");
    double rad=ip.nextDouble();
    double area=findarea(rad);
    System.out.println("The area is: "+area);
  }
  public static double findarea(double r) {
    double ar=Math.PI*r*r;
    return ar;
  }
}

Q3. Write a program to check whether a number is even or odd using a void method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n=ip.nextInt();
    even_odd (n);
  }
  public static void even_odd (int x) {
    if(x%2==0)
      System.out.println("The input number is even");
    else
      System.out.println("The input number is odd");
  }
}

Q4. Write a program to find the sum of digits of a number using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n=ip.nextInt();
    int sum=sum_of_digits(n);
    System.out.println("Sum of digits of input number is: "+sum);
  }
  public static int sum_of_digits(int n) {
    int sum=0;
    while(n>0) {
      int rem=n%10;
      sum=sum+rem;
      n=n/10;
    }
    return sum;
  }
}

Q5. Write a program to reverse the digits of an integer using a method. For example, if the input number is 7648, the program should return 8467.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int n=ip.nextInt();
    int result=reverse(n);
    System.out.println("The reverse of the input integer is: "+result);
  }
  public static int reverse(int n) {
    int rem,rev=0;
    while(n>0) {
      rem=n%10;
      rev=rev*10+rem;
      n=n/10;
    }
    return rev;
  }
}

Q6. Write a program to find the GCD of two numbers using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter two numbers: ");
    int a=ip.nextInt();
    int b=ip.nextInt();
    int result=gcd(a,b);
    System.out.println("The GCD is "+result);
  }
  public static int gcd(int a,int b) {
    int temp;
    while(b>0) {
      temp=a%b;
      a=b;
      b=temp;
    }
    return a;
  }
}

Q7. Write a program to find the factorial of a number using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n=ip.nextInt();
    long result=factorial(n);
    System.out.println("The factorial is: "+result);
  }
    public static long factorial(int n) {
    long fac=1;
    while(n>0) {
      fac=fac*n;
      n--;
    }
    return fac;
  }
}

Q8. Write a program to determine whether a number is prime using a method.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n=ip.nextInt();
    if(isPrime(n))
      System.out.println("The input number is a prime");
    else
      System.out.println("The input number is not a prime");
  }
  public static boolean isPrime(int n) {
    for(int i=2;i<=Math.sqrt(n);i++) {
      if(n%i==0)
        return false;
    }
    return true;
  }
}

Q9. Write a program to print all the prime numbers lees than a given number using the method isPrime written in Q8.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter a number: ");
    int num=ip.nextInt();
    System.out.println("The prime numbers less than "+ num +" are:");
    for(int i=2;i < num; i++){
      if(isPrime(i))
        System.out.print(i+" ");
    }
  }
  public static boolean isPrime(int n) {
    for(int i=2;i<=Math.sqrt(n);i++) {
      if(n%i==0)
        return false;
    }
    return true;
  }
}

Q10. Write a method creditPoints that takes student's mark and returns 4 if it is 90-100, 3 if 80-89, 2 if 70-79, 1 if 60-69 and 0 for less than 60. Incorporate this method into a program that reads mark from the user and displays the credit point.

import java.util.Scanner;
class Test {
  public static void main(String[ ] args) {
    Scanner ip=new Scanner(System.in);
    System.out.print("Enter the mark: ");
    int mark=ip.nextInt();
    System.out.println("The credit point is: "+creditPoints(mark));
  }
  public static int creditPoints(int n) {
    if(n>=90)
      return 4;
    else if(n>=80)
      return 3;
    else if(n>=70)
      return 2;
    else if(n>=60)
      return 1;
    else
      return 0;
  }
}
Note: In this program, we are calling the method inside the println. Insted of it, you can also write
int point= creditPoints(mark);
System.out.println("The credit point is: "+point);