C Programming Language Handbook
C has keywords
keywords are reserved words which carry special meaning
keywords are
switch |
for |
while |
Do while |
If |
Else |
Int |
float |
Char |
Double |
Case |
long |
printf() function is used to display a text to user on the output screen
scanf() function is used to take input from user
header file for printf() and scanf() is stdio.h
header file is a file that contains function definitions
extension of header file is .h
extension of c file is .c
main function is a function which has return type as int or void
if return type is int function main should return a integer value
if return type is void function main should return a float value
C program to display hello world on the output screen
#include<stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}
Output
Hello World
C program to input a number and display its value
#include<stdio.h>
int main()
{
int c;
printf(“Enter a Number”);
scanf(“%d”,&c);
printf(“Number you entered is %d”,c);
}
Explanation of the above program
#include<stdio.h>
#include is a preprocessor directive
stdio.h is header file which contains function definitions for printf and scanf functions
int main()
main is a function from where execution of program starts
int is return type of main function
int is a dataype of a variable
int variable takes 2 bytes of memory
printf() is a function that prints the text on the Console Screen which is also called Output Screen
scanf() is a function that takes input from user from the Console Screen or Output Screen
scanf() function can take input as integer, float, char and double
scanf() function contains %d for integer variable, %f for float variable , %c for char variable
%ld is used for double datatype
scanf() function takes address of a variable as %d
%d is sued for integers
%c is used for char variable
%f is used for float variable
%ld is used for double variable
if we want to display a integer variable in printf function , function contains
%d is sued for
integers
%c is used for char variable
%f is used for float variable
%ld is used for double variable
example of printf() statement to display int variable is
printf(“Value of a is %d”,ch);
example of scanf() statement to display int variable is
scanf(“%d”,&ch);
& operator is sued to take address of variable ch
printf function does not takes address of a variable but takes only name of variable
C program to take an int variable and display its value
#include<stdio.h>
int main()
{
int a;
printf(“Enter Value for a “);
scanf(“%d”,&a);
printf(“Value of a is %d”,a);
return 1;
}
Output
Enter value for a 20
Value for a is 20
C program to take a float variable and display its value
#include<stdio.h>
int main()
{
float a;
printf(“Enter Value for a “);
scanf(“%f”,&a);
printf(“Value of a is %f”,a);
return 1;
}
Output
Enter Value for a
1.23
Value of a is 1.23
C program to take a char variable as input and display its value
#include<stdio.h>
int main()
{
char a;
printf(“Enter a Character”);
scanf(“%c”,&a);
printf(“Value of char variable a is %c”,a);
return 1;
}
C program to take a double variable as input and display its value
#include<stdio.h>
int main()
{
double a;
printf(“Enter a Double Variable “);
scanf(“%ld”,&a);
printf(“Value of Double Variable is %ld”,a);
return 1;
}
C program to input a string which is group of alphabets and display its value
#include<stdio.h>
int main()
{
char name[20];
printf(“Enter a string”);
scanf(“%s”,name);
printf(“String you entered is %s”,name);
return 1;
}
Output
Enter a string raman
String you entered is raman
C program to find area of square by taking side as input
#include<stdio.h>
int main()
{
int side,area;
printf(“Enter side of square “);
scanf(“%d”,&side);
area=side*side;
printf(“Area of square is %d”,area);
return 1;
}
Output
Enter side of square 10
area of square is 100
C program to find perimeter of square by taking side as input
#include<stdio.h>
int main()
{
int side,perimeter;
printf(“Enter side of square “);
scanf(“%d”,&side);
perimeter=4*side;
printf(“Perimeter of square is %d”,perimeter);
return 1;
}
C program to input length and breadth of rectangle and find area of rectangle
#include<stdio.h>
int main()
{
int length,breadth,area;
printf(“Enter length of rectangle “);
scanf(“%d”,&length);
printf(“Enter breadth of rectangle “);
scanf(“%d”,&breadth);
area=length*breadth;
printf(“Area of Rectangle is %d”,area);
return 1;
}
Output
Enter length of rectangle 10
Enter breadth of rectangle 10
Area of Rectangle is 100
C program to input length and breadth of rectangle and find perimeter of rectangle
#include<stdio.h>
int main()
{
int length,breadth,perimeter;
printf(“Enter Length of rectangle “);
scanf(“%d”,&length);
printf(“Enter breadth of rectangle “);
scanf(“%d”,&breadth);
perimeter=2*(length+breadth);
printf(“Perimeter of Rectangle is %d”,perimeter);
return 1;
}
Output
Enter Length of Rectangle 10
Enter breadth of rectangle 20
Perimeter of rectangle 60
C program to input width, depth and height and display its volume
#include<stdio.h>
int main()
{
int width,depth,height,volume;
printf(“Enter Width of Box “);
scanf(“%d”,&width);
printf(“Enter Depth of Box “);
scanf(“%d”,&depth);
printf(“Enter Height of Box “);
scanf(“%d”,&height”);
volume=width*depth*height;
printf(“Volume of Box is %d”,volume);
return 1;
}
Output
Enter Width of Box 10
Enter Depth of Box 20
Enter Height of Box 30
Volume of Box is 6000
C program to input radius of circle and find area of circle
#include<stdio.h>
int main()
{
float radius,area;
printf(“Enter Radius of Circle”);
scanf(“%f”,&radius);
area=3.14*radius*radius;
printf(“Area of Circle is %f”,area);
return 1;
}
C program to input radius of circle and find circumference of circle
#include<stdio.h>
int main()
{
float radius,circumference;
printf(“Enter Radius of Circle “);
scanf(“%f”,&radius);
circumference=2*3.14*radius;
printf(“Circumference of Circle is %f”,circumference);
return 1;
}
C program to demonstrate if statement and check whether a is equal to 10 or not
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a”);
scanf(“%d”,&a);
if(a==10)
{
printf(“Value of a is equal to 10”);
}
else
{
printf(“Value of a is not equal to 10 “);
}
return 1;
}
Output
Enter value of a 10
Value of a is equal to 10
C program to demonstrate ++ operator which means increment operator
#include<stdio.h>
int main()
{
int a;
printf(“Enter Value of a “);
scanf(“%d”,&a);
printf(“Value of a is %d“,a);
a++;
printf(“Value of a after applying increment operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 10
Value of a after applying increment operator is 11
C program to demonstrate – operator which means decrement operator
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
printf(“Value of a is %d”);
a--;
printf(“Value of a after applying decrement operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 10
Value of a after applying increment operator is 9
C program to demonstrate += operator
+=2 operator increments value of a by 2
#include<stdio.h>
int main()
{
int a;
printf(“Enter Value of a “);
scanf(“%d”,&a);
printf(“Value of a is %d”,a);
a+=2;
printf(“Value of a after applying += operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 10
Value of a after applying += operator is 12
C program to demonstrate -= operator
-=2 operator decrements the value of a by 2
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a ”);
scanf(“%d”,&a);
printf(“Value of a after applying -=2 operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 10
Value of a after applying -=operator is 8
C program to demonstrate *= operator
*=2 operator multiplies value of variable with 2
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
printf(“Value of a after applying *= operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a after applying *= operator is 20
C program to demonstrate /= operator
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
printf(“Value of a is %d”,a);
a/=2;
printf(“Value of a after applying /= operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 5
Value of a applying /=operator is 5
C program to demonstrate %= operator
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
printf(“Value of a is %d”,a);
a%=2;
printf(“Value of a after applying %= operator is %d”,a);
return 1;
}
Output
Enter value of a 10
Value of a is 10
Value of a after applying %= operator is 0
program to demonstrate if statement to check whether value of a is greater than 10 or not
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
if(a>10)
{
printf(“Value of a is greater than 10”);
}
else
{
printf(“Value of a is less than 10 or equal to 10”);
}
return 1;
}
Output
Enter value of a 11
Value of a is greater than 10
program to demonstrate if statement to check whether value of a is less than 10 or not
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
if(a<10)
{
printf(“Value of a is less than 10”);
}
else
{
printf(“Value of a is greater than 10 or equal to 10”);
}
return 1;
}
Output
Enter value of a 9
Value of a is less than 10
C program to demonstrate to check if statement whether value of a is greater than 10 or not
#include<stdio.h>
int main()
{
int a;
printf(“Enter Value of a “);
scanf(“%d”,&a);
if(a>=10)
{
printf(“Value of a is greater than 10 or equal to 10”);
}
else
{
printf(“Value of a is less than 10”);
}
return 1;
}
C program to demonstrate if statement to check whether value of a is less than 10 or equal to 10 or not
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
if(a<=10)
{
printf(“Value of a is less than 10 or equal to 10 or not”);
}
else
{
printf(“Value of a is greater than 10”);
}
return 1;
}
C program to input a number and check whether it is not equal to 10
#include<stdio.h>
int main()
{
int a;
printf(“Enter value of a “);
scanf(“%d”,&a);
if(a!=10)
{
printf(“Value of a is not equal to 10 “);
}
else
{
printf(“Value of a is equal to 10 “);
}
return 1;
}
Output
Enter value of a 10
Value of a is not equal to 10
C program to input temperature of water and check whether it is equal to Boiling Point of Water
#include<stdio.h>
int main()
{
int temp;
printf(“Enter temperature in celsius”);
scanf(“%d”,&temp);
if(temp==100)
{
printf(“Temperature is Boiling Point of Water”);
}
else
{
printf(“Temperature is not Boiling point of water”);
}
return 1;
}
Output
Enter temperature in celsius 100
Tempearture is Boiling Point of Water
C program to input age of a person and check whether the person is eligible to vote or not
#include<stdio.h>
int main()
{
int age;
printf(“Enter Age of Person “);
printf(“%d”,&age);
if(age>=18)
{
printf(“Person is eligible to vote “);
}
else
{
printf(“Person is not eligible to vote “);
}
return 1;
}
Output
Enter Age of Person 20
Person is eligible to vote
C program to input a char variable and check whether char variable is equal to a or b
#include<stdio.h>
int main()
{
char c;
printf(“Enter an Alphabet “);
scanf(“%c”,&c);
if((c==’a’) || (ch==’b’))
{
printf(“Character is equal to a or b “);
}
else
{
printf(“Character is not equal to a or b”);
}
return 1;
}
Output
Enter An Alphabet a
Character is equal to a or b
C program to check whether marks taken by user are between 80 and 90
#include<stdio.h>
int main()
{
int marks;
printf(“Enter marks of student”);
scanf(“%d”,&marks);
if((marks>=80) and (marks<=90))
{
printf(“Marks are between 80 and 90”);
}
else
{
printf(“Marks are not between 80 and 90”);
}
return 1;
}
Output
Enter marks of student 85
Marks are between 80 and 90
C program to check a character for a vowel
#include<stdio.h>
int main()
{
char ch;
printf(“Enter an Alphabet”);
scanf(“%c”,&ch);
if((ch==’a’) || (ch==’e’) || (ch==’i’) || (ch==’o’) || (ch==’u’))
{
printf(“Character %c is a vowel “,ch);
}
else
{
printf(“Character %c is not a vowel”,ch);
}
return 1;
}
Output
Enter An Alphabet a
Character a is vowel
C program to calculate grade of student based on marks
marks>=80 and marks<=100 grade is a
marks>=70 and marks<80 grade is b
marks>=60 and marks<70 grade is c
marks>60 grade is d
#include<stdio.h>
int main()
{
int marks;
char grade;
printf(“Enter marks of student”);
scanf(“%d”,&marks);
if((marks>=80) and ((marks<=100))
{
grade=’A’;
}
else if ((marks>=70) and (marks<80))
{
grade=’B’;
}
else if((marks>=60) and (marks<70))
{
grade=’c’;
}
else
{
grade=’D’;
}
printf(“Grade of student is %c”,grade);
return 1;
}
Output
Enter marks of student 90
Grade of student is D
C program to demonstrate switch case statement to check whether character is vowel or not
#include<stdio.h>
int main()
{
char ch;
printf(“Enter a Character “);
scanf(“%c”,&ch);
switch(ch)
{
case ‘a’: printf(“Character is Vowel”);
break;
case ‘e’: printf(“Character is Vowel”);
break;
case ‘i’ : printf(“Character is Vowel”);
break;
case ‘o’: printf(“Character is Vowel”);
break;
case ‘u’: printf(“Character is Vowel”);
break;
default: printf(“Character is not Vowel”);
break;
}
return 1;
}
C program to demonstrate switch statement to display Day of Week as per Number of Day
#include<stdio.h>
int main()
{
int day;
printf(“Enter Day in Number “);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“Monday”);
break;
case 2: printf(“Tuesday”);
break;
case 3: printf(“Wednesday”);
break;
case 4: printf(“Thursday”);
break;
case 5: printf(“Friday”);
break;
case 6: printf(“Saturday”);
break;
case 7: printf(“Sunday”);
break;
default: printf(“Enter a day between 1 and 7”);
}
C program to convert temperature in celsius to fahrenheit and fahrenheit to celsius
#include<stdio.h>
int main()
{
float temp;
float conv;
int ch;
printf(“1. Convert temperature from Celsius to Fahrenheit”);
printf(“2. Convert temperature from Fahrenheit to Celsius”);
if(ch==1)
{
printf(“Enter Temperature in Celsius “);
scanf(“%f”,&temp);
conv=(1.8*temp)+32;
printf(“Temperature in Fahrenheit is %f“,conv);
}
if(ch==2)
{
printf(“Enter Temperature in Fahrenheit “);
scanf(“%f”,&temp);
conv=(temp-32)/1.8;
printf(“Temperature in Fahrenheit is %f”,conv);
}
return 1;
}
Output
Enter Temperature in Celisus 40.0
Temperature in Fahrenheit is 104.0
C program to demonstrate for loop to print numbers from 1 to 10
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
}
return 1;
}
Output
1
2
3
4
5
6
7
8
9
10
Program to print sum of even numbers and odd numbers from 1 to 10
#include<stdio.h>
int main()
{
int i;
int sumeven=0,sumodd=0;
for(i=1;i<=10;i++)
{
if(i%2==0)
{
sumeven=sumeven+i;
}
else
{
sumodd=sumodd+i;
}
printf(“Sum of Even Numbers is %d”,sumeven);
printf(“Sum of Odd Numbers is %d”,sumodd);
return 1;
}
Output
Sum of Even Numbers is 30
Sum of Odd Numbers is 25
C program to check whether number is even or odd
#include<stdio.h>
int main()
{
int a;
printf(“Enter a Number to check whether number is even or odd“);
scanf(“%c”,&a);
if(a==2)
{
printf(“Number is Even “);
}
else
{
printf(“Number is Odd “);
}
return 1;
}
C program to check whether first number is divisible by second number or not
#include<stdio.h>
int main()
{
int a,b;
printf(“Enter First Number “);
scanf(“%d”,&a);
printf(“Enter Second Number “);
scanf(“%d”,&b);
if(a%b==0)
{
printf(“First Number is divisible by second number “);
}
else
{
printf(“First Number is not divisible by second number”);
}
return 1;
}
C program to demonstrate for loop from two numbers entered by user
#include<stdio.h>
int main()
{
int a,b;
int i;
printf(“Enter Value of a ”);
scanf(“%d”,&a);
printf(“Enter Value of b “);
scanf(“%d”,&b);
for(i=a;i<=b;i++)
{
printf(“%d”,i);
}
return 1;
}
C program to demonstrate while loop to display numbers from 1 to 10
#include<stdio.h>
int main()
{
int i;
i=1;
while(i<=10)
{
printf(“%d”,i);
i++;
}
return 1;
}
C Program to check for prime number using while loop
#include<stdio.h>
int main()
{
int i=2;
int a;
int prime=1;
printf(“Enter a Number”);
scanf(“%d”,&a);
while(i<=a/2)
{
if(a%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf(“Number is Prime”);
}
else
{
printf(“Number is Not Prime”);
}
return 1;
}
Output
Enter a Number 5
Number is prime
C program to find fibonicci series from 1 to to 144
#include<stdio.h>
int main()
{
int i,a,b,c;
a=1;
b=1;
for(i=1;i<=10;i++)
{
printf(“%d”,a);
printf(“%d”,b);
c=a+b;
a=b;
b=c;
}
return 1;
}
Program to print table of number from 1 to a number entered by user
#include<stdio.h>
int main()
{
int i=1;
int a,b;
printf(“Enter a number to print table of ”);
scanf(“%d”,&a);
printf(“Enter a number to print the terms ”);
scanf(“%d”,&b);
while(i<=b)
{
printf(“Product of Number is %d is %d”,i,a*i);
i++;
}
return 1;
}
C program to find factorial of number from 1 to 10
#include<stdio.h>
int main()
{
int fact=1;
int a,i;
printf(“Enter a number to print factorial of “);
scanf(“%d”,&a);
for(i=1;i<=5;i++)
{
fact=fact*i;
}
printf(“Factorial of a is %d”,fact);
}
return 1;
}
C program to run a do while loop
#include<stdio.h>
int main()
{
int i=11;
do
{
printf(“%d”,i)
i++;
}
while(i<=10);
Output : 11
C program to run a do while loop
#include<stdio.h>
int main()
{
int i=1;
do
{
printf(“%d\n”,i);
i++;
}while(i<=10);
return 1;
}
Output
1
2
3
4
5
6
7
8
9
10
C program to demonstrate break statement in for loop
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
{
break;
}
printf(“%d”,i);
}
}
Output
1
2
3
4
C program to demonstrate continue statement in for loop
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==5)
{
continue;
}
printf(“%d”,i);
}
}
C program to create a userdefined function calc to calculate area of a rectangle
#include<stdio.h>
void calcarea(int length,int breadth)
{
int area;
area=length*breadth;
printf(“Area of Rectangle is “ + area);
}
int main()
{
int length,breadth;
printf(“Enter Length of Rectangle “);
scanf(“%d”,&length);
printf(“Enter Breadth of Rectangle “);
scanf(“%d”,&breadth);
calcarea(length*breadth);
return 1;
}
C program to calculate simpleinterest from principal amount,rate of interest and time in years
#include<stdio.h>
void calcsi(int p,int r,int t)
{
int si;
si=(p*r*t)/100;
printf(“Simple Interest is %d”,si);
}
int main()
{
int p,r,t;
p=1000;
r=20;
t=3;
calcsi(p,r,t);
return 1;
}
Output
Simple Interest is 600.0
C program to create a function which returns a value 1 for prime number and 0 for not a prime number
#include<stdio.h>
int checkprime(int a)
{
int i;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf(“Number is prime”);
}
else
{
printf(“Number is not prime”);
}
int main()
{
int a;
int prime=0;
printf(“\nEnter a number”);
scanf(“%d”,&a);
prime=checkprime(a);
if(a==1)
{
printf(“Number is prime”);
}
else
{
printf(“Number is not prime”);
}
return 1;
}
C program to create an array of 5 inegers and input values and display them
#include<stdio.h>
int main()
{
int arr[5];
int i,j;
for(i=0;i<5;i++)
{
printf(“\nEnter a number “);
scanf(“%d”,&arr[i]);
}
for(j=0;j<5;j++)
{
printf(“\nNumber is “,arr[i]);
}
return 1;
}
C program to create a structure and display it
#include<stdio.h>
struct employee
{
int ecode;
char name[20];
int esalary;
int edoj;
};
int main()
{
struct employee e;
printf(“\nEnter ecode”);
scanf(“%d”,&e.ecode);
printf(“\nEnter name”);
scanf(“%d”,e.name”);
printf(\nEnter Employee Salary “);
scanf(“%d”,&e.esalary);
printf(“\nEnter Employee Date of Joining “);
scanf(“%d”,&e.edoj);
printf(“\nEmployee Ecode %d”,e.ecode);
printf(“\nEmployee Name %s”,e.name);
printf(“\nEmployee Salary %d”,e.esalary);
printf(“\nEmployee Date of Joining %d”,e.edoj);
return 1;
}