0% found this document useful (0 votes)
30 views6 pages

Popc - Sample Programs

Popc programs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Popc - Sample Programs

Popc programs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SAMPLE C Programs

1. To print HELLO WORLD

#include<stdio.h>
main()
{
printf(“Hello World”);
}

2. To initialize and to display the value of type int

#include<stdio.h>
main()
{
int a; /* declaration of variable a */
a=10; /* initialization of variable a*/
printf(“The value of a is %d”, a);
}

3. To initialize and to display the value of type float

#include<stdio.h>
main()
{
float a;
a=10.50;
printf(“The value of a is %d”, a);
}

4. To initialize and to display the value of type char


NOTE: while printing character or strings donot use & symbol in printf statement
#include<stdio.h>
main()
{
char a;
a = ‘S’;
printf(“The value of a is %c”, a);
}
5. To initialize and to display the value in same line

#include<stdio.h>
main()
{
int a=10; /* declaration and initialization are done in same line */
printf(“The value of a is %d”, a);
}

6. To initialize and display two or more variable

#include<stdio.h>
main()
{
int a=10, b=50 ; /* declaration and initialization are done in same line */
printf(“The value of a is %d %d”, a,b);
}

7. To perform addition of two values

#include<stdio.h>
main()
{
int a = 10, b = 50, c ; /* declaration and initialization are done in same line */
c = a + b;
printf(“The sum is %d”, c);
}

8. To perform addition of two values by reading the value from user


#include<stdio.h>
main()
{
int a , b, c ; /* declaration part*/
printf(“Enter the values for a and b”);
scanf(“%d %d”, &a, &b); /* scanf is used to read input from user */

c = a + b;
printf(“The sum is %d”, c);
}
Similarly write the program for subtraction, multiplication

9. To perform simple mathematical calculation by reading the values from user

#include<stdio.h>
main()
{
int a , b, sum,sub,mul ; /* declaration part */
printf(“Enter the values for a and b”);
scanf(“%d %d”, &a, &b); /* scanf is used to read input from user */

sum = a + b;
sub = a - b;
mul = a * b;
printf(“The result of addition is sum is %d”, sum);
printf(“The result of subtraction is %d”, sub);
printf(“The result of multiplication is %d”, mul);
}

10. To calculate the area of a circle

#include<stdio.h>
main()
{
float pi = 3.148 , area, radius; /* declaration part */
printf(“Enter the value for radius of a circle”);
scanf(“%f”, &radius);
area = pi * radius * radius;
printf(“the area of a circle is %f”, area);
}

11. To swap two numbers

#include<stdio.h>
main()
{
int a,b,temp; /* declaration part */
printf(“Enter the value for a and b”);
scanf(“%d %d”, &a,&b);
printf(“The value of a and b before swapping %d %d”, a, b);
temp = a;
a = b;
b = temp;
printf(“The value of a and b after swapping %d %d”, a, b);
}

12. To convert degree Celsius to Fahrenheit

#include<stdio.h>
main()
{
float celsius, fahrenheit ; /* declaration part */
printf(“Enter the value for celsius”);
scanf(“%f”, &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf(“The temperature in fahrenheit is %f”, fahrenheit);
}

13. To find area and perimeter of a rectangle

#include<stdio.h>
main()
{
int length,breadth; /* declaration part */
printf(“Enter the value for length and breadth”);
scanf(“%d %d”, &length,&breadth);
printf(“The area of rectangle is: %d”,(length * breadth));
printf(“The perimeter of rectangle is: %d”, 2 * (length + breadth));

14. To find square root of a number

#include<stdio.h>
#include<math.h>
main()
{
int a , res;
printf(“enter the value of a”);
scanf(“%d”,&a);
res = sqrt(a);
printf(“the square root of a is %d”, res);
}

15. To find ASCII value of a character

#include<stdio.h>
main()
{
char c;
printf(“enter the character”);
scanf(“%c”, c);
printf(“ASCII value of %c = %d”,c,c);
}

16. To find area of triangle

#include<stdio.h>
main()
{
float b,h,area;
printf(“enter the values of b and h”);
scanf(“%f %f”,&b , &h);
aea = (b * h)/2;
printf(“the area of a triangle is %f”, area);
}

17. To work with all the functions in math.h header file

#include<stdio.h>
main()
{
int x;
printf(“enter the values of x”);
scanf(“%d”, &x);
printf(“The sin value of x is %d”, sin(x));
printf(“The cos value of x is %d”, cos(x));
printf(“The tan value of x is %d”, tan(x));
}
18. To Round-up and Round-down the value
#include<stdio.h>
main()
{
foat x = 4.7;
printf(“The Round-up value of x is %f”, ceil(x));
printf(“The Round-down value of x is %f”, floor(x));
}

19. To find the average of two numbers

#include<stdio.h>
main()
{
int a, b;
printf(“enter the value of a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
int avg=(a+b)/2;
printf(“the average of two values is %d”,avg);
}

20. To convert total days to year, month and days


#include<stdio.h>
main()
{
int d,y,m,nd;
printf(“enter the number of days”);
scanf(“%d”,&d);
y=d/365;
d=d%365;
m=d/30;
nd==d%30;
printf(“%d years,%d month,%d days”,y,m,nd);
}

You might also like