C Exercises: Produce the following table of values
Generate a table with x,x+2,x+4 using loops
Write a C program using looping to produce the following table of values.
x x+2 x+4 x+6 -------------------------------- 1 3 5 7 4 6 8 10 7 9 11 13 10 12 14 16 13 15 17 19
Sample Solution:
C Code:
#include<stdio.h>
int main()
{
int x; // Declare variable x
// Print header for table
printf("x\tx+2\tx+4\tx+6\n\n");
printf("---------------------------\n");
// Loop to generate and print table values
for(x=1; x<=15; x+=3)
printf("%d\t%d\t%d\t%d\n", x, (x+2), (x+4), (x+6));
return 0; // Indicate successful program execution
}
Sample Output:
x x+2 x+4 x+6 --------------------------- 1 3 5 7 4 6 8 10 7 9 11 13 10 12 14 16 13 15 17 19
Flowchart:
C programming Code Editor:
Previous:Write a C program to demonstrates the difference between predecrementing and postdecrementing using the decrement operator --.
Next: Write a C program that reads the side (side sizes between 1 and 10 ) of a square and prints square using hash (#) character.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics