0% found this document useful (0 votes)
40 views5 pages

Java Programming Exercises Solutions

This document contains solutions to 10 Java exercises involving functions and control flow. The exercises include writing functions to calculate the average of 3 numbers, sum of odd numbers from 1 to n, greater of two numbers, circumference of a circle, and checking voting eligibility. Other exercises include writing an infinite loop, counting positive/negative/zero numbers input by the user, exponentiation, GCD calculation, and printing the Fibonacci series.

Uploaded by

Art RSquare
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)
40 views5 pages

Java Programming Exercises Solutions

This document contains solutions to 10 Java exercises involving functions and control flow. The exercises include writing functions to calculate the average of 3 numbers, sum of odd numbers from 1 to n, greater of two numbers, circumference of a circle, and checking voting eligibility. Other exercises include writing an infinite loop, counting positive/negative/zero numbers input by the user, exponentiation, GCD calculation, and printing the Fibonacci series.

Uploaded by

Art RSquare
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

Java Exercise 1 Solutions

Java - Introduction to Programming


Exercise 1 SOLUTIONS

1. Enter 3 numbers from the user & make a function to print their average.
//Try to convert it into a function on your own.
        import [Link].*;

public class Solutions {
   public static void main(String args[]) {
      Scanner sc = new Scanner([Link]);
      int a = [Link]();
      int b = [Link]();
      int c = [Link]();

      int average = (a + b + c) / 3;


      [Link](average);
   }  
}

2. Write a function to print the sum of all odd numbers from 1 to n.


import [Link].*;

public class Solutions {
   public static void printSum(int n) {
       int sum = 0;

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


        if(i % 2 != 0) {
            sum = sum + i;
        }
      }

      [Link](sum);
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner([Link]);
      int n = [Link]();
      printSum(n);
   }  
}
Java
3. WriteExercise
a function 1 Solutions
which takes in 2 numbers and returns the greater of those two.
import [Link].*;

public class Solutions {
   public static int getGreater(int a, int b) {
      if(a > b) {
          return a;
      } else {
          return b;
      }
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner([Link]);
      int a = [Link]();
      int b = [Link]();
      [Link](getGreater(a, b));
   }  
}

4. Write a function that takes in the radius as input and returns the circumference of a circle.
import [Link].*;

public class Solutions {
   public static Double getCircumference(Double radius) {
       return 2 * 3.14 * radius;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner([Link]);
      Double r = [Link]();
      [Link](getCircumference(radius));
   }  
}

5. Write a function that takes in age as input and returns if that person is eligible to vote or not.
A person of age > 18 is eligible to vote.
import [Link].*;

public class Solutions {
   public static boolean isElligible(int age) {
       if(age > 18) {
           return true;
       }
       return false;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner([Link]);
Java Exercise 1 Solutions
      int age = [Link]();
      [Link](isElligible(age));
   }  
}

6. Write an infinite loop using do while condition.


import [Link].*;

public class Solutions {
   public static void main(String args[]) {
      do {

      } while(true);
   }  
}

7. Write a program to enter the numbers till the user wants and at the end it should display the
count of positive, negative and zeros entered.
import [Link].*;

public class Solutions {
   public static void main(String args[]) {
       int positive = 0, negative = 0, zeros = 0;
       [Link]("Press 1 to continue & 0 to stop");
       Scanner sc = new Scanner([Link]);
       int input = [Link]();

       while(input == 1) {
           [Link]("Enter your number : ");
           int number = [Link]();
           if(number > 0) {
               positive++;
           } else if(number < 0) {
               negative++;
           } else {
               zeros++;
           }

           [Link]("Press 1 to continue & 0 to stop");


           input = [Link]();
       }

       [Link]("Positives : "+ positive);


       [Link]("Negatives : "+ negative);
       [Link]("Zeros : "+ zeros);
   }  
Java Exercise 1 Solutions
}

8. Two numbers are entered by the user, x and n. Write a function to find the value of one number
raised to the power of another i.e. .
//Try to convert it into a function on your own.

import [Link].*;

public class Solutions {
   public static void main(String args[]) {
       [Link]("Enter x");
       Scanner sc = new Scanner([Link]);
       int x = [Link]();
       [Link]("Enter n");
       int n = [Link]();

       int result = 1;
       //Please see that n is not too large or else result will exceed the size of int
       for(int i=0; i<n; i++) {
           result = result * x;
       }

       [Link]("x to the power n is : "+ result);


   }  
}

9. Write a function that calculates the Greatest Common Divisor of 2 numbers. (BONUS)
import [Link].*;

public class Solutions {
   public static void main(String args[]) {
       Scanner sc = new Scanner([Link]);
       int n1 = [Link]();
       int n2 = [Link]();

       while(n1 != n2) {
           if(n1>n2) {
               n1 = n1 - n2;
           } else {
               n2 = n2 - n1;
           }
       }
       [Link]("GCD is : "+ n2);
   }  
}
JavatoExercise
//Try 1 Solutions
convert it into a function on your own.
10. Write a program to print Fibonacci series of n terms where n is input by user :
0 1 1 2 3 5 8 13 21 .....
In the Fibonacci series, a number is the sum of the previous 2 numbers that came before it.
(BONUS)
import [Link].*;

public class Solutions {
   public static void main(String args[]) {
       Scanner sc = new Scanner([Link]);
       int n = [Link]();
     
       int a = 0, b = 1;
         
       [Link](a+" ");
     
       if(n > 1) {
           //find nth term
           for(int i=2; i<=n; i++) {
               [Link](b+" ");
               //the concept below is called swapping
               int temp = b;
               b = a + b;
               a = temp;
           }

           [Link]();
       }
   }  
}

Common questions

Powered by AI

A Java program that classifies and counts numbers can use a loop in combination with the Scanner class to continuously take user input until a specific termination condition is met. Within the loop, input numbers are classified as positive, negative, or zero, and the respective counters are incremented. The implementation is shown: int positive = 0, negative = 0, zeros = 0; System.out.println("Press 1 to continue & 0 to stop"); Scanner sc = new Scanner(System.in); int input = sc.nextInt(); while(input == 1) { System.out.println("Enter your number : "); int number = sc.nextInt(); if(number > 0) { positive++; } else if(number < 0) { negative++; } else { zeros++; } System.out.println("Press 1 to continue & 0 to stop"); input = sc.nextInt(); } System.out.println("Positives : "+ positive); System.out.println("Negatives : "+ negative); System.out.println("Zeros : "+ zeros).

To calculate the circumference of a circle given its radius in Java, create a function that multiplies the radius by 2 and by the mathematical constant π (approximated as 3.14). This can be done with the formula circumference = 2 * π * radius. The Java implementation is: import java.util.*; public class Solutions { public static Double getCircumference(Double radius) { return 2 * 3.14 * radius; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); Double r = sc.nextDouble(); System.out.println(getCircumference(r)); } } .

To find the GCD of two integers in Java, employ the Euclidean algorithm. This involves iteratively subtracting the smaller number from the larger number until both numbers become equal, which then represents the GCD. The Java code is as follows: import java.util.*; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); int n2 = sc.nextInt(); while(n1 != n2) { if(n1>n2) { n1 = n1 - n2; } else { n2 = n2 - n1; } } System.out.println("GCD is : "+ n2); } } .

To calculate the power of a number in Java without using built-in methods, a function can use a for-loop to repeatedly multiply the base number. This process is repeated exponent number of times. The Java implementation is: import java.util.*; public class Solutions { public static void main(String args[]) { System.out.println("Enter x"); Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println("Enter n"); int n = sc.nextInt(); int result = 1; for(int i=0; i<n; i++) { result *= x; } System.out.println("x to the power n is : "+ result); } } .

To identify and sum all odd numbers up to a given number n in Java, a for-loop can be used to iterate from 1 to n, with a conditional check to verify if a number is odd using the modulo operator. If i % 2 is not equal to zero, the number is odd and added to the sum. The solution is implemented as follows: import java.util.*; public class Solutions { public static void printSum(int n) { int sum = 0; for(int i=1; i<=n; i++) { if(i % 2 != 0) { sum = sum + i; } } System.out.println(sum); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); printSum(n); } } .

To write a Java function that returns the greater of two numbers, the function should use an if-else conditional statement to compare the two numbers. If the first number is greater than the second, the function should return the first number; otherwise, it should return the second. The implementation is: import java.util.*; public class Solutions { public static int getGreater(int a, int b) { if(a > b) { return a; } else { return b; } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(getGreater(a, b)); } } .

A function can be implemented by using the Scanner class to read three integers from the user and then calculating the average by summing the numbers and dividing by three. The Java code snippet for this would be: import java.util.Scanner; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int average = (a + b + c) / 3; System.out.println(average); } }.

A simple voting eligibility checker in Java can be implemented using a boolean function that checks if a given age is greater than 18, returning true if the person is eligible to vote, and false otherwise. The code is as follows: import java.util.*; public class Solutions { public static boolean isElligible(int age) { if(age > 18) { return true; } return false; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int age = sc.nextInt(); System.out.println(isElligible(age)); } } .

To generate the Fibonacci series up to n terms in Java, initialize two variables representing the first two Fibonacci numbers (0 and 1). Then utilize a for-loop to calculate subsequent numbers by summing the last two numbers in the series. This process continues until n terms are calculated. Implementation: import java.util.*; public class Solutions { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = 0, b = 1; System.out.print(a+" "); if(n > 1) { for(int i=2; i<=n; i++) { System.out.print(b+" "); int temp = b; b = a + b; a = temp; } System.out.println(); } } } .

An infinite loop in Java can be implemented using a do-while construct with a condition that always evaluates to true. In such a loop, the block of code will continue to execute indefinitely because the condition never becomes false. Here is an example: import java.util.*; public class Solutions { public static void main(String args[]) { do { } while(true); } } .

You might also like