Hello Programmers! In this blog series I am going to teach you that how you can learn C and how you can practice C! So, here 3 different basic c program for you to practice.
Program 1. (Print any word on screen by c programming)
1 #include <stdio.h>
2 int main()
3 {
4 printf("Hello World");
5 return 0;
6 }
2 int main()
3 {
4 printf("Hello World");
5 return 0;
6 }
Explanation: With "printf "we print on screen. Here in line 4 you can see “Hello World”. When you run the program ‘Hello world’ will be on your screen. If you use ‘\n’ like : printf("Hello \n World"); then you will get a new line start with ‘world’! So, we can say ‘\n’ start a new line.
Program 2. (Print your own inputs by c programming)
1 #include <stdio.h>
2char main()
1 #include <stdio.h>
2char main()
3{
4 char anycharacter,character;
5 printf("Type a character here");
6 scanf("%c",&anycharacter);
7 character=anycharacter;
8 printf("The Character You Type Is %c",character);
9 }
4 char anycharacter,character;
5 printf("Type a character here");
6 scanf("%c",&anycharacter);
7 character=anycharacter;
8 printf("The Character You Type Is %c",character);
9 }
Explanation: Here we can input a character and then we can get back it as a printed element! So, you can see in line 6 we take an input which is a character. We can take input by ‘scanf’ . The %c is used for formatting the printing outputs of a character using the printf() or fprintf() . The % indicates that it will change that part of the text with some variable also passed as argument.
Program 3. (Calculate circle area with known radius by c programming)
1 #include <stdio.h>
3 #define PI 3.1416
4 void main()
5 {
6 int radius;
7 float area;
8 scanf("%d",&radius);
9 area=PI*(radius*radius);
10 printf("The area of the circle is %f",area);
11 }
1 #include <stdio.h>
3 #define PI 3.1416
4 void main()
5 {
6 int radius;
7 float area;
8 scanf("%d",&radius);
9 area=PI*(radius*radius);
10 printf("The area of the circle is %f",area);
11 }
Explanation: We know that that the area of a circle is πr*r (pi r square) , where r means radius of a circle. So, if you need the area of a circle then at first you need the radius of that circle as an input. In line 8 we get the input which is an integer value. In line we write the equation where we defined the value of PI in lien 3 and now, we can calculate the area. In line 9 we print the output. %d is for integer and %f is for float.
Stay cool!
Stay creative!