Computer Programming
C – Writing Programs
2021-’22 Winter B.Tech
Writing the first C program
Print “Hello World”
/* program to print “Hello World” */ /* COMMENT */
#include <stdio.h> / * LIBRARY FILE ACCESS */
int main() / * FUNCTION HEADING */
{
printf("Hello World"); / * OUTPUT STATEMENT *I
return 0;
}
Print “Hello World”
/* program to print “Hello World”*/ purpose/usage of the program
- Comments
- Can be multi-line
/* ……
…… */
- Can be single line
// ……………..
Print “Hello World”
#include<stdio.h> reference to the header file (stdio .h)
• Instructs the compiler to include this file to the program
(since the definition of “printf()” function is in this file)
Print “Hello World”
int main() heading of the main() function
{ … } the start and end of main() function.
The statements within the braces comprise the main().
return 0; program ends and the control is returned
- Starting point of execution
- If started with int main(),
- there should be return 0, as the last statement in the main function
- Alternative – void main()
Print “Hello World”
printf("Hello World"); printf() function prints the argument onto the screen
- Output data can be written from the computer onto a standard output device
- Library function – will see more on next program
An Online IDE to run your program
1. Go to the website:
https://www.onlinegdb.com/online_c_compiler
2. Copy-paste/write your program in the “Editor”.
3. Click on
4. The output will be printed on the “Console” below
Program 2
/* program to add two numbers*/
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the values of a and b ");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of %d and %d is %d",a,b,c);
return 0;
}
Program 2 – Add two numbers
Problem : Add two numbers and find the sum.
Input: two numbers- a, b
Output: sum of a and b
Step 1: Start
Step 2: Declare variables a, b and sum.
Step 3: Read values a and b.
Step 4: Add a and b and assign the result to sum; sum←a+b
Step 5: Display sum
Step 6: Stop
/* program to add two numbers*/
#include<stdio.h> Declaring the variables
int main()
{
int a,b,c;
printf("Enter the values of a and b ");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of %d and %d is %d",a,b,c);
return 0;
}
/* program to add two numbers*/
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the values of a and b "); Reading the numbers
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of %d and %d is %d",a,b,c);
return 0;
}
•scanf() function is defined in stdio.h file
•Input data can be entered into the computer from a standard input device
•scanf(control1 string, argl, arg2, . . . , argn)
•Use %d for integer, %c for character, %f for float or double values
/* program to add two numbers*/
#include<stdio.h>
int main()
{
int a,b,c;
Add a and b and assign the
printf("Enter the values of a and b "); result to another variable ‘c’
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum of %d and %d is %d",a,b,c);
return 0;
}
/* program to add two numbers*/
#include<stdio.h>
int main() Display the result in intended format -
{ Another variation of printf
int a,b,c;
printf(control string, arg1, arg2, .. , argn)
printf("Enter the values of a and b "); %c – for character
scanf("%d%d",&a,&b); %d – for integer
c=a+b; %f – for float
printf("Sum of %d and %d is %d",a,b,c);
return 0;
}
Program 3
/* program to calculate the area of a circle */
#include<stdio.h>
int main()
{
float radius,area;
printf("Enter the radius: ");
scanf("%f",&radius);
area=3.14159*radius*radius;
printf("Area = %f",area);
return 0;
}
Program 3
float radius,area; declare variables radius and area as type float
printf(“Enter the radius: "); prints the message on the screen
scanf("%f",&radius); prompts user to input the radius
• %f indicates that the corresponding variable radius is of type float.
area=3.14159*radius*radius; computes the expression and assigns the
value to the variable area
printf("Area = %f",area); Prints Area = 12.566360 if the radius entered is 2.
Do programs for
1. Find the Area and Perimeter of a Square.
2. Find the Simple Interest.
3. Find the average of two numbers
4. Find the sum of n natural numbers using
direct formulae
5. Convert the length from meter to centimeter