Tuesday 18 December 2018

C Program with Output


C Programming By Ramesh Mahato  

WAP TO PERFORM INPUT/OUTPUT OF ALL BASIC DATA TYPES:

1. /**
2.  * C program to demonstrate input output of primitive data types
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     /*
10.                * Declare all primitive and derived types
11.                */
12.               char charVal;
13.               unsigned char uCharVal;
14.            
15.               short shortVal;
16.               unsigned short uShortVal;
17.            
18.               int intVal;
19.               unsigned int uIntVal;
20.            
21.               long longVal;
22.               unsigned long uLongVal;
23.            
24.               long long longLongVal;
25.               unsigned long long uLongLongVal;
26.            
27.               float floatVal;
28.               double doubleVal;
29.               long double longDoubleVal;
30.            
31.               /*
32.                * Read input in each type
33.                */
34.               printf("Enter a character: ");
35.               charVal = getchar();
36.               getchar(); // <-- Dummy getchar() to capture enter
37.            
38.               printf("Enter another character: ");
39.               uCharVal = getchar();
40.               getchar(); // <-- Dummy getchar() to capture enter
41.            
42.               printf("Enter a signed short value: ");
43.               scanf("%hi", &shortVal);
44.            
45.               printf("Enter an unsigned short value: ");
46.               scanf("%hu", &uShortVal);
47.            
48.               printf("Enter an signed integer value: ");
49.               scanf("%d", &intVal);
50.            
51.               printf("Enter an unsigned integer value: ");
52.               scanf("%lu", &uIntVal);
53.            
54.               printf("Enter a signed long value: ");
55.               scanf("%ld", &longVal);
56.            
57.               printf("Enter an unsigned long value: ");
58.               scanf("%lu", &uLongVal);
59.            
60.               printf("Enter a signed long long value: ");
61.               scanf("%lld", &longLongVal);
62.            
63.               printf("Enter an unsigned long long value: ");
64.               scanf("%llu", &uLongLongVal);
65.            
66.               printf("Enter a float value: ");
67.               scanf("%f", &floatVal);
68.            
69.               printf("Enter a double value: ");
70.               scanf("%lf", &doubleVal);
71.            
72.               printf("Enter a long double value: ");
73.               scanf("%Lf", &longDoubleVal);
74.            
75.            
76.               /*
77.                * Print the value of all variable
78.                */
79.               printf("\nYou entered character: '%c' \n", charVal);
80.               printf("You entered unsigned character: '%c' \n\n", uCharVal);
81.            
82.               printf("You entered signed short: %hi \n", shortVal);
83.               printf("You entered unsigned short: %hu \n\n", uShortVal);
84.            
85.               printf("You entered signed int: %d \n", intVal);
86.               printf("You entered unsigned int: %lu \n\n", uIntVal);
87.            
88.               printf("You entered signed long: %ld \n", longVal);
89.               printf("You entered unsigned long: %lu \n\n", uLongVal);
90.            
91.               printf("You entered signed long long: %lld \n", longLongVal);
92.               printf("You entered unsigned long long: %llu \n\n", uLongLongVal);
93.            
94.               printf("You entered float: %f \n", floatVal);
95.               printf("You entered double: %lf \n", doubleVal);
96.               printf("You entered long double: %Lf \n", longDoubleVal);
97.            
98.               return 0;
99.           }

Output:


Enter a character: C
Enter another character: P
Enter a signed short value: -32768
Enter an unsigned short value: 65535
Enter an signed integer value: -2147483648
Enter an unsigned integer value: 4294967295
Enter a signed long value: -2147483648
Enter an unsigned long value: 4294967295
Enter a signed long long value: -9223372036854775808
Enter an unsigned long long value: 18446744073709551615
Enter a float value: 1.28766
Enter a double value: 10.915074
Enter a long double value: 100.12345
 
You entered character: 'C'
You entered unsigned character: 'P'
 
You entered signed short: -32768
You entered unsigned short: 65535
 
You entered signed int: -2147483648
You entered unsigned int: 4294967295
 
You entered signed long: -2147483648
You entered unsigned long: 4294967295
 
You entered signed long long: -9223372036854775808
You entered unsigned long long: 18446744073709551615
 
You entered float: 1.287660
You entered double: 10.915074
You entered long double: 100.123450

 


 


WAP TO ENTER TWO NUMBERS AND FIND THEIR SUM


1st Way:


1. /**
2.  * C program to find sum of two numbers
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num1, num2, sum;
10.            
11.               /*
12.                * Read two numbers from user
13.                */
14.               printf("Enter first number: ");
15.               scanf("%d", &num1);
16.               printf("Enter second number:");
17.               scanf("%d", &num2);
18.            
19.               /* Adding both number is simple and fundamental */
20.               sum = num1 + num2;
21.            
22.               /* Prints the sum of two numbers */
23.               printf("Sum of %d and %d = %d", num1, num2, sum);
24.            
25.               return 0;
26.           }

 


 


2nd Way:


1. /**
2.  * C program to find sum of two numbers
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int num1, num2, sum;
10.            
11.               /*
12.                * Input two numbers from user
13.                */
14.               printf("Enter any two numbers : ");
15.               scanf("%d%d", &num1, &num2);
16.            
17.               sum = num1 + num2;
18.            
19.               /* Prints the sum of two numbers */
20.               printf("Sum of %d and %d = %d\n", num1, num2, sum);
21.            
22.               return 0;
23.           }

Output:


Enter any two numbers : 12
        20
        Sum of 12 and 20 = 32
 
 

WAP ENTER TWO NUMBERS TO PERFORM ALL ARITHMETIC OPERATIONS


1.  
2. /**
3.  * C program to perform all arithmetic operations
4.  */
5.  
6. #include <stdio.h>
7.  
8. int main()
9. {
10.               int num1, num2;
11.               int sum, sub, mult, mod;
12.               float div;
13.            
14.               /*
15.                * Input two numbers from user
16.                */
17.               printf("Enter any two numbers: ");
18.               scanf("%d%d", &num1, &num2);
19.            
20.               /*
21.                * Perform all arithmetic operations
22.                */ 
23.               sum = num1 + num2;
24.               sub = num1 - num2;
25.               mult = num1 * num2;
26.               div = (float)num1 / num2;
27.               mod = num1 % num2;
28.            
29.               /*
30.                * Print result of all arithmetic operations
31.                */
32.               printf("SUM = %d\n", sum);
33.               printf("DIFFERENCE = %d\n", sub);
34.               printf("PRODUCT = %d\n", mult);
35.               printf("QUOTIENT = %f\n", div);
36.               printf("MODULUS = %d", mod);
37.            
38.               return 0; 
39.           }

Output:


Enter any two numbers : 20 10
SUM = 30
DIFFERENCE = 10
PRODUCT = 200
QUOTIENT = 2.000000
MODULUS = 0
 
 
 

WAP TO FIND PERIMETER OF A RECTANGLE


1. /**
2.  * C program to find perimeter of rectangle
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float length, width, perimeter;
10.            
11.               /*
12.                * Input length and width of rectangle from user
13.                */
14.               printf("Enter length of the rectangle: ");
15.               scanf("%f", &length);
16.               printf("Enter width of the rectangle: ");
17.               scanf("%f", &width);
18.            
19.               /* Calculate perimeter of rectangle */
20.               perimeter = 2 * (length + width);
21.            
22.               /* Print perimeter of rectangle */
23.               printf("Perimeter of rectangle = %f units ", perimeter);
24.            
25.               return 0;
26.           }

Output:


Output
 
Enter length of the rectangle: 5
Enter width of the rectangle: 10
Perimeter of rectangle = 30.000000
 
 

WAP TO ENTER LENGTH AND BREADTH OF A RECTANGLE AND FIND ITS AREA


1. /**
2.  * C program to find area of rectangle
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float length, width, area;
10.            
11.               /*
12.                * Input length and width of rectangle
13.                */
14.               printf("Enter length of rectangle: ");
15.               scanf("%f", &length);
16.               printf("Enter width of rectangle: ");
17.               scanf("%d", &width);
18.            
19.               /* Calculate area of rectangle */
20.               area = length * width;
21.            
22.               /* Print area of rectangle */
23.               printf("Area of rectangle = %f sq. units ", area);
24.            
25.               return 0;
26.           }
27.            

Output:


Enter length of rectangle: 5
Enter width of rectangle: 10
Area of rectangle = 50.000000 sq. units
 
 

WAP TO ENTER RADIUS OF A CIRCLE AND FIND ITS DIAMETER,CIRCUMFERENCE AND AREA


1. /**
2.  * C program to calculate diameter, circumference and area of circle
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float radius, diameter, circumference, area;
10.            
11.               /*
12.                * Input radius of circle from user
13.                */
14.               printf("Enter radius of circle: ");
15.               scanf("%f", &radius);
16.            
17.               /*
18.                * Calculate diameter, circumference and area
19.                */
20.               diameter = 2 * radius;
21.               circumference = 2 * 3.14 * radius;
22.               area = 3.14 * (radius * radius);
23.            
24.               /*
25.                * Print all results
26.                */
27.               printf("Diameter of circle = %.2f units \n", diameter);
28.               printf("Circumference of circle = %.2f units \n", circumference);
29.               printf("Area of circle = %.2f sq. units ", area);
30.            
31.               return 0;
32.           }

Output:


Enter radius of circle: 10
Diameter of circle = 20.00 units
Circumference of circle = 62.79 units
Area of circle = 314.00 sq. units
 
 

WAP TO ENTER IN CENTIMETER AND CONVERT IT INTO METER AND KILOMETER


1. /**
2.  * C program to convert centimeter into meter and kilometer
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float cm, meter, km;
10.            
11.               /* Input length in centimeter from user */
12.               printf("Enter length in centimeter: ");
13.               scanf("%f", &cm);
14.            
15.               /* Convert centimeter into meter and kilometer */
16.               meter = cm / 100.0;
17.               km    = cm / 100000.0;
18.            
19.               printf("Length in Meter = %.2f m \n", meter);
20.               printf("Length in Kilometer = %.2f km", km);
21.            
22.               return 0;
23.           }

Output:


Enter length in centimeter: 1000
Length in Meter = 10.00 m
Length in Kilometer = 0.01 km
 
 

WAP TO ENTER TEMPERATURE IN CELSIUS AND CONVERT IT INTO FAHRENHEIT


1. /**
2.  * C program to convert temperature from degree celsius to fahrenheit
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float celsius, fahrenheit;
10.            
11.               /* Input temperature in celsius */
12.               printf("Enter temperature in Celsius: ");
13.               scanf("%f", &celsius);
14.            
15.               /* celsius to fahrenheit conversion formula */
16.               fahrenheit = (celsius * 9 / 5) + 32;
17.            
18.               printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
19.            
20.               return 0;
21.           }

Output:


Enter temperature in Celsius: 100
        100 Celsius = 212.00 Fahrenheit
 
 

WAP TO ENTER TEMPERATURE IN FAHRENHEIT AND CONVERT TO CELSIUS


1. /**
2.  * C program to convert temperature from degree fahrenheit to celsius
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float celsius, fahrenheit;
10.            
11.               /* Input temperature in fahrenheit */
12.               printf("Enter temperature in Fahrenheit: ");
13.               scanf("%f", &fahrenheit);
14.            
15.               /* Fahrenheit to celsius conversion formula */
16.               celsius = (fahrenheit - 32) * 5 / 9;
17.            
18.               /* Print the value of celsius */
19.               printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
20.            
21.               return 0;
22.           }

Output:


Enter temperature in Fahrenheit: 205
205.00 Fahrenheit = 96.11 Celsius
 
 
 
 

WAP TO CONVERT DAYS INTO YEARS,WEEKS AND DAYS:


1. /**
2.  * C program to convert days in to years, weeks and days
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int days, years, weeks;
10.            
11.               /* Input total number of days from user */
12.               printf("Enter days: ");
13.               scanf("%d", &days);
14.            
15.               /* Conversion */
16.               years = (days / 365);   // Ignoring leap year
17.               weeks = (days % 365) / 7;
18.               days  = days - ((years * 365) + (weeks * 7));
19.            
20.               /* Print all resultant values */
21.               printf("YEARS: %d\n", years);
22.               printf("WEEKS: %d\n", weeks);
23.               printf("DAYS: %d", days);
24.            
25.               return 0;
26.           }

Output:


Enter days: 373
YEARS: 1
WEEKS: 1
DAYS: 1
 
 
 
 
 
 

WAP TO FIND POWER OF ANY NUMBER X^Y


1. /**
2.  * C program to find power of any number
3.  */
4.  
5. #include <stdio.h>
6. #include <math.h> // Used for pow() function
7.  
8. int main()
9. {
10.               double base, expo, power;
11.            
12.               /* Input two numbers from user */
13.               printf("Enter base: ");
14.               scanf("%lf", &base);
15.               printf("Enter exponent: ");
16.               scanf("%lf", &expo);
17.            
18.               /* Calculates base^expo */
19.               power = pow(base, expo);
20.            
21.               printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);
22.            
23.               return 0;
24.           }

Output:


Enter base: 5
Enter exponent: 3
5 ^ 3 = 125
 
 

WAP TO ENTER ANY NUMBER AND CALCULATE ITS SQUARE ROOT


1. /**
2.  * C program to find square root of a number
3.  */
4.  
5. #include <stdio.h>
6. #include <math.h>
7.  
8. int main()
9. {
10.               double num, root;
11.            
12.               /* Input a number from user */
13.               printf("Enter any number to find square root: ");
14.               scanf("%lf", &num);
15.            
16.               /* Calculate square root of num */
17.               root = sqrt(num);
18.            
19.               /* Print the resultant value */
20.               printf("Square root of %.2lf = %.2lf", num, root);
21.            
22.               return 0;
23.           }

Output:


Enter any number to find square root: 144
Square root of 144.00 = 12.00
 
 

WAP ENTER TWO ANGLES OF A TRIANGLE AND FIND THE THIRD ANGLE


1. /**
2.  * C program to find all angles of a triangle if two angles are given
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     int a, b, c;
10.            
11.               /* Input two angles of the triangle */
12.               printf("Enter two angles of triangle: ");
13.               scanf("%d%d", &a, &b);
14.            
15.               /* Compute third angle */
16.               c = 180 - (a + b);
17.            
18.               /* Print value of the third angle */
19.               printf("Third angle of the triangle = %d", c);
20.            
21.               return 0;
22.           }

Output:


Enter two angles of triangle: 60 30
Third angle of the triangle = 90
 
 
 

WAP TO ENTER BASE AND HEIGHT OF A TRIANGLE AND FIND ITS AREA


1. /** 
2.  * C program to find area of a triangle if base and height are given
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float base, height, area;
10.            
11.               /* Input base and height of triangle */
12.               printf("Enter base of the triangle: ");
13.               scanf("%f", &base);
14.               printf("Enter height of the triangle: ");
15.               scanf("%f", &height);
16.            
17.               /* Calculate area of triangle */
18.               area = (base * height) / 2;
19.            
20.               /* Print the resultant area */
21.               printf("Area of the triangle = %.2f sq. units", area);
22.            
23.               return 0;
24.           }


Output:


Enter base of the triangle: 10
Enter height of the triangle: 15
Area of the triangle = 75.00 sq. units
 
 
 

WAP TO CALCULATE AREA OF AN EQUILATERAL TRIANGLE


1. /**
2.  * C program to find area of an equilateral triangle
3.  */
4.  
5. #include <stdio.h>
6. #include <math.h> // Used for sqrt() function
7.  
8. int main()
9. {
10.               float side, area;
11.            
12.               /* Input side of equilateral triangle */
13.               printf("Enter side of an equilateral triangle: ");
14.               scanf("%f", &side);
15.            
16.               /* Calculate area of equilateral triangle */
17.               area = (sqrt(3) / 4) * (side * side);
18.            
19.               /* Print resultant area */
20.               printf("Area of equilateral triangle = %.2f sq. units", area);
21.            
22.               return 0;
23.           }

Output:


utput
 
Enter side of an equilateral triangle: 75
Area of equilateral triangle = 2435.69 sq. units
 
 

WAP TO ENTER MARKS OF FIVE SUBJECTS AND CULCULATE TOTAL,AVERAGE AND PERCENTAGE


1. /**
2.  * C program to calculate total, average and percentage of five subjects
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float eng, phy, chem, math, comp; 
10.               float total, average, percentage;
11.            
12.               /* Input marks of all five subjects */
13.               printf("Enter marks of five subjects: \n");
14.               scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
15.            
16.               /* Calculate total, average and percentage */
17.               total = eng + phy + chem + math + comp;
18.               average = total / 5.0;
19.               percentage = (total / 500.0) * 100;
20.            
21.               /* Print all results */
22.               printf("Total marks = %.2f\n", total);
23.               printf("Average marks = %.2f\n", average);
24.               printf("Percentage = %.2f", percentage);
25.            
26.               return 0;
27.           }

Output:


Enter marks of five subjects: 
95
76
85
90
89
Total marks = 435.00
Average marks = 87.00
Percentage = 87.00
 
 
 

WAP TO ENTER MARKS OF FIVE SUBJECTS AND CULCULATE TOTAL,AVERAGE AND PERCENTAGE


1. /**
2.  * C program to calculate total, average and percentage of five subjects
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float eng, phy, chem, math, comp; 
10.               float total, average, percentage;
11.            
12.               /* Input marks of all five subjects */
13.               printf("Enter marks of five subjects: \n");
14.               scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
15.            
16.               /* Calculate total, average and percentage */
17.               total = eng + phy + chem + math + comp;
18.               average = total / 5.0;
19.               percentage = (total / 500.0) * 100;
20.            
21.               /* Print all results */
22.               printf("Total marks = %.2f\n", total);
23.               printf("Average marks = %.2f\n", average);
24.               printf("Percentage = %.2f", percentage);
25.            
26.               return 0;
27.           }

Output:


Enter marks of five subjects: 
95
76
85
90
89
Total marks = 435.00
Average marks = 87.00
Percentage = 87.00
 
 
 

WAP TO ENTER P,T,R AND CALCULATE SIMPLE INTEREST


1. /**
2.  * C program to calculate simple interest
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float principle, time, rate, SI;
10.            
11.               /* Input principle, rate and time */
12.               printf("Enter principle (amount): ");
13.               scanf("%f", &principle);
14.            
15.               printf("Enter time: ");
16.               scanf("%f", &time);
17.            
18.               printf("Enter rate: ");
19.               scanf("%f", &rate);
20.            
21.               /* Calculate simple interest */
22.               SI = (principle * time * rate) / 100;
23.            
24.               /* Print the resultant value of SI */
25.               printf("Simple Interest = %f", SI);
26.            
27.               return 0;
28.           }

Output:


Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Simple Interest = 129.600006
 
 
 

WAP TO ENTER P,T,R AND CALCULATE COMPOUND INTEREST


1. /**
2.  * C program to calculate Compound Interest
3.  */
4.  
5. #include <stdio.h>
6. #include <math.h>
7.  
8. int main()
9. {
10.               float principle, rate, time, CI;
11.            
12.               /* Input principle, time and rate */
13.               printf("Enter principle (amount): ");
14.               scanf("%f", &principle);
15.            
16.               printf("Enter time: ");
17.               scanf("%f", &time);
18.            
19.               printf("Enter rate: ");
20.               scanf("%f", &rate);
21.            
22.               /* Calculate compound interest */
23.               CI = principle* (pow((1 + rate / 100), time));
24.            
25.               /* Print the resultant CI */
26.               printf("Compound Interest = %f", CI);
27.            
28.               return 0;
29.           }

Output:


 
 
Output
 
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Compound Interest = 1333.099243
 
 
 

WAP TO ENTER P,T,R AND CALCULATE SIMPLE INTEREST


1. /**
2.  * C program to calculate simple interest
3.  */
4.  
5. #include <stdio.h>
6.  
7. int main()
8. {
9.     float principle, time, rate, SI;
10.            
11.               /* Input principle, rate and time */
12.               printf("Enter principle (amount): ");
13.               scanf("%f", &principle);
14.            
15.               printf("Enter time: ");
16.               scanf("%f", &time);
17.            
18.               printf("Enter rate: ");
19.               scanf("%f", &rate);
20.            
21.               /* Calculate simple interest */
22.               SI = (principle * time * rate) / 100;
23.            
24.               /* Print the resultant value of SI */
25.               printf("Simple Interest = %f", SI);
26.            
27.               return 0;
28.           }

Output:


Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Simple Interest = 129.600006
 
 


No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint