Professional Documents
Culture Documents
C Programming File
C Programming File
C Programming File
C Programming File
PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE HARGUN AMRIT
[Type the abstract of the document here. The abstract is typically a short summary of the contents of the document. Type the abstract of the document here. The abstract is typically a short summary of the contents of the document.]
Index
P1. Program to print a sentence in output screen 2 P2. Program to enter two numbers and check which one is greater.. 2 P3. Program to add two integers.. 3 P4. Program for showing one integer value, float value and character constant on output screen. 3 P5. Program to input a number and check whether it is even or odd. 4 P6. Program to print the following pattern. 4 * ** *** **** ***** P7. Program to print name 5 times using while loop 5 P8. Program to print sum of first 10 integers using while loop. 5 P9. Program to find out the roots of quadratic equation.. 6 P10. Program to print the following pattern.. 7 1 23 456 7 8 9 10 11 12 13 14 15 P11. Program to print the following pattern.. 7 0 01 012 0123 01234 P12. Program to print the Fibonacci series to n number of places. 8 P13. Program to display marks of 5 students. 9 P14. Program to find average and sum of marks obtained by a class of 5 students. 10 P15. Program to print a string. 11 P16. Program to print the length of the string. 11 P17. Program to print the reverse of the string.. 12 P18. Program to write input marks of 5 subjects and calculate the percentage. 13 P19. Program to input the time and print the day time 14 P20. Program to print factorial of a number. 14 P21. Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle.. 15 P22. Write a program to find average male and female height in the class 16 P23. Write a program to accept a character and determine whether it is an alphabet, character or special symbol.. 17 P24. Program to print out powers of 2: 1, 2, 4, 8..up to 2^N. 18 P25. Program to swap two numbers without a third variable.. 19 P26. Calculate Electricity Bill with if-else condition.. 20 P27. Program to print the alphabet set a to z and A to Z in decimal form and character form. 21 P28. Program to print area of circle. 22 P29. Program to print the address of a variable along with its value.. 22 P30. Program to read a series of words from a terminal using scanf function .. 23
Output:
My name is hargun
P2. Program to enter two numbers and check which one is greater.
#include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b; printf("Enter two numbers: "); scanf(%d%d",&a,&b); if(a>b) { printf("a is greater than b");
Output:
Enter two numbers : 3 5 a is less than b 2
Output:
Enter an integer: 3 Enter another integer: 5 The sum of the two integers is: 8
P4. Program for showing one integer value, float value and character constant on output screen.
#include<stdio.h> #include<conio.h> void main() { clrscr(); int a=5; float b=8.5; char c='A'; printf("Interger Value = %d",a); printf("Float Value = %f",b); } printf("Character Constant = %c",c); getch();
Output:
Interger Value = 5 Float Value = 8.5 Character Constant = A
Output:
Enter a number: 2 (or 3) Number is even (or Number is odd)
Output:
My name is hargun My name is hargun My name is hargun My name is hargun My name is hargun
getch(); }
Output:
The sum is 55
Output:
Enter values of a, b and c: 4 2 1 The roots are imaginary
#include<stdio.h> #include<conio.h> Void main() { int i,j,var=1; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d ",var); var+=1; } printf("\n"); } getch(); }
Output:
Enter the number of places: 10 0 1 1 2 3 5 8 13 21 34 55
Output:
Enter marks of student 1: 12 Enter marks of student 2: 13 Enter marks of student 3: 14 Enter marks of student 4: 10 Enter marks of student 5: 15 The marks of 5 students are: 12 13 14 10 15
P14. Program to find average and sum of marks obtained by a class of 5 students.
#include<stdio.h> #include<conio.h> Void main() { int avg,sum=0; int i,marks[5]; for(i=0;i<=4;i++) { printf("\nEnter the marks: "); scanf("%d",&marks[i]); } for(i=0;i<=4;i++) sum=sum+marks[i]; avg=sum/5; printf("\nAverage marks are: %d\n",avg); getch(); }
Output:
Enter the marks: 10 Enter the marks: 12 Enter the marks: 14 Enter the marks: 15 Enter the marks: 12 Average marks are: 12
10
Output:
Enter the name John Smith The name entered is John Smith
Output:
Enter the name: hargun The name entered is: hargun The length of the string is: 6
11
Output:
Enter the string to reverse=> This is a sample string string < This is a sample string> reverse <gnirts elpmas a si sihT >
12
P18. Program to write input marks of 5 subjects and calculate the percentage.
#include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e; float p; printf("\nEnter the marks of 5 subjects: "); scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); p=(a+b+c+d+e)/5; printf("\nThe percentage is: "); printf("%f\n\n",p); if(p>=60) printf("Ist division"); else if(p<=59&&p>=50) printf("Second division"); else if(p<=49&&p>=40) printf("Third division"); else printf("Fail"); getch(); }
Output:
Enter the marks of 5 subjects: 85 75 65 89 96 The percentage is: 82.000000 Ist division
13
P19. Program to input the time and print the day time
#include<stdio.h> #include<conio.h> void main() { clrscr(); int t; printf("Enter the time in 24 hours clock format\n"); scanf("%d",&t); if(t==0) printf("It is midnight\n"); else if(t>0 && t<400) printf("The time is after midnight and not yet morning\n"); else if(t>=400 && t<700) printf("It is early morning"); else if(t>=700 && t<1200) printf("It is morning"); else if(t==1200) printf("It is exact afternoon"); else if(t>1200 && t<300) printf("It is afternoon"); else if(t>=300 && t<=700) printf("It is evening"); else printf("It is Night"); getch(); }
Output:
Enter the time in 24 hours clock format 400 It is early morning
Output:
Enter the number: 5 The factorial is: 120 14
P21. Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle
#include <stdio.h> /*header file*/ #include <conio.h> /*header file*/ #include <math.h> int main() /*main function*/ { float tri_area(float a,float b, float c); /* new function*/ float s1; float s2; float s3; float area; printf("\nEnter the three sides of a triangle"); scanf("%f%f%f",&s1,&s2,&s3); area=tri_area(s1,s2,s3); printf("\n Area of triangle is : %f square units.", area); /*printing area*/ _getch(); } float tri_area(float a,float b,float c) { float s; float area; s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); /*herons formula*/ return(area); }
Output:
Enter the three sides of a triangle 12 14 16 Area of triangle is : 81.332649 square units.
15
P22. Write a program to find average male and female height in the class
#include<stdio.h> /*header file*/ #include<conio.h> int main() /*main function*/ { float mavg=0, favg=0; int mh[5], fh[5], count; float mtot; float ftot; getch(); mtot=0,ftot=0; } for(count=0;count<5;count++) /*for loop*/ { printf("\nEnter the height of %d",count+1); printf(" male: "); scanf("%d",&mh[count]); mtot+=mh[count]; } mavg=mtot/5; printf("\n"); for(count=0;count<5;count++) /*for loop*/ { printf("\n\nEnter the height of %d",count+1); printf(" female: "); scanf("%d",&fh[count]); printf("\nThe average male height is %f",mavg); printf("\nThe average female height is %f",favg); } favg+=ftot/5; /*Formula to find average*/ ftot+=fh[count];
Output:
Enter the height of 1 male: 5 Enter the height of 2 male: 6 Enter the height of 3 male: 5 Enter the height of 4 male: 6 Enter the height of 5 male: 6 Enter the height of 1 female: 5 Enter the height of 2 female: 5 Enter the height of 3 female: 6 Enter the height of 4 female: 5 Enter the height of 5 female: 6 The average male height is 5.600000 The average female height is 5.400000
16
P23. Write a program to accept a character and determine whether it is an alphabet, character or special symbol
#include<stdio.h> /*header file*/ #include<conio.h> int main() /*main function*/ { char ch; /*variable declaration*/ printf("\n Enter a character : "); scanf("%c",& ch); if(ch>=65 && ch<=90) { printf("\nThe character is an upper case letter"); } if(ch>=97 && ch<=122) { printf("\nThe character is an lower case letter"); } if(ch>=48 && ch<=57) { printf("\nThe character is a digit"); } if((ch>=0 && ch<48)||(ch>=57 && ch<65)||(ch>90 && ch<97)||(ch>122)) { printf("\nThe character is a special symbol"); } getch(); }
Output:
Enter a character : a The character is an lower case letter
17
printf("\t
\t
2^n\n");
printf("\t================\n"); for (n=0; n<=N; n++) { printf("\t%3d \t %6d\n", n, val); val = 2*val; getch(); }
Output:
n 2^n 8 9 10 11 12 13 14 15 16 256 512 1024 2048 4096 8192 16384 32768 65536 ================ 0 1 2 3 4 5 6 7 1 2 4 8 16 32 64 128
18
Output:
enter First Number : 3 enter Second Number : 4 first Number is : 4 second Number is : 3
19
Output:
Enter Total Units: 350 Total: 1662
20
P27. Program to print the alphabet set a to z and A to Z in decimal form and character form
#include<stdio.h> #include<conio.h> main() { char c; printf("\n\n"); for(c=65;c<=122;c+=1) { if(c>90&&c<97) continue; printf("|%4d - %c ", c,c); } printf("|\n"); getch(); }
Output:
| 65 - A | 66 - B | 67 - C | 68 - D | 69 - E | 70 - F | 71 - G | 72 - H | 73 - I | 74 - J | 75 - K | 76 - L | 77 - M | 78 - N | 79 - O | 80 - P | 81 - Q | 82 - R | 83 - S | 84 - T | 85 - U | 86 - V | 87 - W | 88 - X | 89 - Y | 90 - Z | 97 - a | 98 - b | 99 - c | 100 - d | 101 - e | 102 - f | 103 - g | 104 - h | 105 - i | 106 - j | 107 - k | 108 - l | 109 - m | 110 - n | 111 - o | 112 - p | 113 - q | 114 - r | 115 - s | 116 - t | 117 - u | 118 - v | 119 - w | 120 - x | 121 - y | 122 - z |
21
Output:
To Print Area Of Circle in Centimetres Enter Radius of Circle = 3 Area of Circle is 28.260000
P29. Program to print the address of a variable along with its value
#include<stdio.h> #include<conio.h> void main() { char a; getch(); int x; } float p, q; a='A'; x=125; p=10.25, q=18.76; printf("%c is stored at addr %u.\n",a,&a); printf("%f is stored at addr %u.\n",p,&p); printf("%f is stored at addr %u.\n",q,&q); printf("%d is stored at addr %u.\n",x,&x);
Output:
A is stored at addr 2293575. 125 is stored at addr 2293568. 10.250000 is stored at addr 2293564. 18.760000 is stored at addr 2293560.
22
P30. Program to read a series of words from a terminal using scanf function
#include<stdio.h> #include<conio.h> main() { char word1[40], word2[40], word3[40], word4[40]; printf("Enter text: "); scanf("%s %s", word1, word2); scanf("%s", word3); scanf("%s", word4); printf("\n"); printf("word 1 = %s\nword2 = %s\n", word1, word2); printf("word 3 = %s\nword4 = %s\n", word3, word4); getch();
Output:
Enter text: this is a sample-text word 1 = this word2 = is word 3 = a word4 = sample-text
23