0% found this document useful (0 votes)
20 views4 pages

Java Assignment

Uploaded by

Shariq Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Java Assignment

Uploaded by

Shariq Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Give the output value of the following program for n = 3. Show the dry run/working.

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


int output,i,k;
j= 1;
output = 0;
while(j <= n) {
k=(j*2)-1;
output = output +k;
j++; } //while
System.out.println(output);
} //main
} //test

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

?1?= , ?2?= ; ?3?= ,?4?= ,?5?=


Rewrite your code here

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.

import Java Util.Scanner, public class Power {

public static void main(String[] args) {

double x; int n;

Scanner in-new Scanner (System.in);

x=in.nextDouble0;

n=in.nextInt();

double result=1.0;

int inverse=0;

if (n<0){

inverse=1;

n=?1?;

if (n!=0){

for (int i=0;?2?; i++)

result=?3?;

} //for

if (inverse==1){

result=?4?;

System.out.println(x+ “power "+n+”is”+result);

//main

//class

(Write ?1?=…etc and also rewrite the program with the correct values for ?1? etc.)
?1?= ,?2?= ,?3?= , ?4?=

Rewrite the code replacing ?1?..... etc:

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

You might also like