0% found this document useful (0 votes)
11 views7 pages

For Loop SOLUTIONS

Uploaded by

Muhammad Asim
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)
11 views7 pages

For Loop SOLUTIONS

Uploaded by

Muhammad Asim
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/ 7

FINDING THE SMALLEST VALUES FROM A NUMBER OF VALUES INPUT BY THE USER

// Read the number of values to input

// int numberOfValues = input.nextInt();

//

// // Initialize the smallest value

// int smallest = input.nextInt();

//

// // Iterate through the remaining values and update the smallest value if necessary

// for (int i = 1; i < numberOfValues; i++) {

// int currentValue = input.nextInt();

// if (currentValue < smallest) {

// smallest = currentValue;

// }

// }

// Display the smallest value

// System.out.println("The smallest value is " + smallest);


Write a program in which user enters a grade A, B, C, D, or F and program outputs
numeric value of grades as 4, 3, 2, 1, and 0 respectively. Additionally, after entering the
grade user enters the sign of the grade i.e. +, – or X. The + sign increases the numeric
value by 0.3, and - sign decreases it by 0.3 while X means no change in the grade.
Note:
• • There is no F+ or F-
• • However, an A+ has the value 4.0
• • The final output of the program should be the numeric value of the complete grade
• • Example: INPUT: B+ OUPUT: 3.3

System.out.print("Enter the letter grade (A, B, C, D, or F): ");

Scanner input = new Scanner (system.in);

char letterGrade = input.next().charAt(0);

double gpa;

if (letterGrade == 'A') {

gpa = 4.0;

} else if (letterGrade == 'B') {

gpa = 3.0;

} else if (letterGrade == 'C') {

gpa = 2.0;

} else if (letterGrade == 'D') {

gpa = 1.0;

} else if (letterGrade == 'F') {

gpa = 0.0;

} else {

System.out.println("Invalid grade input.");

return }
System.out.print("Enter the sign (+, -, or X): ");

String sign = input.nextLine(); // Take the first character

if (letterGrade == 'A')

if (sign.equals("+") && !(letterGrade == 'A')) {

gpa += 0.3;

// gpa = gpa + 0.3;

else if (sign.equals("-")) {

gpa -= 0.3;

System.out.println("Numeric value of the grade: " + gpa);

scanner.close();
Write a program that takes an integer input from user and print the table of that number upto
10. (Print the table only if the input number is between 3 and 20) [Note: Use for loop]

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer between 3 and 20: ");

int number = scanner.nextInt();

// Check if the input number is within the specified range (3 to 20)

if (number >= 3 && number <= 20) {

System.out.println("Multiplication table for " + number + ":");

// Use a for loop to print the multiplication table up to 10

for (int i = 1; i <= 10; i++) {

int result = number * i;

System.out.println(number + " x " + i + " = " + result);

} else {

System.out.println("Input number is not within the specified range.");

}
Write a program that calculates the product of the odd integers from 1 to 15, then displays the results in
a message dialog [Note: Use for loop]

int product = 1;

// Use a for loop to calculate the product of odd integers from 1 to 15

for (int i = 1; i <= 15; i += 2) {

product *= i;

Print 1 2 3 4 5 4 3 2 1 using a single for loop

for (int i = 1; i <= 5; i++) {

System.out.print(i + " ");

for (int i = 4; i >= 1; i--) {

System.out.print(i + " ");

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

int number = scanner.nextInt();

if (number < 0) {

System.out.println("Factorial is not defined for negative numbers.");

} else {

long factorial = 1; // Use a long to handle larger factorials

for (int i = 1; i <= number; i++) {

factorial *= i;

Take input x-coordinate and y-coordinate value and tell that in which quadrant
does that point (x,y) lies. Also check that the range of X should be within 180 and -
180 and Y should be between 90 and -90

// Check if the x and y values are within the specified ranges

if (x >= -180 && x <= 180 && y >= -90 && y <= 90) {

if (x > 0 && y > 0) {

System.out.println("The point (" + x + ", " + y + ") lies in Quadrant I.");

} else if (x < 0 && y > 0) {

System.out.println("The point (" + x + ", " + y + ") lies in Quadrant II.");

} else if (x < 0 && y < 0) {

System.out.println("The point (" + x + ", " + y + ") lies in Quadrant III.");


} else if (x > 0 && y < 0) {

System.out.println("The point (" + x + ", " + y + ") lies in Quadrant IV.");

} else if (x == 0 && y != 0) {

System.out.println("The point (" + x + ", " + y + ") lies on the y-axis.");

} else if (x != 0 && y == 0) {

System.out.println("The point (" + x + ", " + y + ") lies on the x-axis.");

} else {

System.out.println("The point (" + x + ", " + y + ") is at the origin (0,0).");

} else {

System.out.println("The point is outside the specified range for x and y.");

You might also like