#Include #Include : Page 1 of 40
#Include #Include : Page 1 of 40
Page 1 of 40
1: /* Program To Find Volume Of Cuboid */
2: #include <stdio.h>
3:
4: int main() /* int main means the main function returns an int value */
5: {
6: float l = 25, b = 4, h = 3.125, vol = 0;
7: /* l for length, b for breadth, h for height, vol for volume */
8:
9: vol = l * b * h;
10: printf("Volume of Cuboid = %f\n", vol);
11:
12: return 0;
13: }
Page 2 of 40
1: /* Program To Find Volume Of Cylinder */
2: #include <stdio.h>
3: #include <math.h>
4:
5: #define PI 22.0/7.0
6:
7: int main()
8: {
9: float r = 7.0, h = 1.0, vol = 0.0;
10: /* r for Radius, h for Height, vol for Volume */
11:
12: vol = PI * r * r * h;
13:
14: printf("Volume Of Cylinder = %f\n", vol);
15: }
16:
Page 3 of 40
1: /* Program to declare and assign values to vañables */
2: #include <stdio.h>
3:
4: int main()
5: {
6: /* Variable declaration */
7: int a;
8: char ch;
9: float P;
10: double d;
11: char name[10] = "CV RAMAN";
12:
13: /* Assigning values to variables */
14: a = 125;
15: ch = 'R';
16: P = 6677.88;
17: d = 995533.234;
18:
19: /* Printing variable values */
20: printf("int a = %d\n", a);
21: printf("char ch = %c\n", ch);
22: printf("float P = %f\n", P);
23: printf("double d = %lf\n", d);
24: printf("string name = %s\n", name);
25:
26: return 0;
27: }
28:
Page 4 of 40
1: /* Program To Find Volume Of Sphere */
2: # include<stdio.h>
3:
4: # define PI 22.0/7.0
5:
6: int main()
7: {
8: float r, vol = 0;
9: /* r for Radius, vol for Volume */
10:
11: printf("Enter Radius Value to find volume of Sphere");
12: scanf("%f", &r);
13:
14: vol = 4.0/3.0 * PI * r * r * r;
15:
16: printf("Volume Of Sphere = %f\n",vol);
17: }
Page 5 of 40
1: /* Program To Find Total & Average For Given MPC Marks */
2: #include <stdio.h>
3:
4: int main()
5: {
6: int m = 96, p = 94, c = 90, total = 0;
7: float avg = 0;
8:
9: total = m + p + c;
10: avg = (float)total / 3;
11: /* Total value (int) is converted to float value */
12: /* Converting one data type value into another data type value
13: into another data type value is called type coversion (or) type casting */
14:
15: printf("Total Marks Of MPC Subject = %d\n", total);
16: printf("Average = %f\n", avg);
17:
18: return 0;
19: }
20:
Page 6 of 40
1: /* Program to display student data */
2: #include <stdio.h>
3:
4: int main()
5: {
6: /* Variable initialization */
7: int stid = 1234;
8: char name[15] = "DENNIS RITCHIE";
9: char gender = 'M';
10: char Course[15] = "M";
11: float fee = 65000.00;
12: double avg = 96.56;
13:
14: printf("Student Information: \n");
15: printf("Id = %d\n", stid);
16: printf("Name = %s\n", name);
17: printf("Gender = %c\n", gender);
18: printf("Course = %s\n", Course);
19: printf("Fees = %.2f\n", fee);
20: printf("Average = %.2f\n", avg);
21:
22: return 0;
23: }
24:
Page 7 of 40
1: /* program to convert centigrade to fahrenheit and fahrenheit to centigrade */
2: #include <stdio.h>
3:
4: int main()
5: {
6: float c=30,f=90, cent=0, fahr=0;
7:
8: cent = (f-32)*5/9;
9: fahr = (c*9/5)+32;
10:
11: printf("Fahrenheit %f to Centigrade %f \n",f,cent);
12: printf("Centigrade %f to Fahrenheit %f \n",c,fahr);
13: }
Page 8 of 40
1: /* Program To Swap 2 VariablesWithout Using 3rd Variables */
2: # include<stdio.h>
3:
4: int main()
5: {
6: int a = 100, b = 200;
7:
8: printf("Before Swap: a = %d\t, b = %d\n",a,b);
9: a = a + b;
10: b = a - b;
11: a = a - b;
12:
13: printf("After Swap: a = %d\t, b = %d\n",a,b);
14: }
Page 9 of 40
1: /* Program to Calculate speed of vehicle */
2: #include <stdio.h>
3:
4: void main()
5: {
6: float dist-320;
7: time = 3.5°;
8: float speed =0;
9: Speed dist/time;
10: Print of ("Distance = %2f KMS \n", dist);
11: Printf ("Time = % 2f Hours \n", time);
12: Print of ("speed = %2f KM/Hr \n", speed);
13: }
Page 10 of 40
1: /* Program To Finc Area & Perimeter Of Rectangle */
2: #include <stdio.h>
3:
4: int main()
5: {
6: float l = 25, b = 12, area = 0, peri = 0;
7:
8: area = l * b;
9: peri = 2 * (l + b);
10:
11: printf("Area = %f\n", area);
12: printf("Perimeter = %f\n", peri);
13:
14: return 0;
15: }
16:
Page 11 of 40
1: /* Program To Find Area And Perimeter Of Square */
2: #include <stdio.h>
3: int main()
4: {
5: float side, area = 0, Peri = 0;
6:
7: printf("Enter Side Value To Find Area & Perimeter Of Square");
8: scanf("%f\n", & side);
9:
10: area = side * side;
11: Peri = 4 * side;
12:
13: printf("Area = %f\n", area);
14: printf("Perimeter = % f\n", Peri);
15: }
Page 12 of 40
1: /* program to find calculate area of Right-angled triangle */
2: #include <stdio.h>
3:
4: int main()
5: {
6: float b, h, area=0;
7: b=16, h=20;
8:
9: area = 1.0/2.0 * b * h;
10:
11: printf("Area = %f\n", area);
12: }
Page 13 of 40
1: /* Program To Find Area Of Rhombus */
2: #include<stdio.h>
3:
4: int main()
5: {
6: float a = 15, b = 18, area = 0;
7:
8: area = 0.5 * a * b;
9:
10: printf("Area = %f\n", area);
11: }
Page 14 of 40
1: /* Program To Find Calculate Empolyee Bonus & Net Salary */
2: #include<stdio.h>
3:
4: int main()
5: {
6: float salary = 28000, bonus = 0, netsal = 0;
7:
8: bonus = salary * 0.10;
9: netsal = salary + bonus;
10:
11: printf("Salary = %f\n", salary);
12: printf("Bonus = %f\n", bonus);
13: printf("Net Salary = %f\n", netsal);
14: }
Page 15 of 40
1: /* program using getchar and putchar functions */
2: #include <stdio.h>
3: #include <conio.h>
4: int main()
5: {
6: char ch;
7: /* clrscr(); */
8:
9: printf("Enter a character \n");
10: ch=getchar();
11:
12: printf("\n Input character is :");
13: putchar(ch);
14:
15: printf("Enter a character \n");
16: ch=getchar();
17:
18: printf("\n Input character is :");
19: putchar(ch);
20:
21: putchar('\n');
22: putchar('A');
23: putchar('\t');
24: putchar(':');
25: putchar('\t');
26: putchar('9');
27: return 0;
28: }
29:
Page 16 of 40
1: /* Program Using %S Specification In Scanf To Read Sting */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: char name[30];
8: printf("Enter a String \n ");
9: scanf("%s", & name);
10:
11: printf("\n The Entered String:%s", name);
12: return 0;
13: }
Page 17 of 40
1: /* program using %[characters] specification */
2: #include<stdio.h>
3: #include <conio.h>
4:
5: int main()
6: {
7: char name[50];
8:
9: printf("\n Enter a String with Uppercase and spaces \n");
10: scanf("%[A-Z,' ']",&name);
11: /* reads characters A-Z comma(,) and spaces only */
12:
13: printf("\n The Entered String: %s", name);
14:
15: return 0;
16: }
17:
Page 18 of 40
1: /* program to display character output */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: char ch = 'A';
8: /* clrscr(); */
9: printf("%c \n", ch);
10: printf("%3c \n", ch);
11: printf("%5c \n", ch);
12: printf("%-3c \n", ch);
13: printf("%03c \n", ch);
14: printf("%05c \n", ch);
15: return 0;
16: }
Page 19 of 40
1: /* program to display output of decimal integer */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: int m = 12345;
8: /* clrscr */
9: printf("%d \n", m);
10: printf("%3d \n", m); /* 12345 is 5 digits & width is 3d only */
11: printf("%5d \n", m);
12: printf("%10d \n", m);
13: printf("%010d \n", m); /* padding blankspace with zeros */
14: printf("%-10d \n", m); /* left justified */
15:
16: return 0;
17: }
Page 20 of 40
1: /* program to display output of float values */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: float m = 98.7654;
8: /* clrscr */
9:
10: printf("%f\n", m);
11: printf("%7.4f \n", m);
12: printf("7.2f \n", m); /* randed to 2 decimal places */
13: printf("*,*f \n", 7, 2, m);
14: printf("07.2f \n", m); /* padding blanks with zeroes */
15: printf("-7.2f \n", m); /* left justified */
16:
17: printf("\n Exponential format \n");
18: printf("%10.2e \n", m);
19: printf("%-10.2e \n", m);
20: printf("%10.2E \n", m);
21: printf("%e \n", m);
22: printf("%12.4e \n", -m);
23:
24: return 0;
25: }
Page 21 of 40
1: /* program to display stirng */
2: #include<stdio.h>
3: #include<conio.h>
4: int main()
5: {
6: char name[20] = "DENNIS RITCHIE";
7: /* clrscr(); */
8: printf("%s \n", name);
9: printf("%8s \n", name); /* width is les than name */
10: printf("%20s \n", name);
11: printf("%20.10s \n", name); /* first 10 characters of name */
12: printf("%-20.10s \n", name);
13: /* first 10 characters with left justified */
14: printf("%5s \n", name); /* first 5 characters of name */
15:
16: return 0;
17: }
Page 22 of 40
1: /* program using %c specification */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: char ch;
8: /* clrscr */
9: printf("Enter An Alphabet Or Digit Or Symbol: \n");
10: scanf("%c", &ch);
11: printf("character = %c \n", ch);
12:
13: return 0;
14: }
Page 23 of 40
1: /* program to read signed integers */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: int a; long int b; short int c;
8: /*clrscr */
9: printf("Enter an intger: \n");
10: scanf("%d", &a);
11: printf("Enter a Long Integer: \n");
12: scanf("%ld", &b);
13: printf("Enter a Short Integer: \n");
14: scanf("%hi", c);
15: printf("integer a = %d \n", a);
16: printf("long integer b = % ld \n", b);
17: printf("short integer c = %hi \n", c);
18:
19: return 0;
20: }
Page 24 of 40
1: /* program to read float, double */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: float p; double q;
8: /* clrscr(); */
9: printf("Enter a Float Value: \n");
10: scanf("%f", &p);
11: printf("Enter a Double Float Value: \n");
12: scanf("%f", &q);
13: printf("float p = %f \n", p);
14: printf("double q = %lf \n", q);
15:
16: return 0;
17: }
Page 25 of 40
1: /* program using scanf with width specifer for string */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: char name[20];
8: /* clrscr(); */
9: printf("Enter a Name: \n");
10: scanf("%8s", &name);
11: printf("name = %s\n", name);
12:
13: return 0;
14: }
Page 26 of 40
1: /* scanf with width specifiers for reading integers */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: int a, b, c;
8: printf("Enter a, b values: \n");
9: scanf("%3d%2d", &a, &b);
10: scanf("%d", &c);
11: printf("a = %d \t b =%d \t c = %d \n", a, b, c);
12:
13: return 0;
14: }
Page 27 of 40
1: /* reading integers */
2: #include<stdio.h>
3: int main()
4: {
5: int a, b;
6: printf("Enter a, b values: \n");
7: scanf("%d%d", &a, &b);
8: printf("a = %d \t b = %d \n", a, b);
9:
10: return 0;
11: }
Page 28 of 40
1: /* program to display integer x in different forms & conversions */
2: #include<stdio.h>
3:
4: int main()
5: {
6: int x = 100;
7: /* x in different forms */
8: printf("Decimal: x = %d \n", x);
9: printf("Integer: x = %i \n", x);
10: printf("Octal: x = %o \n", x);
11: printf("Hexa Decimal: x = %x \n", x);
12: /* Converting x into float, double, long int */
13: printf("x as float x = %f \n", (float)(x));
14: printf("x as double float x = %lf \n", (double)(x));
15: printf("x as long int x = %ld \n", (long int)(x));
16: return 0;
17: }
Page 29 of 40
1: /* program using float x in different forms */
2: #include<stdio.h>
3:
4: int main()
5: {
6: float x = 12.3456;
7: printf("float x = %f \n", x);
8: printf("exponential x = %e \n", x);
9: printf("float general form x = %g \n", x);
10: printf("double float x = %lf \n", x);
11: printf("decimal int x = %d \n", x);
12:
13: return 0;
14: }
Page 30 of 40
1: /* program to calculate compuound interest */
2: #include <stdio.h>
3: #include<conio.h>
4: #include<math.h>
5:
6: int main()
7: {
8: float p,r, t, ci = 0.0;
9:
10: printf("Enter Principal Amount, Rate, Time: \n ");
11: scanf("%f%f%f", &p, &r, &t);
12:
13: ci = p * pow((1+r/100), t) - p;
14:
15: printf("\n Principal Amount = Rs. %2f", p);
16: printf("Rate of Intrest = %2f%%", r);
17: printf("\n Time = %2f years", t);
18: printf("\n Compound Interest = Rs. %2f", ci);
19: printf("\n Total Amount = Rs. %2f", p+ci);
20: return 0;
21:
22: }
Page 31 of 40
1: 34/* program to find square root of given number */
2: #include<math.h>
3: #include<stdio.h>
4: #include<conio.h>
5:
6: int main()
7: {
8: double x, sqrtval = 0.0;
9:
10: printf("Enter a Number: \n");
11: scanf("%lf", &x);
12: sqrtval=sqrt(x);
13: printf("\n square root of %lf = %f \n", x, sqrtval);
14:
15: return 0;
16: }
Page 32 of 40
1: /* Program to find the simple interest */
2:
3: #include <stdio.h>
4: #include <conio.h>
5:
6: int main()
7: {
8: float p,r, t, si=0.0;
9: printf("Enter Principle Amount, Rate, Time \n");
10: scanf("%f%f%f", &p, &r, & t);
11: si = (p * t * r)/100;
12:
13: printf("In Principal Amount = Rs. %2f \n", p);
14: printf("Rate of Interest = %2f%% \n", r);
15: printf("Time = %.2f years \n", t);
16: printf("simple Interest = RS. %2f", si);
17:
18: return 0;
19: }
Page 33 of 40
1: /* Program of Power value for & given number */
2:
3: #include <math.h>
4: #include <stdio.h>
5:
6: int main()
7: {
8: double x, y, powval = 0.0; // Initialize powval to 0.0
9:
10: printf("Enter number and Power values: \n");
11: scanf("%lf %lf", &x, &y);
12: /* double Pow (double x, double x) /*
13: /* Pow function retums value x to the Power of y */
14: powval = pow(x, y);
15:
16: printf("\n %lf to the power of %lf = %lf \n", x, y, powval);
17:
18: return 0;
19: }
20:
Page 34 of 40
1: /* Program-using float x in different forms */
2: #include <stdio.h>
3:
4: int main()
5: {
6: float x=12.3456;
7:
8: printf("float x=% f \n", x);
9: printf("Exponential x=%e \n", x);
10: printf("float general form x = %g \n", x);
11: printf("double float x=% 1f\n",x);
12: printf("decimal int x=%d\n", (int)(x));
13:
14: return 0;
15: }
Page 35 of 40
1: /* C Program to demonstrate the difference b/w %i and %d Specifier */
2: #include <stdio.h>
3:
4: int main()
5: {
6: int a, b, c;
7: printf("Enter a value in decimal format: "); /* input: 12 */
8: scanf("%d", a);
9: printf("Enter b value in octal format: "); /*input : 012 */
10: scanf("%i", b);
11: printf ("Enter c value in hexadecimal format: "); /* ippat : 0 x 12 */
12: scanf("%i", &c);
13: printf("a = %; \t b=%i \t c=%i", a, b, c);
14: return 0;
15: }
Page 36 of 40
1: /*Program to find area of triangle */
2: #include <stdio.h>
3: #include <math.h>
4:
5: int main()
6: {
7: float a,b,c, s=0, area = 0;
8:
9: printf("Enter a, b, c values: \n");
10: scanf("%f %f %f",&a, &b, &c);
11:
12: s = (a+b+c)/2;
13:
14: area = (sqrt)(s*(s-a)*(s-b)*(s-c));
15: printf ("Area of Traingle = %f \n", area);
16: }
Page 37 of 40
1: /* program to use arithematic operators */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: int a = 21, b = 8;
8: float add = 0, sub = 0, mul = 0, div = 0,rem = 0;
9:
10: add = a + b;
11: sub = a - b;
12: mul = a * b;
13: div = a / b;
14: rem = a % b;
15:
16: printf("\n Addition = %d\n", add);
17: printf("\n Subtraction = %d\n", sub);
18: printf("\n Multiplication = %d\n", mul);
19: printf("\n Division = %d\n", div);
20: printf("\n Remainder = %d\n", rem);
21:
22: return 0;
23: }
Page 38 of 40
1: /* Relational operators example */
2: #include <stdio.h>
3: #include <conio.h>
4:
5: int main()
6: {
7: int a=10,b=8;
8: /* clrscr(); */
9:
10: printf("a>b=%d \n", !a>b);
11: printf("a<b=%d\n",a<b);
12: printf("a==b=%d \n",a==b);
13:
14: return 0;
15: }
Page 39 of 40
1: /* program using relational operators */
2: #include<stdio.h>
3: #include<conio.h>
4:
5: int main()
6: {
7: int a = 5, b = 5, c = 10;
8: /* clrscr(); */
9:
10: printf("%d == %d is %d \n", a, b, a == b); /* 1 */
11: printf("%d == %d is %d \n",a, c, a == c); /* 0 */
12: printf("%d > %d is %d \n", a, b, a > b); /* 0 */
13: printf("%d > %d is %d \n", a, c, a > c); /* 0 */
14: printf("%d < %d is %d \n", a, c, a > b); /* 0 */
15: printf("%d < %d is %d \n", a, b, a < b); /* 0 */
16: printf("%d == %d is %d \n", a, b, a == b);
17: printf("%d == %d is %d \n", a, c, a == c);
18: printf("%d > %d is %d \n", a, b, a > b);
19: printf("%d > %d is %d\n", a, c, a > c);
20: printf("%d < %d is %d \n", a, b, a < b);
21: printf("%d < %d is %d\n", a, c, a < c);
22: printf("%d != %d is %d \n", a, b, a = b);
23: printf("%d != Xd is %d\n", a, c, a = c);
24: printf("%d >= %d is %d \n", a, b, a >= b);
25: printf("%d >= %d is %d \n", a, c, a >= c);
26: printf("%d <= %d is %d \n", a, b, a <= b);
27: printf("%d <= %d is %d\n", a, c, a <= c);
28:
29: return 0;
30: }
Page 40 of 40