Java Assignment
Java Assignment
2) The following Java program takes two inputs a and b and finds the greatest common
divisor. There are places left with ?1?, ?2?, ?3?, ?4? & ?5? which must be replaced by
java statements, expressions so that the program works correctly.
Write those 5 missing statements/expressions and also rewrite the complete code in the
given box.
import java.util.Scanner;
public class xyz {
public static void main (String[] args) {
int r;
Scanner in = new Scanner (system.in);
int a= in.nextint(); // input from user a
int b= in.nextInt(); // input from user b
int Gcd;
while(?1?){
r=?2?;
b=?3?;
a=?4?;
} //while
if (a==0) {
?5?;}
else{
Gcd=-1;
System.out.println("Gcd:"+Gcd);}
} //main
} //class
3) Using the conditional operator alone(e.g (a>b)? 3:2) write a statement that will check
yyyy (integer) for leap year and return True if leap year and False if not.
4) Write a program that takes N as a user input and prints a (2N+ 1)-by-(2N+1) lines like the
one below (N=3):
5) The following program takes two inputs x and n from user and computes x raised to the power
of n. Here x is the base value and n is the power. Five place in the code are marked by ?1?, ?
2?, ?3? and ?4?. Replace these with expressions or statements so that the program works
correctly.
double x; int n;
x=in.nextDouble0;
n=in.nextInt();
double result=1.0;
int inverse=0;
if (n<0){
inverse=1;
n=?1?;
if (n!=0){
result=?3?;
} //for
if (inverse==1){
result=?4?;
//main
//class
(Write ?1?=…etc and also rewrite the program with the correct values for ?1? etc.)
?1?= ,?2?= ,?3?= , ?4?=
6) Write a program that takes N as a user input and prints N lines like the one below (N=4). Each
row has row number of elements (3rd row has 3 numbers, 4th has 4 numbers etc) and its a
running sequence starting from 1. Each cell is one space.
1
2 3
4 5 6
7 8 9 10