In the previous section we discussed the basics of C variables. Now let us take a look at some real-life application Problems.
1. Write a C Program to reverse a user-entered number
#include
int main() {
int num, reverse_number = 0;
printf("Enter number: ");
scanf("%d", &num);
while (num > 0) {
reverse_number = reverse_number* 10 +num % 10;
num /= 10;
}
printf("Reversed number: %d\n", reverse_number);
return 0;
}
// Enter number: 1234
// Reversed number: 4321
| Iteration | num (Before) | num % 10 (Last Digit) | reverse_number Calculation | num /= 10 (Remaining) |
|---|---|---|---|---|
| 1 | 1234 | 4 | (0 * 10) + 4 = 4 | 123 |
| 2 | 123 | 3 | (4 * 10) + 3 = 43 | 12 |
| 3 | 12 | 2 | (43 * 10) + 2 = 432 | 1 |
| 4 | 1 | 1 | (432 * 10) + 1 = 4321 | 0 |
| End | 0 | N/A | Loop terminates | 0 |
2. Write a C program to count the number of digits in a number
#include
int main() {
int num, num_count = 0;
printf("Enter number: ");
scanf("%d", &num);
while (num != 0) {
num_count++;
num /= 10;
}
printf("Number of digits: %d\n", num_count);
return 0;
}
// Enter number: 25355
// Number of digits: 5
| Iteration | num (Before) | Action (num_count++) | num /= 10 (Remaining) | num_count |
|---|---|---|---|---|
| 1 | 25355 | Increment | 25355 / 10 = 2535 | 1 |
| 2 | 2535 | Increment | 2535 / 10 = 253 | 2 |
| 3 | 253 | Increment | 253 / 10 = 25 | 3 |
| 4 | 25 | Increment | 25 / 10 = 2 | 4 |
| 5 | 2 | Increment | 2 / 10 = 0 | 5 |
| End | 0 | N/A | Loop terminates | 5 |
3. Write a C Program to check if the number is an Armstrong number or not
#include
#include
int main() {
int num, temp, digit, count = 0;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp != 0) {
count++;
temp /= 10;
}
temp = num;
while (temp != 0) {
digit = temp % 10;
sum += pow(digit, count);
temp /= 10;
}
if (sum == num)
printf("%d is an Armstrong number\n", num);
else
printf("%d is not an Armstrong number\n", num);
return 0;
}
// Enter a number: 156
// 156 is not an Armstrong number
// Enter a number: 153
// 153 is an Armstrong number
| Iteration | digit (temp % 10) | Calculation (pow(digit, 3)) | sum += result | temp /= 10 |
|---|---|---|---|---|
| 1 | 3 | 3^3 = 27 | 0 + 27 = 27 | 15 |
| 2 | 5 | 5^3 = 125 | 27 + 125 = 152 | 1 |
| 3 | 1 | 1^3 = 1 | 152 + 1 = 153 | 0 |
4. Write a C Program to print the below Pattern by using while Loop
* * * *
* * * *
* * * *
* * * *
#include
int main() {
int i = 1, j, n = 4;
while (i <= n) {
j = 1;
while (j <= n) {
printf("* ");
j++;
}
printf("\n");
i++;
}
return 0;
}
// * * * *
// * * * *
// * * * *
// * * * *
| Outer Iteration (i) | Inner Iteration (j) | Action | Output in Row |
|---|---|---|---|
| i = 1 | j = 1, 2, 3, 4 | Print * four times | * * * * |
| j = 5 | j <= 4 is False | Move to next line (\n) | |
| i = 2 | j = 1, 2, 3, 4 | Print * four times | * * * * |
| j = 5 | j <= 4 is False | Move to next line (\n) | |
| i = 3 | j = 1, 2, 3, 4 | Print * four times | * * * * |
| j = 5 | j <= 4 is False | Move to next line (\n) | |
| i = 4 | j = 1, 2, 3, 4 | Print * four times | * * * * |
| j = 5 | j <= 4 is False | Move to next line (\n) | |
| i <= 4 is False | Loop terminates |
5. Write a C Program to print the below pattern
1
1 2
1 2 3
1 2 3 4
#include
int main() {
int i = 1, j;
while (i <= 4) {
j = 1;
while (j <= i) {
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
// 1
// 1 2
// 1 2 3
// 1 2 3 4
| Row (i) | Condition (i <= 4) | Inner (j) | Condition ( j <= i) | Action | Row Output |
|---|---|---|---|---|---|
| 1 | True | 1 | 1 <= 1 (True) | Print 1 | 1 |
| 2 | 2 <= 1 (False) | Next Line | |||
| 2 | True | 1, 2 | 1 <= 2(True), 2 <= 2 (True) | Print 1 , 2 | 1 2 |
| 3 | 3 <= 2(False) | Next Line | |||
| 3 | True | 1, 2, 3 | 1 <= 3(True), 2 <= 3(True), 3 <= 3(True) | Print 1, 2, 3 | 1 2 3 |
| 4 | 4 <= 3(False) | Next Line | |||
| 4 | True | 1, 2, 3, 4 | 1 <= 4, .......... 4 <= 4 (True) | Print 1, 2, 3, 4 | 1 2 3 4 |
| 5 | 5 <= 4(False) | Next Line | |||
| 5 | False | - | - | Terminate |
6. Write a C Program to print the pyramid pattern
*
* *
* * *
* * * *
#include
int main() {
int i = 1, j, space;
while (i <= 4) {
space = 4 - i;
while (space-- > 0)
printf(" ");
j = 1;
while (j <= i) {
printf("* ");
j++;
}
printf("\n");
i++;
}
return 0;
}
// *
// * *
// * * *
// * * * *