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);
}
}
3. Write a function 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]);
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);
}
}
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);
}
}
//Try to 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]();
}
}
}