Day 5 of Programming in C
Write a program to print out all Armstrong numbers between 1 and 500.
#include <stdio.h>
int main(){
int d1,d2,d3,sum;
printf("Armstrong numbers between 1 to 500 are :\n");
for(int i=1;i<=500;i++){
d1 = i%10;
d2 = (i/10)%10;
d3 = i/100;
sum = (d1 * d1 * d1) + (d2 * d2 * d2) + (d3 * d3 * d3);
if(sum == i){
printf("%d\n",i);
}
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `d1`, `d2`, `d3`, and `sum`.
Step 4: Print the string "Armstrong numbers between 1 to 500 are :\n" to the console.
Step 5: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 500, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, calculate `d1` as the remainder of `i` divided by 10 (the units digit).
Step 7: Calculate `d2` as the remainder of `i` divided by 10, then divided by 10 (the tens digit).
Step 8: Calculate `d3` as `i` divided by 100 (the hundreds digit).
Step 9: Calculate `sum` as the sum of `d1` cubed, `d2` cubed, and `d3` cubed.
Step 10: Check if `sum` is equal to `i`.
Step 11: If `sum` is equal to `i`, print the value of `i` followed by a newline character to the console.
Step 12: The `for` loop continues until `i` is greater than 500.
Step 13: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `d1`, `d2`, `d3`, and `sum`.
Step 4: Print the string "Armstrong numbers between 1 to 500 are :\n" to the console.
Step 5: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 500, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, calculate `d1` as the remainder of `i` divided by 10 (the units digit).
Step 7: Calculate `d2` as the remainder of `i` divided by 10, then divided by 10 (the tens digit).
Step 8: Calculate `d3` as `i` divided by 100 (the hundreds digit).
Step 9: Calculate `sum` as the sum of `d1` cubed, `d2` cubed, and `d3` cubed.
Step 10: Check if `sum` is equal to `i`.
Step 11: If `sum` is equal to `i`, print the value of `i` followed by a newline character to the console.
Step 12: The `for` loop continues until `i` is greater than 500.
Step 13: Return 0 from the `main` function, indicating successful program execution.
Write a program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.
#include <stdio.h>
int main()
{
int pos = 0, neg = 0, zero = 0, num;
char ch;
do {
printf("Enter an integer: ");
if (scanf("%d", &num)){
(num > 0) ? pos++ : (num == 0) ? zero++ : neg++;
} else {
printf("Invalid input! Not an integer.\n");
scanf("%*s");
continue;
}
printf("Enter 'n' to quit, otherwise press Enter to continue: ");
fflush(stdin);
scanf("%c", &ch);
} while (ch != 'n');
printf("Positive = %d\nNegative = %d\nZero = %d\n", pos, neg, zero);
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `pos`, `neg`, `zero`, and `num`, initializing `pos`, `neg`, and `zero` to 0.
Step 4: Declare a character variable `ch`.
Step 5: Start a `do-while` loop.
Step 6: Inside the loop, print the string "Enter an integer: " to the console.
Step 7: Check if `scanf` successfully reads an integer and stores it in `num`.
Step 8: If `scanf` is successful, use a ternary operator: if `num` is greater than 0, increment `pos`; otherwise, if `num` is equal to 0, increment `zero`; otherwise, increment `neg`.
Step 9: If `scanf` fails, print "Invalid input! Not an integer.\n" to the console.
Step 10: If `scanf` fails, use `scanf("%*s")` to consume the invalid input from the input buffer.
Step 11: If `scanf` fails, use `continue` to restart the loop from the beginning.
Step 12: Print the string "Enter 'n' to quit, otherwise press Enter to continue: " to the console.
Step 13: Use `fflush(stdin)` to clear the input buffer.
Step 14: Read a character from the input and store it in `ch`.
Step 15: The `do-while` loop continues as long as `ch` is not equal to 'n'.
Step 16: Print the values of `pos`, `neg`, and `zero` with labels "Positive =", "Negative =", and "Zero =" to the console, each followed by a newline character.
Step 17: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `pos`, `neg`, `zero`, and `num`, initializing `pos`, `neg`, and `zero` to 0.
Step 4: Declare a character variable `ch`.
Step 5: Start a `do-while` loop.
Step 6: Inside the loop, print the string "Enter an integer: " to the console.
Step 7: Check if `scanf` successfully reads an integer and stores it in `num`.
Step 8: If `scanf` is successful, use a ternary operator: if `num` is greater than 0, increment `pos`; otherwise, if `num` is equal to 0, increment `zero`; otherwise, increment `neg`.
Step 9: If `scanf` fails, print "Invalid input! Not an integer.\n" to the console.
Step 10: If `scanf` fails, use `scanf("%*s")` to consume the invalid input from the input buffer.
Step 11: If `scanf` fails, use `continue` to restart the loop from the beginning.
Step 12: Print the string "Enter 'n' to quit, otherwise press Enter to continue: " to the console.
Step 13: Use `fflush(stdin)` to clear the input buffer.
Step 14: Read a character from the input and store it in `ch`.
Step 15: The `do-while` loop continues as long as `ch` is not equal to 'n'.
Step 16: Print the values of `pos`, `neg`, and `zero` with labels "Positive =", "Negative =", and "Zero =" to the console, each followed by a newline character.
Step 17: Return 0 from the `main` function, indicating successful program execution.
Write a program to produce the following output:
*
* *
* * *
* * * *
#include <stdio.h>
int main(){
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4 - i; j++)
{
printf("\t");
}
for (int k = 1; k <= i; k++)
{
printf("*\t\t");
}
printf("\n\n");
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 4, and incrementing `i` by 1 in each iteration.
Step 4: Inside the outer loop, start an inner `for` loop, initializing integer `j` to 1, continuing as long as `j` is less than or equal to 4 minus `i`, and incrementing `j` by 1 in each iteration.
Step 5: Inside the first inner loop, print a tab character (`\t`) to the console.
Step 6: The first inner loop continues until `j` is greater than 4 minus `i`.
Step 7: Start a second inner `for` loop, initializing integer `k` to 1, continuing as long as `k` is less than or equal to `i`, and incrementing `k` by 1 in each iteration.
Step 8: Inside the second inner loop, print an asterisk (`*`), a tab character (`\t`), and another tab character (`\t`) to the console.
Step 9: The second inner loop continues until `k` is greater than `i`.
Step 10: Print two newline characters (`\n\n`) to the console.
Step 11: The outer loop continues until `i` is greater than 4.
Step 12: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 4, and incrementing `i` by 1 in each iteration.
Step 4: Inside the outer loop, start an inner `for` loop, initializing integer `j` to 1, continuing as long as `j` is less than or equal to 4 minus `i`, and incrementing `j` by 1 in each iteration.
Step 5: Inside the first inner loop, print a tab character (`\t`) to the console.
Step 6: The first inner loop continues until `j` is greater than 4 minus `i`.
Step 7: Start a second inner `for` loop, initializing integer `k` to 1, continuing as long as `k` is less than or equal to `i`, and incrementing `k` by 1 in each iteration.
Step 8: Inside the second inner loop, print an asterisk (`*`), a tab character (`\t`), and another tab character (`\t`) to the console.
Step 9: The second inner loop continues until `k` is greater than `i`.
Step 10: Print two newline characters (`\n\n`) to the console.
Step 11: The outer loop continues until `i` is greater than 4.
Step 12: Return 0 from the `main` function, indicating successful program execution.
Write a program in C to display the final amount of 10 people such that : A = p(1+r/100)n and check which amount is larger.
#include <stdio.h>
#include <math.h>
int main(){
float a, p, r, n, max = 0;
for (int i = 0; i < 10; i++)
{
printf("Enter the values of P, R & N: ");
scanf("%f %f %f", &p, &r, &n);
a = p * pow(1 + r / 100, n);
printf("The principal amount compounds to: %.2f\n", a);
if (a > max)
{
max = a;
}
}
printf("The maximum principal amount is: %.2f\n", max);
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Include the math library `math.h`.
Step 3: Define the `main` function, which returns an integer.
Step 4: Declare float variables `a`, `p`, `r`, `n`, and `max`, initializing `max` to 0.
Step 5: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 10, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, print the string "Enter the values of P, R & N: " to the console.
Step 7: Read three float values from the input and store them in `p`, `r`, and `n` respectively using `scanf`.
Step 8: Calculate `a` using the formula `p * pow(1 + r / 100, n)`, where `pow` is a function from the math library.
Step 9: Print the string "The principal amount compounds to: " followed by the value of `a` formatted to two decimal places, followed by a newline character to the console.
Step 10: Check if `a` is greater than `max`.
Step 11: If `a` is greater than `max`, assign the value of `a` to `max`.
Step 12: The `for` loop continues until `i` is greater than or equal to 10.
Step 13: Print the string "The maximum principal amount is: " followed by the value of `max` formatted to two decimal places, followed by a newline character to the console.
Step 14: Return 0 from the `main` function, indicating successful program execution.
Step 2: Include the math library `math.h`.
Step 3: Define the `main` function, which returns an integer.
Step 4: Declare float variables `a`, `p`, `r`, `n`, and `max`, initializing `max` to 0.
Step 5: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 10, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, print the string "Enter the values of P, R & N: " to the console.
Step 7: Read three float values from the input and store them in `p`, `r`, and `n` respectively using `scanf`.
Step 8: Calculate `a` using the formula `p * pow(1 + r / 100, n)`, where `pow` is a function from the math library.
Step 9: Print the string "The principal amount compounds to: " followed by the value of `a` formatted to two decimal places, followed by a newline character to the console.
Step 10: Check if `a` is greater than `max`.
Step 11: If `a` is greater than `max`, assign the value of `a` to `max`.
Step 12: The `for` loop continues until `i` is greater than or equal to 10.
Step 13: Print the string "The maximum principal amount is: " followed by the value of `max` formatted to two decimal places, followed by a newline character to the console.
Step 14: Return 0 from the `main` function, indicating successful program execution.
Display the ASCII values of lowercase alphabets.
#include <stdio.h>
int main(){
for (int ch = 'a'; ch <= 'z'; ch++)
{
printf("ASCII Value of %c is %d\n", ch, ch);
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `ch` to the ASCII value of 'a', continuing as long as `ch` is less than or equal to the ASCII value of 'z', and incrementing `ch` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `ch`, followed by the string " is ", followed by the integer value of `ch`, followed by a newline character to the console.
Step 5: The `for` loop continues until `ch` is greater than the ASCII value of 'z'.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `ch` to the ASCII value of 'a', continuing as long as `ch` is less than or equal to the ASCII value of 'z', and incrementing `ch` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `ch`, followed by the string " is ", followed by the integer value of `ch`, followed by a newline character to the console.
Step 5: The `for` loop continues until `ch` is greater than the ASCII value of 'z'.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Display the ASCII values of uppercase alphabets.
#include <stdio.h>
int main(){
for (int ch = 'A'; ch <= 'Z'; ch++)
{
printf("ASCII Value of %c is %d\n", ch, ch);
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `ch` to the ASCII value of 'A', continuing as long as `ch` is less than or equal to the ASCII value of 'Z', and incrementing `ch` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `ch`, followed by the string " is ", followed by the integer value of `ch`, followed by a newline character to the console.
Step 5: The `for` loop continues until `ch` is greater than the ASCII value of 'Z'.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `ch` to the ASCII value of 'A', continuing as long as `ch` is less than or equal to the ASCII value of 'Z', and incrementing `ch` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `ch`, followed by the string " is ", followed by the integer value of `ch`, followed by a newline character to the console.
Step 5: The `for` loop continues until `ch` is greater than the ASCII value of 'Z'.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Display the ASCII values 1 to 40 special characters.
#include <stdio.h>
int main(){
for(int i=1;i<=40;i++){
printf("ASCII Value of %c is %d\n",i, i);
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 40, and incrementing `i` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `i`, followed by the string " is ", followed by the integer value of `i`, followed by a newline character to the console.
Step 5: The `for` loop continues until `i` is greater than 40.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Start a `for` loop, initializing integer `i` to 1, continuing as long as `i` is less than or equal to 40, and incrementing `i` by 1 in each iteration.
Step 4: Inside the loop, print the string "ASCII Value of ", followed by the character represented by `i`, followed by the string " is ", followed by the integer value of `i`, followed by a newline character to the console.
Step 5: The `for` loop continues until `i` is greater than 40.
Step 6: Return 0 from the `main` function, indicating successful program execution.
Display the prime numbers from 1-1000.
#include <stdio.h>
int main(){
int num, count;
printf("List of prime numbers between 1 to 1000:\n");
for (num = 2; num <= 1000; num++)
{
count = 0;
for (int i = 2; i < num; i++)
{
if (num % i == 0){
count++;
break;
}
}
if(count == 0)
printf("%d\n", num);
}
return 0;
}
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `num` and `count`.
Step 4: Print the string "List of prime numbers between 1 to 1000:\n" to the console.
Step 5: Start a `for` loop, initializing `num` to 2, continuing as long as `num` is less than or equal to 1000, and incrementing `num` by 1 in each iteration.
Step 6: Inside the outer loop, initialize `count` to 0.
Step 7: Start an inner `for` loop, initializing integer `i` to 2, continuing as long as `i` is less than `num`, and incrementing `i` by 1 in each iteration.
Step 8: Inside the inner loop, check if `num` modulo `i` is equal to 0.
Step 9: If `num` modulo `i` is equal to 0, increment `count` by 1.
Step 10: If `num` modulo `i` is equal to 0, use `break` to exit the inner loop.
Step 11: The inner loop continues until `i` is greater than or equal to `num`.
Step 12: Check if `count` is equal to 0.
Step 13: If `count` is equal to 0, print the value of `num` followed by a newline character to the console.
Step 14: The outer loop continues until `num` is greater than 1000.
Step 15: Return 0 from the `main` function, indicating successful program execution.
Step 2: Define the `main` function, which returns an integer.
Step 3: Declare integer variables `num` and `count`.
Step 4: Print the string "List of prime numbers between 1 to 1000:\n" to the console.
Step 5: Start a `for` loop, initializing `num` to 2, continuing as long as `num` is less than or equal to 1000, and incrementing `num` by 1 in each iteration.
Step 6: Inside the outer loop, initialize `count` to 0.
Step 7: Start an inner `for` loop, initializing integer `i` to 2, continuing as long as `i` is less than `num`, and incrementing `i` by 1 in each iteration.
Step 8: Inside the inner loop, check if `num` modulo `i` is equal to 0.
Step 9: If `num` modulo `i` is equal to 0, increment `count` by 1.
Step 10: If `num` modulo `i` is equal to 0, use `break` to exit the inner loop.
Step 11: The inner loop continues until `i` is greater than or equal to `num`.
Step 12: Check if `count` is equal to 0.
Step 13: If `count` is equal to 0, print the value of `num` followed by a newline character to the console.
Step 14: The outer loop continues until `num` is greater than 1000.
Step 15: Return 0 from the `main` function, indicating successful program execution.
Comments