PREPARED BY: SIR ZUBAIR KHAN ![]() |
The Textbook of COMPUTER SCIENCE For Class XII – Class 12 – Sindh Textbook Board |
(a).
55555
4444
333
22
1
Ans: Here's a C program to generate the desired output.
#include <stdio.h>
int main() {
int i, j;
for (i = 5; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}
Explanation:
The outer loop iterates from 5 to 1, with i decreasing by 1 in each iteration.
The inner loop iterates from 1 to i and prints the value of i j times on a single line.
After the inner loop, a newline character is printed to move to the next line.
(b). Write a C program to print the following output
*
**
***
****
*****
Ans: Here's a C program to generate the desired output.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
The outer loop iterates from 1 to 5, with i increasing by 1 in each iteration.
The inner loop iterates from 1 to i, and prints a * character j times on a single line.
After the inner loop, a newline character is printed to move to the next line.
(c). Write a C program to print the following output
1
12
123
1234
12345
Ans: Here's a C program to generate the desired output.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Explanation:
The outer loop iterates from 1 to 5, with i increasing by 1 in each iteration.
The inner loop iterates from 1 to i, and prints the value of j on a single line.
After the inner loop, a newline character is printed to move to the next line.
(d). Write a C program to print the following output
1
4 4
9 9 9
16 16 16 16
25 25 25 25 25
Ans: Here's a C program to generate the desired output.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", i*i);
}
printf("\n");
}
return 0;
}
Explanation:
The outer loop iterates from 1 to 5, with i increasing by 1 in each iteration.
The inner loop iterates from 1 to i, and prints the value of i*i followed by a space on a single line.
After the inner loop, a newline character is printed to move to the next line.
(e). Write a C program to print the following output
2 3 4 5 63 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Ans: Here's a C program to generate the desired output.
#include <stdio.h>
int main() {
int i, j;
for (i = 2; i <= 5; i++) {
for (j = 0; j < 5; j++) {
printf("%-8d", i + j);
}
printf("\n");
}
return 0;
}
Explanation:
The outer loop iterates from 2 to 5, with i increasing by 1 in each iteration.
The inner loop iterates from 0 to 4, and prints the value of i+j left-justified and padded to 8 characters on a single line.
After the inner loop, a newline character is printed to move to the next line.