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

Computer Applications: Loops & Programs

The document contains a series of programming exercises focused on loops and nested loops in Java. It includes tasks such as predicting outputs of given code snippets, writing programs for specific functionalities, and analyzing code behavior. The exercises aim to enhance understanding of control structures and algorithmic thinking in computer applications for Std IX students.

Uploaded by

Atharvva
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)
5 views4 pages

Computer Applications: Loops & Programs

The document contains a series of programming exercises focused on loops and nested loops in Java. It includes tasks such as predicting outputs of given code snippets, writing programs for specific functionalities, and analyzing code behavior. The exercises aim to enhance understanding of control structures and algorithmic thinking in computer applications for Std IX students.

Uploaded by

Atharvva
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

Arya Vidya Mandir Group of Schools

Std IX Computer Applications

Loops and Nested loops

1) Predict the output and also write the number of times the loop will execute:
public class MultipleVariables
{
public static void main()
{
for(int i=0,j=1; i<2 && j<3 ; i++,j++)
{
[Link]("i:" + i + "\nj:" + j);
}
}
}

2)Predict the output:


int i;
for(i=15;i>1;i-=2)
{
if(i>10)
[Link](i++);
else
[Link](--i);
}

3) What will be the output of the following code?


int m=2;
int n=15;
for(int i=1;i<5;i++)
m++;
--n;
[Link]("m="+m);
[Link]("n="+n);

4) Give the output of the following program segment and also mention the number of
times the loop is executed.
int a,b;
for(a=6,b=4;a<=4;a=a+6)

1 | Page
{
if(a%b==0)
break;
}
[Link](a);

5) Write a program to find the sum of all 3-digit even natural numbers.

6) Write a program to input an integer and print its factors.

7) Allow the user to input 50 numbers. Count the numbers which are divisible by 3.

8) Write a program to input an integer and find its factorial.


Factorial of a number is the product of all natural numbers till that number.
For example, the factorial of 5 is 120 since 1×2×3×4×5=120.

9)Write a program with two functions for the following:


(i) To find and display the sum of the series given below:
𝑆 = 𝑥1 – 𝑥2 + 𝑥3 – 𝑥4 + 𝑥5. . . . . . . . . . . . . . . .. – 𝑥20
(where x = 2)
(ii) To display the following series:
1 11 111 1111 11111

10) A tech number has even number of digits. If the number is split in two equal
halves, then the square of sum of these halves is equal to the number itself.
Write a program to generate and print all four digits tech [Link],3025 and
9801
Example: Consider the number 3025 Square of sum of the halves of 3025 =
(30+25)2 = (55)2 = 3025 is a tech number.

11) Convert the following segment into an equivalent do-while loop.


int x,c;
for(x=10,c=20;c>=10;c=c-2)
x++;

12) Analyse the following program segment and determine how many times the loop
will be executed. What will be the output of the program segment?

int p=200;
while(true)
{
if(p<100)
break;
p=p–20;
}
[Link](p);

13) Rewrite the following program segment using a while loop instead of the for
statement.
int f=1,i;

2 | Page
for(i=1;i<=5;i++)
{
f*=i;
[Link](f);
}

14) Write a program to generate all 3 digit palindrome numbers.


eg. 121 is a palindrome number. If you read the number left to right OR right to left it
will be same number.

15) Analyze the given program segment and answer the following questions:
for(int i=3;i<=4;i++)
{
for(int j=2;j<i;j++)
{
[Link]("");
}
[Link]("WIN" );
}
(i) How many times does the inner loop execute?
(ii) Write the output of the program segment.

16) Give the output of the following snippets based on nested loops
a) int i,j;
for (i=0; i<4; i++)
{
for (j=i; j>=0; j--)
[Link](j);
[Link]();
}

b) int y,p;
for (int x=1; x<=3; x++)
{
for (y=1; y<=2; y++)
{
p = x * y;
[Link](p);
}
[Link]( );
}

c) int a,b;
for (a=1; a<=2; a++)
{
for (b=(64+a); b<=70; b++)
[Link]((char)b);
[Link]();
}

3 | Page
17) Write a program to display all prime and non-prime numbers from 1 to 100 using
switch case. Take input of choice from the user.
If choice is 1: display all prime numbers
If choice is 2: display all non-prime numbers
Hint: A number is said to be prime if it is only divisible by 1 and the number itself.

18)
Give the output:
int x,y;
for(x=1; x<=5; x++)
{
for(y=1; y<x; y++)
{
if(x == 4)
break;
[Link](y);
}
[Link]( );
}

19)
i. Give the output of the following program segment and also mention the number of
times the loop is executed.
int a,b;
for(a=6,b=4;a<=4;a=a+6)
{
if(a%b==0)
break;
}
[Link](a);

ii. Analyse the following program segment and determine how many times the loop
will be executed. What will be the output of the program segment?

int p=200;
while(true)
{
if(p<100)
break;
p=p–20;
}
[Link](p);

20) Write a program to accept five whole numbers from user and print its square. But
if the whole number = 0 then print the number is zero and continue with the next
iteration.
**********END*********

4 | Page

Common questions

Powered by AI

Assign numbers to mutability: if the choice is 1, iterate from 1 to 100 and apply the prime checking condition (only divisible by 1 and itself). If choice is 2, use a similar iteration to identify non-primes. Implement logic within a switch-case to separate outputs based on user input choice.

The loop decreases `p` by 20 with each iteration. Starting from 200, `p` becomes 180, 160, 140, 120, and finally 100. The loop exits when `p-20` equals 80 which is less than 100. `System.out.println(p);` outputs 80.

Initialize `f` to 1 and `i` to 1. Use `while(i <= 5)` and inside the loop, multiply `f` by `i` for the factorial, print `f`, then increment `i`. This structure echoes the iterative multiplication of the for-loop.

To implement this, initialize sum as 0. Iterate a loop from 1 to 20. If the counter variable is odd, add 2 to sum, else subtract 2 from sum. Initialize x to 2 because all series elements are 2. Final sum will be calculated considering the alternation pattern with even elements subtracted.

The variable `m` will increment 4 times in the loop, resulting in `m = 6`. The expression `--n` executes once after the loop ends, so `n` becomes 14. Therefore, the output will be `m=6` and `n=14`.

The inner loop will execute 0 times when `i=3` because its condition `j<i` becomes `j<3` but starts from `j=2`, hence running once and printing an empty string. With `i=4`, it runs twice. The outer loop prints "WIN" each time. Output will be: WIN WIN

The loop will execute `i` from 0 to 1 and `j` from 1 to 2. The output will be: ``` i:0 j:1 i:1 j:2 ```The loop stops because `j` becomes 3, which fails the loop condition `j<3`.

Initialize `x` to 10 and `c` to 20. Use `while(c >= 10)`, increment `x`, and `decrement c` by 2 inside the loop body. This conversion keeps the loop condition and body behavior consistent with the original for-loop.

The outer loop will execute 0 times because the loop condition `a <= 4` is false initially as `a` is initialized to 6. Therefore, the statement `System.out.println(a);` will output 6.

A tech number is a number with an even number of digits that can be split into two equal halves, and the square of their sum equals the number itself. To identify four-digit tech numbers, iterate over all four-digit numbers, split each into two halves, compute the sum and its square. If this square equals the original number, it is a tech number. Examples are 2025, 3025, and 9801.

You might also like