0% found this document useful (0 votes)
2 views

Gr-9 - Ch-3 - Loops with Java - Questions

This document is a Grade 9 Java programming assignment focused on loops and control statements. It includes questions about loops, jump statements, and example programs for various tasks such as printing natural numbers, checking for prime numbers, and creating a Rock, Paper, Scissors game. The document provides both theoretical explanations and practical coding exercises.

Uploaded by

shaikabrar0010
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)
2 views

Gr-9 - Ch-3 - Loops with Java - Questions

This document is a Grade 9 Java programming assignment focused on loops and control statements. It includes questions about loops, jump statements, and example programs for various tasks such as printing natural numbers, checking for prime numbers, and creating a Rock, Paper, Scissors game. The document provides both theoretical explanations and practical coding exercises.

Uploaded by

shaikabrar0010
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

Grade - 9

Chapter-3

Loops with Java

ANSWER THE GIVEN BELOW QUESTIONS:

Q1). What is a Loop?

Q2). Define the while loop and explain it using its syntax?

Q3). Draw and label the While-loop diagram.

Q4). What is for loop? Explain for loop using its syntax?

Q5). Define the term Jump Statement.

Q6). What is break statement? Explain it with one example program.

Q7). What is continue statement? Explain it with one example program.

Q8). Write a program to print first 10 natural numbers using a while loop.

Q9). Write a program to print the odd numbers between 1 to 10.

Q10). Write a program to print first 10 natural numbers using a for loop.

Q11). Write a program to find the factorial of a number.


For PRACTICE PROGRAMS: -
1). Write a program to find the sum of the following series: 1 + 4 + 7 + . . . . . . + 28.
Ans).
public class SeriesSum {
public static void main(String[ ] args) {
int start = 1;
int end = 28;
int step = 3;
int sum = findSumOfSeries(start, end, step);
System.out.println("Sum of the series is: " + sum);
}
public static int findSumOfSeries(int start, int end, int step) {
int sum = 0;
for (int i = start; i <= end; i += step) {
sum += i;
}
return sum;
}}

2). Write a program to determine if a number is prime or not.


Ans).
import java.util.Scanner;
public class PrimeChecker {
public static void main(String[ ] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's prime: ");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}}
return true;
} }

3). Create a Rock, Paper, Scissors game in Java where one player will be the computer and
the other player is the user.
Ans).
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[ ] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[ ] choices = {"Rock", "Paper", "Scissors"};
System.out.println("Welcome to Rock, Paper, Scissors game!");
while (true) {
System.out.print("Enter your choice (Rock, Paper, Scissors) or 'exit' to end the game: ");
String userChoice = scanner.nextLine().toLowerCase();
if (userChoice.equals("exit")) {
System.out.println("Game ended. Goodbye!");
break;
}
if (!userChoice.equals("rock") && !userChoice.equals("paper") &&
!userChoice.equals("scissors")) {
System.out.println("Invalid choice. Please enter Rock, Paper, or Scissors.");
continue;
}
int computerIndex = random.nextInt(3);
String computerChoice = choices[computerIndex];
System.out.println("Computer chose: " + computerChoice);

determineWinner(userChoice, computerChoice);
}
scanner.close();
}
public static void determineWinner(String userChoice, String computerChoice)
{
if (userChoice.equals(computerChoice)) {
System.out.println("It's a tie!");
} else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) ||
(userChoice.equals("paper") && computerChoice.equals("rock")) ||
(userChoice.equals("scissors") && computerChoice.equals("paper"))) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
} } }

You might also like