Java Exercise 1 Solutions
Java Exercise 1 Solutions
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 java.util.*;
int average = (a + b + c) / 3;
System.out.println(average);
}
}
System.out.println(sum);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
printSum(n);
}
}
3. Write a function which takes in 2 numbers and returns the greater of those
two.
import java.util.*;
4. Write a function that takes in the radius as input and returns the
circumference of a circle.
import java.util.*;
} 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 java.util.*;
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 java.util.*;
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;
}
while(n1 != n2) {
if(n1>n2) {
n1 = n1 - n2;
} else {
n2 = n2 - n1;
}
}
System.out.println("GCD is : "+ n2);
}
}
int a = 0, b = 1;
System.out.print(a+" ");
if(n > 1) {
//find nth term
for(int i=2; i<=n; i++) {
System.out.print(b+" ");
//the concept below is called swapping
int temp = b;
b = a + b;
a = temp;
}
System.out.println();
}
}
}