Chapter 6 - Let Us C
Write a program to print the multiplication table of the number entered by the user.
#include <stdio.h>
int main(){
int num;
printf("Enter the number : ");
scanf("%d",&num);
for(int i=1;i<=10;i++){
printf("%d * %d = %d\n",num,i,num*i);
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `num` to store the number entered by the user.
Step 4: Prompt the user to enter a number and store it in the `num` variable using `scanf`.
Step 5: A `for` loop iterates from 1 to 10 (inclusive). The loop variable `i` represents the multiplier.
Step 6: Inside the loop, `printf` is used to display the multiplication table for the given number. It prints the number (`num`), the multiplier (`i`), and their product (`num * i`). The `\n` creates a new line after each row of the multiplication table is printed.
Step 7: The loop continues until `i` becomes greater than 10.
Step 8: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `num` to store the number entered by the user.
Step 4: Prompt the user to enter a number and store it in the `num` variable using `scanf`.
Step 5: A `for` loop iterates from 1 to 10 (inclusive). The loop variable `i` represents the multiplier.
Step 6: Inside the loop, `printf` is used to display the multiplication table for the given number. It prints the number (`num`), the multiplier (`i`), and their product (`num * i`). The `\n` creates a new line after each row of the multiplication table is printed.
Step 7: The loop continues until `i` becomes greater than 10.
Step 8: The program returns 0, indicating successful execution.
According to a study, the approximate level of intelligence of a
person can be calculated using the following formula:
i = 2+(y+0.5x)
Write a program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.
#include <stdio.h>
int main()
{
printf("y | x | i\n");
for(int y=1;y<=6;y++){
for(float x = 5.5;x<=12.5;x+=0.5){
printf("%d | %.1f | %.2f\n",y,x,2+(y+0.5*x));
}
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Print the header for the table: "y | x | i\n".
Step 4: An outer `for` loop iterates from `y = 1` to `y = 6` (inclusive).
Step 5: Inside the outer loop, an inner `for` loop iterates from `x = 5.5` to `x = 12.5` incrementing by 0.5 in each step (`x += 0.5`).
Step 6: Inside the inner loop:
Step 8: The outer loop continues until `y` becomes greater than 6.
Step 9: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Print the header for the table: "y | x | i\n".
Step 4: An outer `for` loop iterates from `y = 1` to `y = 6` (inclusive).
Step 5: Inside the outer loop, an inner `for` loop iterates from `x = 5.5` to `x = 12.5` incrementing by 0.5 in each step (`x += 0.5`).
Step 6: Inside the inner loop:
- Calculate the value of `i` using the formula `2 + (y + 0.5 * x)`.
- Print the values of `y`, `x`, and `i` in a formatted table row using `printf`: `printf("%d | %.1f | %.2f\n", y, x, 2 + (y + 0.5 * x));` `%d` formats `y` as an integer, `%.1f` formats `x` as a floating-point number with one decimal place, and `%.2f` formats `i` as a floating-point number with two decimal places. `\n` creates a new line after each row.
Step 8: The outer loop continues until `y` becomes greater than 6.
Step 9: The program returns 0, indicating successful execution.
When interest compounds q times per year at an annual rate of r% for n years, the principal p compounds to an amount a as per the following formula a=p(1+r/q)^nq
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
#include <stdio.h>
#include <math.h>
int main(){
float a,p,r,q,n;
for (int i = 0; i < 10; i++)
{
printf("Enter the values of p,r,n & q : ");
scanf("%f %f %f %f",&p,&r,&n,&q);
a = p*(pow(1+r/q,n*q));
printf("The principal amount compounds to %f\n",a);
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare floating-point variables `a`, `p`, `r`, `q`, and `n` to store the amount, principal, rate, number of times interest is compounded per year, and number of years, respectively.
Step 4: A `for` loop iterates 10 times (from `i = 0` to `i = 9`).
Step 5: Inside the loop:
Step 7: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare floating-point variables `a`, `p`, `r`, `q`, and `n` to store the amount, principal, rate, number of times interest is compounded per year, and number of years, respectively.
Step 4: A `for` loop iterates 10 times (from `i = 0` to `i = 9`).
Step 5: Inside the loop:
- Prompt the user to enter the values of `p`, `r`, `n`, and `q` and store them in the respective variables using `scanf`.
- Calculate the compounded amount `a` using the formula: `a = p * (pow(1 + r / q, n * q));`. The `pow` function from the math library (which isn't explicitly included but should be) is used to calculate the power.
- Print the calculated compounded amount `a` using `printf`.
Step 7: The program returns 0, indicating successful execution.
If x is input through the keyboard, write a program to calculate the sum of first seven terms of the ln(x) series.
#include <stdio.h>
#include <math.h>
int main(){
float x,sum = 0;
printf("Enter a valid number : ");
scanf("%f",&x);
if(x<=0){
printf("Invalid Input! Please enter a positive number");
return 1;
}
for (int i = 1; i < 8; i++)
{
sum += (pow(x-1,i)/i)*(pow(-1,i+1));
}
printf("Your value is %.4f",sum);
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare a floating-point variable `x` to store the user's input and initialize a floating-point variable `sum` to 0.0 to store the result of the calculation.
Step 4: Prompt the user to enter a valid number and store it in `x` using `scanf`.
Step 5: Check if the input `x` is less than or equal to 0. If it is, print an "Invalid Input" message and exit the program with a return value of 1 (indicating an error).
Step 6: A `for` loop iterates from `i = 1` to `i = 7` (exclusive of 8).
Step 7: Inside the loop:
Step 9: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare a floating-point variable `x` to store the user's input and initialize a floating-point variable `sum` to 0.0 to store the result of the calculation.
Step 4: Prompt the user to enter a valid number and store it in `x` using `scanf`.
Step 5: Check if the input `x` is less than or equal to 0. If it is, print an "Invalid Input" message and exit the program with a return value of 1 (indicating an error).
Step 6: A `for` loop iterates from `i = 1` to `i = 7` (exclusive of 8).
Step 7: Inside the loop:
- Calculate the term `(pow(x - 1, i) / i) * pow(-1, i + 1)`. `pow(x - 1, i)` calculates (x-1) raised to the power of `i`. `pow(-1, i + 1)` calculates (-1) raised to the power of (i+1), which effectively alternates between -1 and 1, providing the alternating sign in the series.
- Add the calculated term to the `sum`.
Step 9: The program returns 0, indicating successful execution.
Write a program to generate all Pythagorean Triplets with side length less than or equal to 30.
#include <stdio.h>
int main(){
for (int c = 5; c <= 30; c++)
{
for (int b = 1; b < c; b++)
{
for (int a = 1; a < b; a++)
{
if (a * a + b * b == c * c)
printf("(%d,%d,%d)\n", a, b, c);
}
}
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf`.
Step 2: The `main` function is the entry point of the program.
Step 3: A nested set of `for` loops is used to iterate through possible values of `a`, `b`, and `c` to find Pythagorean triplets.
Step 4: The outermost loop iterates from `c = 5` to `c = 30` (inclusive). This loop represents the hypotenuse of the right-angled triangle.
Step 5: The middle loop iterates from `b = 1` to `b = c - 1`. This loop represents one of the legs of the right-angled triangle. It's important that `b` is less than `c` because the hypotenuse is always the longest side.
Step 6: The innermost loop iterates from `a = 1` to `a = b - 1`. This loop represents the other leg of the right-angled triangle. It's important that `a` is less than `b` to avoid duplicates (e.g., (3,4,5) and (4,3,5) would be counted as different triplets if `a` could be equal to or greater than `b`).
Step 7: Inside the innermost loop, the Pythagorean theorem is checked: `if (a * a + b * b == c * c)`.
Step 8: If the condition in Step 7 is true (the Pythagorean theorem holds), it means that `a`, `b`, and `c` form a Pythagorean triplet. The program prints the triplet using `printf`: `printf("(%d,%d,%d)\n", a, b, c);`.
Step 9: The innermost loop continues until `a` becomes equal to `b`.
Step 10: The middle loop continues until `b` becomes equal to `c`.
Step 11: The outermost loop continues until `c` becomes equal to 31.
Step 12: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: A nested set of `for` loops is used to iterate through possible values of `a`, `b`, and `c` to find Pythagorean triplets.
Step 4: The outermost loop iterates from `c = 5` to `c = 30` (inclusive). This loop represents the hypotenuse of the right-angled triangle.
Step 5: The middle loop iterates from `b = 1` to `b = c - 1`. This loop represents one of the legs of the right-angled triangle. It's important that `b` is less than `c` because the hypotenuse is always the longest side.
Step 6: The innermost loop iterates from `a = 1` to `a = b - 1`. This loop represents the other leg of the right-angled triangle. It's important that `a` is less than `b` to avoid duplicates (e.g., (3,4,5) and (4,3,5) would be counted as different triplets if `a` could be equal to or greater than `b`).
Step 7: Inside the innermost loop, the Pythagorean theorem is checked: `if (a * a + b * b == c * c)`.
Step 8: If the condition in Step 7 is true (the Pythagorean theorem holds), it means that `a`, `b`, and `c` form a Pythagorean triplet. The program prints the triplet using `printf`: `printf("(%d,%d,%d)\n", a, b, c);`.
Step 9: The innermost loop continues until `a` becomes equal to `b`.
Step 10: The middle loop continues until `b` becomes equal to `c`.
Step 11: The outermost loop continues until `c` becomes equal to 31.
Step 12: The program returns 0, indicating successful execution.
Population of a town today is 100000. The population has increased steadily at the rate of 10% per year for last 10 years. Write a program to determine the population at the end of each year in the last decade.
#include <stdio.h>
int main(){
int pop = 100000;
for(int year = 1;year<=10;year++){
pop = pop*0.9;
printf("Population %d years ago = %d\n",year,pop);
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `pop` and initialize it to 100000, representing the initial population.
Step 4: A `for` loop iterates from `year = 1` to `year = 10` (inclusive).
Step 5: Inside the loop:
Step 7: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `pop` and initialize it to 100000, representing the initial population.
Step 4: A `for` loop iterates from `year = 1` to `year = 10` (inclusive).
Step 5: Inside the loop:
- Calculate the population for the current year by multiplying the previous year's population by 0.9 (representing a 10% decrease): `pop = pop * 0.9;`
- Print the population for the current year using `printf`: `printf("Population %d years ago = %d\n", year, pop);`
Step 7: The program returns 0, indicating successful execution.
Ramanujan number (1729) is the smallest number that can be expressed as sum of two cubes in two different ways.Write a program to print all such numbers up to a reasonable limit.
#include <stdio.h>
int main(){
int limit;
printf("Enter a limit: ");
scanf("%d", &limit);
for (int i = 1729; i <= limit; i++) {
int count = 0;
for (int a = 1; a * a * a < i; a++) {
for (int b = a + 1; a * a * a + b * b * b <= i; b++) {
if (a * a * a + b * b * b == i) {
count++;
}
}
}
if (count >= 2) {
printf("%d\n", i);
}
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf` and `scanf`.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `limit` to store the upper limit entered by the user.
Step 4: Prompt the user to enter a limit and store it in the `limit` variable using `scanf`.
Step 5: An outer `for` loop iterates from `i = 1729` to `i = limit` (inclusive). This loop checks each number in the given range to see if it's a taxicab number.
Step 6: Inside the outer loop, an integer variable `count` is initialized to 0. This variable will keep track of how many ways the current number `i` can be expressed as the sum of two cubes.
Step 7: A nested set of `for` loops is used to iterate through all possible pairs of integers `a` and `b` such that `a^3 + b^3` could potentially equal `i`.
Step 8: The first inner loop iterates from `a = 1` while `a^3` is less than `i`. This is an optimization; there's no need to check larger values of `a`.
Step 9: The second inner loop iterates from `b = a + 1` while `a^3 + b^3` is less than or equal to `i`. It starts from `a+1` to avoid checking the same pair twice (e.g., 1^3 + 2^3 and 2^3 + 1^3).
Step 10: Inside the innermost loop, the sum of cubes is checked: `if (a * a * a + b * b * b == i)`.
Step 11: If the condition in Step 10 is true, it means that `i` can be expressed as the sum of `a^3` and `b^3`. Increment the `count`.
Step 12: The innermost loop continues until `b` becomes large enough that `a^3 + b^3` exceeds `i`.
Step 13: The first inner loop continues until `a^3` exceeds `i`.
Step 14: After the inner loops finish, check if `count` is greater than or equal to 2. This means that `i` can be expressed as the sum of two cubes in at least two *different* ways, so it's a taxicab number.
Step 15: If `count` is greater than or equal to 2, print the value of `i` using `printf`.
Step 16: The outer loop continues until `i` becomes greater than `limit`.
Step 17: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Declare an integer variable `limit` to store the upper limit entered by the user.
Step 4: Prompt the user to enter a limit and store it in the `limit` variable using `scanf`.
Step 5: An outer `for` loop iterates from `i = 1729` to `i = limit` (inclusive). This loop checks each number in the given range to see if it's a taxicab number.
Step 6: Inside the outer loop, an integer variable `count` is initialized to 0. This variable will keep track of how many ways the current number `i` can be expressed as the sum of two cubes.
Step 7: A nested set of `for` loops is used to iterate through all possible pairs of integers `a` and `b` such that `a^3 + b^3` could potentially equal `i`.
Step 8: The first inner loop iterates from `a = 1` while `a^3` is less than `i`. This is an optimization; there's no need to check larger values of `a`.
Step 9: The second inner loop iterates from `b = a + 1` while `a^3 + b^3` is less than or equal to `i`. It starts from `a+1` to avoid checking the same pair twice (e.g., 1^3 + 2^3 and 2^3 + 1^3).
Step 10: Inside the innermost loop, the sum of cubes is checked: `if (a * a * a + b * b * b == i)`.
Step 11: If the condition in Step 10 is true, it means that `i` can be expressed as the sum of `a^3` and `b^3`. Increment the `count`.
Step 12: The innermost loop continues until `b` becomes large enough that `a^3 + b^3` exceeds `i`.
Step 13: The first inner loop continues until `a^3` exceeds `i`.
Step 14: After the inner loops finish, check if `count` is greater than or equal to 2. This means that `i` can be expressed as the sum of two cubes in at least two *different* ways, so it's a taxicab number.
Step 15: If `count` is greater than or equal to 2, print the value of `i` using `printf`.
Step 16: The outer loop continues until `i` becomes greater than `limit`.
Step 17: The program returns 0, indicating successful execution.
Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight.
#include <stdio.h>
int main(){
for(int hour = 0;hour<=24;hour++){
(hour == 0)?(printf("Midnight\n")):
(hour<12)?(printf("%d:00AM\n",hour)):
(hour == 12)?(printf("Noon\n")): printf("%d:00PM\n",hour - 12);
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf`.
Step 2: The `main` function is the entry point of the program.
Step 3: A `for` loop iterates from `hour = 0` to `hour = 24` (inclusive). This loop represents the hours in a day (using a 24-hour clock).
Step 4: Inside the loop, a series of nested ternary operators is used to print the time in a 12-hour format with AM/PM indicators.
Step 5: The first ternary operator `(hour == 0) ? ... : ...` checks if the hour is 0 (midnight). If it is, print "Midnight\n".
Step 6: If the hour is not 0, the second ternary operator `(hour < 12) ? ... : ...` checks if the hour is less than 12 (AM).
Step 7: If the hour is less than 12, print the hour followed by ":00AM\n" using `printf("%d:00AM\n", hour);`.
Step 8: If the hour is not less than 12, the third ternary operator `(hour == 12) ? ... : ...` checks if the hour is 12 (noon).
Step 9: If the hour is 12, print "Noon\n".
Step 10: Otherwise (if the hour is greater than 12), it's PM. Print the hour minus 12 (to convert to 12-hour format) followed by ":00PM\n" using `printf("%d:00PM\n", hour - 12);`.
Step 11: The loop continues until `hour` becomes 25.
Step 12: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: A `for` loop iterates from `hour = 0` to `hour = 24` (inclusive). This loop represents the hours in a day (using a 24-hour clock).
Step 4: Inside the loop, a series of nested ternary operators is used to print the time in a 12-hour format with AM/PM indicators.
Step 5: The first ternary operator `(hour == 0) ? ... : ...` checks if the hour is 0 (midnight). If it is, print "Midnight\n".
Step 6: If the hour is not 0, the second ternary operator `(hour < 12) ? ... : ...` checks if the hour is less than 12 (AM).
Step 7: If the hour is less than 12, print the hour followed by ":00AM\n" using `printf("%d:00AM\n", hour);`.
Step 8: If the hour is not less than 12, the third ternary operator `(hour == 12) ? ... : ...` checks if the hour is 12 (noon).
Step 9: If the hour is 12, print "Noon\n".
Step 10: Otherwise (if the hour is greater than 12), it's PM. Print the hour minus 12 (to convert to 12-hour format) followed by ":00PM\n" using `printf("%d:00PM\n", hour - 12);`.
Step 11: The loop continues until `hour` becomes 25.
Step 12: The program returns 0, indicating successful execution.
Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
#include <math.h>
int main(){
int num = 1;
for (int i = 1; i <= 4; i++) {
for (int k = 0; k < 4 - i; k++) {
printf("\t");
}
for (int j = 0; j < i; j++) {
printf("%d\t\t", num++);
}
printf("\n");
}
return 0;
}
Step 1: Include the `stdio.h` header file for standard input/output functions like `printf`. The `math.h` header is included in the code, but it's not actually used. It can be removed.
Step 2: The `main` function is the entry point of the program.
Step 3: Initialize an integer variable `num` to 1. This variable will store the numbers to be printed in the pattern.
Step 4: An outer `for` loop iterates from `i = 1` to `i = 4` (inclusive). This loop controls the number of rows in the pattern.
Step 5: Inside the outer loop, a nested `for` loop (the first inner loop) iterates from `k = 0` to `k = 4 - i`. This loop prints the leading tabs before each row of numbers. The number of tabs decreases as `i` increases, creating the triangular shape.
Step 6: After the first inner loop, another nested `for` loop (the second inner loop) iterates from `j = 0` to `j = i - 1`. This loop controls the number of numbers printed in each row. The number of numbers increases as `i` increases.
Step 7: Inside the second inner loop, the current value of `num` is printed followed by two tabs: `printf("%d\t\t", num++);`. The `num++` increments the value of `num` after it's printed.
Step 8: After the second inner loop finishes, a newline character is printed to move to the next line: `printf("\n");`.
Step 9: The outer loop continues until `i` becomes 5.
Step 10: The program returns 0, indicating successful execution.
Step 2: The `main` function is the entry point of the program.
Step 3: Initialize an integer variable `num` to 1. This variable will store the numbers to be printed in the pattern.
Step 4: An outer `for` loop iterates from `i = 1` to `i = 4` (inclusive). This loop controls the number of rows in the pattern.
Step 5: Inside the outer loop, a nested `for` loop (the first inner loop) iterates from `k = 0` to `k = 4 - i`. This loop prints the leading tabs before each row of numbers. The number of tabs decreases as `i` increases, creating the triangular shape.
Step 6: After the first inner loop, another nested `for` loop (the second inner loop) iterates from `j = 0` to `j = i - 1`. This loop controls the number of numbers printed in each row. The number of numbers increases as `i` increases.
Step 7: Inside the second inner loop, the current value of `num` is printed followed by two tabs: `printf("%d\t\t", num++);`. The `num++` increments the value of `num` after it's printed.
Step 8: After the second inner loop finishes, a newline character is printed to move to the next line: `printf("\n");`.
Step 9: The outer loop continues until `i` becomes 5.
Step 10: The program returns 0, indicating successful execution.
Comments