17 - Solutions For Practice Qs
17 - Solutions For Practice Qs
Solution 2:
import java.util.Scanner;
public class Solution {
publicstaticvoidmain(String[]args) {
Scanner sc = new Scanner(System.in);
int number;
int choice;
int evenSum
= 0; int
oddSum = 0;
do{
System.out.print("Enter the number ");
number = sc.nextInt();
if(number%2==0) {
evenSum += number;
} else {
oddSum += number;
}
} while(choice==1);
for(inti=1;i<=num;i++) {
fact *= i;
}
System.out.println("Factorial: "+fact);
}
}
Solution 4:
import java.util.*;
class MultiplicationTable {
public static void printMultiplicationTable(int number){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number:");
int n = sc.nextInt();
for(inti=1;i<=10;i++) {
System.out.println(n+" * "+i+" ="+n*i);
}
}
publicstaticvoidmain(Strings[]) {
printMultiplicationTable(5);
}
}
Solution 5:
Scope of variable is referred to the part of the program where the variable can be used.
In this program variable i is declared in the for loop. So scope of variable i is limited to the for loop
only that is between { and } of the for loop. There is a display statement after the for loop where
variableiis used which meansiis used outof scope. This leads to compilation errors.
The program given will not run and give an error instead. To correct the program, the variable i
needs to be declared outside the for loop.