Programs of C-Language
In This Article, You will find out the Important programs in C-Language.
That will help you to perform accurately in your Paper.
Before going towards programs let us know what C-Language is?
C-language
The C programming language was developed by Dennis Ritchie in 1972 at AT & Bell Laboratories. It was
derived from an early programming language named B. The B was developed
by Ken Thomspon in 1969-1970.
Program # 1
Hello World!
#include <stdio.h>
Void main(void)
{
printf("Hello
World!");
Return 0;
}
Output
Hello World!
Program # 2
Sum of Characters
#include <stdio.h>
Void main(void)
{
char ch1,
ch2,
sum;
ch1 = '2';
ch2 = '6';
printf("Sum = %d", sum);
}
Output
Sum = 104
Explanation
The sum of character '2' and character '6' is equal to 104 because,
The ASCII code for character '2 is 50, and character '6' is 54.
Program # 3
Use of Comments
#include <stdio.h>
Void main(void)
{
/*
Name: Hamza Asif
Date: 20/06/22
*/
printf("This is My First Program.");
}
Output
This is My First Program.
Explanation
Symbols (/*) and (*/) are used to comment multi-line comments and
(//) symbol is used to print single-line comments.
Program # 4
Use of Increment Operator
#include <stdio.h>
Void main(void)
{
Int
I,
j,
k,
L;
j = 10;
I = ++j;
k = 10;
L = k++;
printf("I is %d\n", i);
printf("j is %d\n", j);
printf("k is %d\n", k);
printf("L is %d\n", L);
}
Output
I is 11
j is 11
k is 11
L is 11
Program # 5
Print Area of Square
#include <stdio.h>
Void main(void)
{
int height, width, area;
height = 4;
width = 4;
area=
height *
width;
printf("Area of square = %d",
area);
}
Output
Area of Square = 16
Program # 6
Sum of two floating numbers
#include <stdio.h>
Void main(void)
{
float num1, num2, sum;
num1 = 24.27;
num2 = 41.50;
printf("Sum = %f", sum);
}
Output
Sum = 65.77
Explanation
(%f) is the data type for "float". {printf("")} statement sums up
two floating point numbers and give output in float data type.
Program # 7
Use of format specifier
#include <stdio.h>
Void main(void)
{
int num1, num2, i_sum;
int num3, num4, f_sum;
num1 = 10;
num2 = 15;
num3 =
21.4798;
num4 = 3.7589;
i_sum = num1 + num2 ;
f_sum = num3 + num4
printf("Sum of %d + %d = %4d", i_sum);
printf("Sum of %f + %f = %2.2f:, f_sum);
}
Output
Sum of 10 + 15= 0025
Sum of 21.4798 + 3.7589 = 25.24
Explanation
We have used format specifiers %4d and %2.2f within printf statement which
specifies the number of columns in which our output will be displayed.
Program # 8
Use of Escape sequence
#include <stdio.h>
int main ()
{
printf("\n new line escape
sequence tutorial");
printf("\t 15 \t 400");
printf ("\n Second line
\n");
}
Output
__
new line escape sequence tutorial 15
400
Second line
--
Program # 9
Convert Kilometer into Meters
#include <stdio.h>
Void main(void)
{
double meter, kilometer;
printf("Please enter distance in Kilometers>");
scanf("%lf", &kilometer)
meter = kilometer * 1000;
printf("Given Kms in meters = %lf",
meter)
}
Output
Please enter distance in kilometers>45
Given Kms in meters = 45000.000000
Program # 10
Maximum between 3 numbers
#include <stdio.h>
Void main(void)
{
int a, b, c;
printf("Please enter three numbers\n");
scanf("%d %d %d", &a &b &c);
if(a> b)
{
printf("%d is
the maximum number", a);
} else
if (a> c)
{
printf("%d is
the maximum number",c);
} else if (b> c)
{
printf("%d is the maximum number",b);
} else
{
printf("%d is
the maximum number",c);
}
}
Output
Since we have to take input from the user
therefore we have not written any output.
Program # 11
Number is negative, positive or zero.
#include <stdio.h>
Void main(void)
{
int a;
printf("Please enter a numbers\n");
scanf(" %d", &a );
if(a == 0)
{
printf("%d is
Zero", a);
} else if (a > 0)
{
printf("%d is the
positive number",a);
} else if (a < 0)
{
printf("%d is the
negative number",a);
} else
{
printf("Please
Enter the Number");
}
}
#include <stdio.h>
Void main(void)
{
int a;
printf("Please enter a numbers\n");
scanf(" %d", &a );
if(a == 0)
{
printf("%d is
Zero", a);
} else if (a > 0)
{
printf("%d is the
positive number",a);
} else if (a < 0)
{
printf("%d is the
negative number",a);
} else
{
printf("Please
Enter the Number");
}
}
Output
Since we have to take input from the user therefore we have not
written any output.
Program # 12
Number is Even or Odd.
#include <stdio.h>
Void main(void)
{
int num;
printf("Please enter a numbers\n");
scanf(" %d", &num );
if(num % 2 == 0)
{
printf("%d is Even number",
num);
} else if
(! num % 2 == 0)
{
printf("%d
is the Odd number",num);
} else
{
printf("Please Enter the valid Number");
}
}
#include <stdio.h>
Void main(void)
{
int num;
printf("Please enter a numbers\n");
scanf(" %d", &num );
if(num % 2 == 0)
{
printf("%d is Even number",
num);
} else if
(! num % 2 == 0)
{
printf("%d
is the Odd number",num);
} else
{
printf("Please Enter the valid Number");
}
}
Output
Since we have to take input from the user therefore we have
not written any output.
Program # 13
Year is leap or not
#include <stdio.h>
Void main(void)
{
int year;
printf("Please enter a year\n");
scanf(" %d", &year );
if(year % 4 == 0)
{
printf("%d is leap year",
year);
} else
{
printf("%d is not a leap year",year);
}
}
#include <stdio.h>
Void main(void)
{
int year;
printf("Please enter a year\n");
scanf(" %d", &year );
if(year % 4 == 0)
{
printf("%d is leap year",
year);
} else
{
printf("%d is not a leap year",year);
}
}
Output
Since we have to take input from the user therefore we
have not written any output.
Program # 14
Find Factorial of any Number
#include <stdio.h>
Void main(void)
{
long float a=1, f=2,
n;
printf("Enter any Number\n");
scanf("%d", &n);
while(a <= n)
{
f= f*a;
a++;
}
printf("Factorial= %d", f);
}
Output
Since we have to take input from the user
therefore we have not written any output.
Program # 15
Enter day number to print day name
#include <stdio.h>
Void main(void)
{
int day;
printf("Please Enter day Number\n");
scanf("%d",
&day);
if(day ==
1)
{
printf("The day is
Monday");
} else if (day == 2)
{
printf("The day is
Tuesday");
} else if (day == 3)
{
printf("The day is
wednesday");
}
else if (day == 4)
{
printf("The day is
Thursday");
}
else if (day == 5)
{
printf("The day is
Friday");
} else if (day == 6)
{
printf("The day is
Saturday");
}
else if (day == 7)
{
printf("The day is
Sunday");
}
else
{
printf("Please enter Valid day
Number");
}
}
Output
Since we have to take input from
the user therefore we have not
written any output.
0 Comments