Coding for DUMMIES 03 : printf();
If you are still not have
enough idea about C programming I recommend you to read and try this exercises
in this post on your own. Because it will help you for better understanding
about the C programming. In this post I’m going to discuss some exercises that
only requires printf function to achive the results. In this post also, I’m
going to maintain my Q&A style. Okay let’s move into the first question.
Question 1 - My first program
#include<stdio.h> int main ()
{ printf("Programming is Fun\n");
return 0; } |
Try this program.
In here you can see the #include <stdio.h> in
the starting in the program. As we said in the previous posts this indicates
the standard input output header file, which contains programs that allow us to
use functions like printf. Without this header file we have to type everything
to achieve that particular tasks again and again. If it happen like that, it
will be a totally time waste. In the second line you can see, we have called
the main function. It is good to remember, when we executing the program the
executer starts executing from this line. Now I have another question for you.
How do you identify that main is a function? That is by the parentheses that
immediately comes after the word main. Before the “main” there is the keyword “int”. It says that the type
of the return value. Then you can see the opening and closing braces. They
contain the body of the program, and also you can say they identify the scope
of the main function. Within braces you can see that there is a statement that
starts with a printf function. You already know what it does. Then the next
statement there is the return statement. It will return value 0, when our
program executed successfully. You can assign any integer value here. It will
not effect to the program. But in standard we use zero. Now you can see the
output like this ,
Other than the things we have discussed it is better to
understand the meaning of some words that are using in programming world.
Because in the journey of C programming you will hear these words. In this post
I’m going to explain three words like that.
The first one is gcc command. gcc command is the
command which we use to compile our c program when we are using the popular
GNUC compiler in UNIX.
The second word is Compile.
Compiling is converting the source program into a lower form. The compiler
examines each program statement and checks it to ensure that it conforms to the
syntax of the language. And also compiler indicates errors. The third word is Build.
The process of compiling and linking a program is often called building.
Now lets move into the next question.
Question 2 - Write a program
to display your personal details.
This is easy. Same as before. What you have to do is edit two extra statements to display your Reg. No and Index. The output will be as follows.
Question 3 - Write program to print
the following shapes
Shape 1 :
*
**
*****
*
* *
** * **
** * * **
*
*
* *
********
In this exercise you need use space bar and tab carefully to place the asterisks marks where it is necessary. The code for this program will be like this.
Shape 2 :
*
* *
* * *
* * * *
* * * * *
* * * * * *
Shape 3 :
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Question 4 - Print the Multiplication table for 2
This will little advance than previous questions. But
actually there is nothing. Only thing you have to do is use necessary
characters and place them correctly within a printf statement. Code of the
program will be like this.
Now its time to test your understanding so far. do the following questions without watching the code.
Question 5 - Write
a program to display following
train schedule
Question
6 - Write a program to display following
output:
Answers :
question 5 -
Contribution - Ranishka Gunathilake @RG761 5641 – 3.4
Sandakelum Kumarasiri @SK2208 5737 - 5,6
Mahesh Wijenayaka @MW1513 5734 - 1,2