City Montessori School ,RDSO Campus
JAVA PROGRAMS ( class IX)
Q1 program calculates the sum of the digits of a given integer using a while loop.
Ans import [Link];
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int sum = 0;
int temp = number; // Store the original number
while (temp > 0) {
int digit = temp % 10; // Get the last digit
sum += digit; // Add to sum
temp /= 10; // Remove the last digit
}
[Link]("Sum of digits of " + number + " is: " + sum);
[Link]();
}
}
Q2 program calculates the factorial of a given number using a while loop.
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a non-negative number: ");
int number = [Link]();
long factorial = 1; // Use long for larger factorials
int i = 1;
if (number < 0) {
[Link]("Factorial is not defined for negative numbers.");
} else {
while (i <= number) {
factorial *= i; // Multiply factorial by current i
i++;
}
[Link]("Factorial of " + number + " is: " + factorial);
}
[Link]();
}
}
Q3 What is the output of following lines of code?
int datacount = 1;
while(datacount <= 6) {
if(datacount % 2 == 2)
[Link] ("First Set");
else if(datacount % 3 == 0)
[Link] ("Second Set");
else
[Link] ("Third Set");
datacount++; }
Ans
Third Set
Third Set
Second Set
Third Set
Third Set
Second Set
Give the output and show the dry run.
public static void abc()
{
int x=1, i=2;
do
{
x*=i;
}while(++i<=5);
[Link](x);
}
Ans 120
Q5 The following program is supposed to check the given number is prime or not. Some part of
the program is replaced by ______, with the numbering 1 to 5, fill this part so that program
works correctly.
class checkPrime {
public static void main(String args[]) {
int i, f=0;
int n = [Link](args[0]);
for(i=__1__; __2__; i++)
{
if(__3__==0)
{
f=__4__;
break;
}
}
if(f==__5__)
[Link](“The given no. “+n+” is a prime number”);
else
[Link](“The given no. “+n+” is not a prime number”);
}
}Ans:
(1) 2 (2) i < n (3) n%i (4) 1 (5) 0
Q6 Convert following do-while loop into for loop.
int i=1;
int d=5;
do{
d=d*2
[Link](d);
i++;
}while(i<=5);
Ans.
for(int i=1, d=5; i<=5; i++)
{
d = d * 2;
[Link](d);
}
Q7 Convert the following while loop to the corresponding for loop
int m = 5, n = 10;
while (n>=1)
{
[Link](m*n);
n–-;
}
Ans.
for(int m=5, n=10; n >=1; n--)
{
[Link](m*n);
}
Q8 Write the output and show the dry run too.
public class test
{
public static void main()
{
int i=1,n=6,f=1;
while(i<=n)
{
f=f*i; i++;
[Link](f+” “);
}}}
Q9 Write an equivalent while() loop for the following for() loop.
int s=0;
for(int x=1; x<=25; x+=2)
s+=x;
Ans.
int x=1,s=0;
while (x<=25){
s +=x;
x+=2;
}
Q10 Analyze the following program segment and determine how many times the body of loop
will executed ?
int x= 5;
int y= 75;
while (x <= y) {
y =y/x;
[Link](y);
}
Ans.
The above loop will execute two times
Value of x value of y
5 75 ->It is initial values, check for the condition x<= y
(true), enters into loop
15 -> new value y= 15, again check for the condition
x<= y (true)
3 ->new value y= 3, again check for the condition
x<= y (false) Loop gets terminated.
Output:
15
3
Q11 Predict the output
int a,b;
for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
[Link]((char) b);
[Link]( );
}
Ans
ABCDEF
BCDEF
Analyse the following program segment and determine how many times the loop will be
executed and what will be the output of the program segment.
int k=1,i=2;
while(++i<6)
k*=i;
[Link](k);
Ans.
The loop will execute 3 times and the output is 60