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

لاب3

This document contains solutions to 4 programming lab exercises: 1) A program that inputs 10 integers from the user and prints the largest. It uses variables for a counter, the current number, and the largest found so far. 2) A program that reads a nonnegative integer and calculates and prints its factorial. It initializes a factorial variable and calculates the factorial in a while loop. 3) A program that prints a table of values using a while loop to iterate from 1 to 5 and print the values and their multiples of 10, 100, and 1000. 4) A program that prints a checkerboard pattern using only print statements for asterisks and newlines, checking the row number modulo 2 to alternate spaces and asterisks

Uploaded by

442050215
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)
35 views4 pages

لاب3

This document contains solutions to 4 programming lab exercises: 1) A program that inputs 10 integers from the user and prints the largest. It uses variables for a counter, the current number, and the largest found so far. 2) A program that reads a nonnegative integer and calculates and prints its factorial. It initializes a factorial variable and calculates the factorial in a while loop. 3) A program that prints a table of values using a while loop to iterate from 1 to 5 and print the values and their multiples of 10, 100, and 1000. 4) A program that prints a checkerboard pattern using only print statements for asterisks and newlines, checking the row number modulo 2 to alternate spaces and asterisks

Uploaded by

442050215
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
You are on page 1/ 4

CS2301: Computer Programming 2

Lab #3
Solutions
1. Write a Java application that inputs a series of 10 integers and determines and prints the largest
integer. Your program should use at least the following three variables:
a) counter: A counter to count to 10.
b) number:The integer most recently input by the user.
c) largest: The largest number found so far.

// Program determines and prints the largest of 10 numbers.


import java.util.Scanner;

public class Largest {


// determines the largest of 10 numbers
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// get first number and assign it to variable largest


System.out.print("Enter number: ");
int largest = input.nextInt();

int counter = 1;

// get rest of the numbers and find the largest


while (counter < 10) {
System.out.print("Enter number: ");
int number = input.nextInt();

if (number > largest) {


largest = number;
}

++counter;
}

System.out.printf("Largest number is %d%n", largest);


}
}

Instructor: Dr.Ameerah Alothman 1st semester 1443


CS2301: Computer Programming 2
Lab #3
Solutions
2. The factorial of a nonnegative integer n is written as n!, and is defined as follows: n!=n·(n–1)·(n–
2)·...·1 (for values of n greater than or equal to 1) and n!=1 (for n=0)
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120. Write an application that reads a nonnegative integer
and computes and prints its factorial.

// Program calculates a factorial.


import java.util.Scanner;

public class Factorial {


// calculates the factorial of a number
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int factorial = 1;

System.out.print("Enter a positive Integer: ");


int number = input.nextInt();

System.out.printf("%d! is ", number);

// calculate factorial
while (number > 1) {
factorial *= number;
number--;
}

System.out.println(factorial);
}
}

Instructor: Dr.Ameerah Alothman 1st semester 1443


CS2301: Computer Programming 2
Lab #3
Solutions
3. Write a Java application that uses looping to print the following table of values:

// Program prints a table of values using a while loop.

public class Table {


public static void main(String[] args) {
int n = 1;

System.out.printf("N\t10*N\t100*N\t1000*N%n%n");

while (n <= 5) {
System.out.printf("%d\t%d\t%d\t%d%n",
n, (10 * n), (100 * n), (1000 * n));
++n;
}
}
}

Instructor: Dr.Ameerah Alothman 1st semester 1443


CS2301: Computer Programming 2
Lab #3
Solutions
4. Write an application that uses only the output statements:
System.out.print( "* " );
System.out.print( " " );
System.out.println();
to display the checkerboard pattern that follows. A System.out.println method call with no arguments
causes the program to output a single newline character. [Hint: Repetition statements are required].

// Program prints a checkerboard pattern.


public class Stars {
public static void main(String[] args) {
int row = 1;

while (row <= 8) {


int column = 1;

if (row % 2 == 0) {
System.out.print(" ");
}

while (column <= 8) {


System.out.print("* ");
column++;
}

System.out.println();
row++;
}
}
}

Instructor: Dr.Ameerah Alothman 1st semester 1443

You might also like