Please help am not very good at c program. Am supposed to create a source code that will display to the screen the first 20 numbers with their squares and cubes.
Certainly! I can help you with that. Here is a sample C program that will display the first 20 numbers along with their squares and cubes:
```c
#include <stdio.h>
int main() {
int i;
printf("Number\tSquare\tCube\n");
printf("------\t------\t------\n");
for (i = 1; i <= 20; i++) {
int square = i * i;
int cube = i * i * i;
printf("%d\t%d\t%d\n", i, square, cube);
}
return 0;
}
```
Now, let me explain how the code works:
1. The `#include <stdio.h>` line is included to allow the usage of input/output functionalities in the program.
2. The `main()` function is where the program starts its execution.
3. The variable `i` will be used to iterate through the numbers from 1 to 20.
4. The first two `printf` statements display the headers for the table that will be printed to the screen.
5. The `for` loop is used to iterate through the numbers from 1 to 20.
6. Inside the loop, the variables `square` and `cube` are calculated by multiplying `i` with itself, and with itself twice, respectively.
7. Finally, the `printf` statement inside the loop displays the current number, its square, and its cube in a tabular format.
8. The program ends with `return 0;` statement, indicating that everything executed successfully.
To compile and run the program:
1. Save the code in a file with a `.c` extension, such as `numbers.c`.
2. Open a terminal or command prompt and navigate to the directory where you saved the file.
3. Compile the code using a C compiler. For example, you can use the command `gcc numbers.c -o numbers` to compile the code and create an executable file called `numbers` (or replace `gcc` with the appropriate compiler on your system).
4. Run the compiled program by executing the command `./numbers` (or `numbers.exe` on Windows).
You should then see the table printed to the screen with the first 20 numbers, their squares, and cubes.