qwertyuiopasdfghjklzxcvbn mqwertyuiopasdfghjklzxcvb nmqwertyuiopasdfghjklzxcv bnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklz xcvbnmqwertyuiopasdfghjkl zxcvbnmqwertyuiopasdfghjk lzxcvbnmqwertyuiopasdfghj klzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfg hjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopa C Programming File PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE SHERIL SHARMA
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
SHERIL SHARMA
P1. Program to print a sentence in output screen…………………………………………………………………………………………………………………… 2P2. Program to enter two numbers and check which one is greater……………………………………………………………………………………….. 2P3. Program to add two integers…………………………………………………………………………………………………………………………………………….. 3P4. Program for showing one integer value, float value and character constant on output screen…………………………………………. 3P5. Program to input a number and check whether it is even or odd………………………………………………………………………………………. 4P6. Program to print the following pattern………………………………………………………………………………………………………………………………. 4** ** * ** * * ** * * * *P7. Program to print name 5 times using while loop………………………………………………………………………………………………………………… 5P8. Program to print sum of first 10 integers using while loop…………………………………………………………………………………………………. 5P9. Program to find out the roots of quadratic equation………………………………………………………………………………………………………….. 6P10. Program to print the following pattern…………………………………………………………………………………………………………………………….. 712 34 5 67 8 9 1011 12 13 14 15P11. Program to print the following pattern…………………………………………………………………………………………………………………………….. 700 10 1 20 1 2 30 1 2 3 4P12. Program to print the Fibonacci series to n number of places……………………………………………………………………………………………. 8P13. Program to display marks of 5 students……………………………………………………………………………………………………………………………. 9P14. Program to find average and sum of marks obtained by a class of 5 students…………………………………………………………………. 10P15. Program to print a string…………………………………………………………………………………………………………………………………………………. 11P16. Program to print the length of the string…………………………………………………………………………………………………………………………. 11P17. Program to print the reverse of the string……………………………………………………………………………………………………………………….. 12P18. Program to write input marks of 5 subjects and calculate the percentage………………………………………………………………………. 13P19. Program to input the time and print the day time…………………………………………………………………………………………………………… 14P20. Program to print factorial of a number……………………………………………………………………………………………………………………………. 14P21. Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle….. 15P22. Write a program to find average male and female height in the class……………………………………………………………………………… 16P23. Write a program to accept a character and determine whether it is an alphabet, character or special symbol……………….. 17P24. Program to print out powers of 2: 1, 2, 4, 8..up to 2^N……………………………………………………………………………………………………. 18P25. Program to swap two numbers without a third variable………………………………………………………………………………………………….. 19P26. Calculate Electricity Bill with if-else condition………………………………………………………………………………………………………………….. 20P27. Program to print the alphabet set a to z and A to Z in decimal form and character form…………………………………………………. 21P28. Program to print area of circle…………………………………………………………………………………………………………………………………………. 22P29. Program to print the address of a variable along with its value……………………………………………………………………………………….. 22P30. Program to read a series of words from a terminal using scanf function ……………………………………………………………………….. 23
Index
2
P1. Program to print a sentence in output screen.
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("My name is Sheril");
getch();
}
Output:My name is Sheril
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");
printf("\na is less than b");
}
getch();
}
Output:Enter two numbers : 3 5
3
a is less than b
4
P3. Program to add two integers.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf("Enter an integer: ");
scanf("%d",&a);
printf("\nEnter another integer: ");
scanf("%d",&b);
c=a+b;
printf("The sum of the two integers is: ");
printf("c=%d",c);
getch();
}
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
5
Character Constant = A
6
P5. Program to input a number and check whether it is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("\nEnter a number: ");
scanf("%d",&n);
if(n%2==0)
{
printf("Number is even");
}
else
printf("Number is odd");
getch();
}
Output:Enter a number: 2 (or 3)
Number is even (or Number is odd)
P6. Program to print the following pattern** ** * ** * * ** * * * *
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
}
getch();
7
}
8
P7. Program to print name 5 times using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
while(i<=5)
{
printf("My name is Sheril\n");
i++;
}
getch();
}
Output:My name is Sheril
My name is Sheril
My name is Sheril
My name is Sheril
My name is Sheril
P8. Program to print sum of first 10 integers using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0, sum=0;
while(a<10)
{
a++;
sum+=a;
} printf("The sum is %d,sum);
getch();
}
Output:The sum is 55
9
P9. Program to find out the roots of quadratic equation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
int a,b,c;
float dis,root1,root2;
printf("\nEnter values of a, b and c: ");
scanf("%d%d%d",&a,&b,&c);
dis=(b*b)-4*a*c;
printf("\ndis=%f",dis);
root1=-b+sqrt(dis)/2.0*a;
root2=-b-sqrt(dis)/2.0*a;
if(dis<0)
{
printf("\nThe roots are imaginary");
}
else if(dis==0)
{
printf("\nThe roots are equal and real");
}
else
{
printf("\nThe roots are real and distinct");
printf("\nThe roots are %f%f",root1,root2);
}
getch();
}
Output:Enter values of a, b and c: 4 2 1
The roots are imaginary
10
11
P10. Program to print the following pattern12 34 5 67 8 9 1011 12 13 14 15
#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();
}
P11. Program to print the following pattern00 10 1 20 1 2 30 1 2 3 4
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
12
}
13
P12. Program to print the Fibonacci series to n number of places
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=-1,b=1,c=0,n;
printf("Enter the number of places: ");
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
c=a+b;
printf("%d ",c);
a=b;
b=c;
}
getch();
}
Output:Enter the number of places: 10
0 1 1 2 3 5 8 13 21 34 55
14
P13. Program to display marks of 5 students.
#include<stdio.h>
#include<conio.h>
main()
{
int marks[5];
int i=1;
for(i=1;i<=5;i++)
{
printf("\nEnter marks of student %d: ",i);
scanf("%d",&marks[i]);
}
printf("\nThe marks of 5 students are: ");
for(i=1;i<=5;i++)
{
printf("%d ",marks[i]);
}
getch();
}
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
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
16
P15. Program to print a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int c;
char name[20];
printf("Enter the name");
gets(name);
printf("The name entered is");
printf("%s",name);
getch();
}
Output:Enter the name John Smith
The name entered is John Smith
P16. Program to print the length of the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int c;
char name[20];
printf("Enter the name: ");
gets(name);
printf("The name entered is: ");
printf("%s",name);
c=strlen(name);
printf("\nThe length of the string is: %d",c);
getch();
}
Output:Enter the name: Sheril
The name entered is: Sheril
The length of the string is: 6
17
P17. Program to print the reverse of the string.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100], rstr[100];
int i,k;
printf("Enter the string to reverse=>");
gets(str);
for (i=strlen(str)-1,k=0; i>=0; i--,k++)
{
rstr[k]=str[i];
}
rstr[k]='\0';
printf("string <%s> reverse <%s>\n",str,rstr);
getch();
}
Output:Enter the string to reverse=> This is a sample string
string < This is a sample string> reverse <gnirts elpmas a si sihT >
18
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
19
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
P20. Program to print factorial of a number.
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1,i=1,n;
printf("Enter the number:\n");
scanf("%d",&n);
while(n!=i)
{
fact=fact*n;
n--;
}
20
printf("The factorial is: %d",fact);
getch();
}
Output:Enter the number:
5
The factorial is: 120
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*/