Introduction
Introduction
Chapter
              INTRODUCTION—C PROGRAMS
#include<stdio.h>
#include<conio.h>
void main()
int a,b,s;
clrscr();
scanf(“%d%d",&a,&b);
s=a+b;
printf(“sum=%d”,s);
getch();
Output:
sum=11
#include<stdio.h>
#include<conio.h>
void main()
int r;
float pi=3.14,area,ci;
clrscr();
12
                                                              INTRODUCTION—C PROGRAMS   13
scanf(“%d”,&r);
area=pi*r*r;
ci=2*pi*r;
printf(“circumference=%f ”,ci);
getch();
Output:
area of circle=78.000
circumference=31.4
#include<stdio.h>
#include<conio.h>
void main()
int p,r,t,si;
clrscr();
printf(“enter principle, Rate of interest & time to find simple interest: ”);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
getch();
Output:
enter principle, rate of interest & time to find simple interest: 500
5
simple interest=50
#include<stdio.h>
#include<conio.h>
void main()
{
C PROGRAMS WITH SOLUTIONS
float c,f;
clrscr();
scanf(“%f”,&c);
f=(1.8*c)+32;
getch();
Output:
temp in Fahrenheit=89.59998
#include<stdio.h>
#include<conio.h>
void main()
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}
Output:
65
50
60
60
sum=300
percentage=60.000
#include<stdio.h>
#include<conio.h>
                                                          INTRODUCTION—C PROGRAMS   15
void main()
int a,b;
clrscr();
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int n,a,r=0;
clrscr();
scanf(“%d”,&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
printf(“reverse=%d”,r);
getch();
Output:
reverse=654
C PROGRAMS WITH SOLUTIONS
#include<stdio.h>
#include<conio.h>
void main()
int gs,bs,da,ta;
clrscr();
scanf(“%d”,&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf(“gross salary=%d”,gs);
getch();
Output:
gross salary=122
#include<stdio.h>
#include<conio.h>
void main()
int gs,bs,da,ta;
clrscr();
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf(“gross salary=%d”,gs);
getch();
Output:
2*1=2
                                             INTRODUCTION—C PROGRAMS   17
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“a is greatest”);
if((b>c)&&(b>a))
printf(“b is greatest”);
if((c>a)&&(c>b))
printf(“c is greatest”);
getch();
}
Output:
b is greatest
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(“%d%d”,&a,&b);
getch();
Output:
b is greater
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf(“%d”,&n);
if(n%4==0)
else
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
                                                         INTRODUCTION—C PROGRAMS   19
printf(“no is odd”);
getch();
Output:
no is odd
#include<stdio.h>
#include<conio.h>
void main()
int x,y;
clrscr();
scanf(“%d”,&x);
x<<=3;
y=x;
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
char ch;
clrscr();
printf(“enter m for Monday\nt for Tuesday\nw for Wednesday\nh for Thursday\nf for
scanf(“%c”,&ch);
switch(ch)
case „m‟:
20          C PROGRAMS WITH SOLUTIONS
case „M‟:
printf(“monday”);
break;
case „t‟:
case „T‟:
printf(“tuesday”);
break;
case „w‟:
case „W‟:
printf(“wednesday”);
break;
case „h‟:
case „H‟:
printf(“thursday”);
break;
case „f ‟:
case „F‟:
printf(“friday”);
break;
case „s‟:
case „S‟:
printf(“saturday”);
break;
case „u‟:
case „U‟:
printf(“sunday”);
break;
default :
printf(“wrong input”);
break;
getch();
Output:
t for Tuesday
                                                            INTRODUCTION—C PROGRAMS   21
w for Wednesday
h for Thursday
f for Friday
s for Saturday
u for Sunday: f
friday
#include<stdio.h>
#include<conio.h>
void main()
int a,b,n,s,m,su,d;
clrscr();
scanf(“%d%d”,&a,&b);
scanf(“%d”,&n);
switch(n)
case 1:
s=a+b;
printf(“sum=%d”,s);
break;
case 2:
m=a*b;
printf(“multiply=%d”,m);
break;
case 3:
su=a-b;
printf(“subtraction=%d”,su);
break;
case 4:
d=a/b;
printf(“divission=%d”,d);
break;
C PROGRAMS WITH SOLUTIONS
default:
printf(“wrong input”);
break;
getch();
Output:
2 for multiply
3 for subtraction
4 for division: 1
sum=12
#include<stdio.h>
#include<conio.h>
void main()
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
sum=sum+i;
}
printf(“sum =%d”,sum);
getch();
Output:
1 no is=1
2 no is=2
3 no is=3
4 no is=4
5 no is=5
                                        INTRODUCTION—C PROGRAMS   23
6 no is=6
7 no is=7
8 no is=8
9 no is=9
10 no is=10
sum=55
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
getch();
Output:
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
C PROGRAMS WITH SOLUTIONS
for(j=5;j>=i;j--)
printf(“ ”);
for(k=1;k<=i;k++)
printf(“*”);
printf(“\n”);
getch();
Output:
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
for(j=3;j>=i;j--)
printf(“ ”);
{
for(k=1;k<=i*2-1;k++)
printf(“*”);
printf(“\n”);
getch();
}
                                                   INTRODUCTION—C PROGRAMS   25
Output:
***
*****
#include<stdio.h>
#include<conio.h>
void main()
int a=1,b=1,c=0,i;
clrscr();
printf("%d\t%d\t",a,b);
for(i=0;i<=10;i++)
c=a+b;
if(c<100)
printf("%d\t",c);
a=b;
b=c;
getch();
Output:
1 1 2 3 5 8 13 21 34 55 89
22] Program to find factorial of a number.
#include<stdio.h>
#include<conio.h>
void main()
int n,i,fact=1;
clrscr();
scanf(“%d”,&n);
C PROGRAMS WITH SOLUTIONS
for(i=n;i>=1;i--)
fact=fact*i;
printf(“Factorial=%d”,fact);
getch();
Output:
Enter a no: 5
Factorial=120
#include<stdio.h>
#include<conio.h>
void main()
int i,n,r=0;
clrscr();
scanf(“%d”,&n);
for(i=2;i<=n-1;i++)
if(n%i==0)
r=1;
break;
if(r==0)
printf(“prime no”);
else
printf(“Not prime”);
getch();
Output:
Not prime
                                                    INTRODUCTION—C PROGRAMS   27
#include<stdio.h>
#include<conio.h>
void main()
int n,i,sum=0;
clrscr();
scanf(“%d”,&n);
printf(“1”);
for(i=2;i<=n-1;i++)
for(i=1;i<=n;i++)
sum=sum+i;
printf(“ 1/%d”,n);
printf(“\nSum=1/%d”,sum+1/n);
getch();
Output:
Sum=1/28
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
scanf(“%d”,&n);
for(i=1;i<n;i=i+2)
printf(“%d+”,i);
sum=sum+i;
C PROGRAMS WITH SOLUTIONS
printf("%d",n);
printf("\nsum=%d",sum+n);
getch();
Output:
1+3+5+7
Sum=16
26] Program to use bitwise AND operator between the two integers.
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(“%d %d”,&a,&b);
c=a&b;
getch();
Output:
#include<conio.h>
void main()
int *p1,*p2,sum;
clrscr();
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
                                                     INTRODUCTION—C PROGRAMS   29
printf(“sum=%d”,sum);
getch();
Output:
20
sum=30
#include<stdio.h>
#include<conio.h>
void main()
int *p1,*p2,sum;
clrscr();
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
getch();
Output:
20
sum=30
29] Program to show sum of 10 elements of array & show the average.
#include<stdio.h>
#include<conio.h>
void main()
int a[10],i,sum=0;
float av;
clrscr();
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
C PROGRAMS WITH SOLUTIONS
for(i=0;i<10;i++)
sum=sum+a[i];
printf(“sum=%d”,sum);
av=sum/10;
printf(“average=%.2f”,av);
getch();
Output:
sum=42
average=4.22
#include<stdio.h>
#include<conio.h>
void main()
int a[5],max,i;
clrscr();
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
max=a[0];
for(i=1;i<5;i++)
if(max<a[i])
max=a[i];
                                        INTRODUCTION—C PROGRAMS   31
getch();
Output:
maximum no= 7
#include<stdio.h>
#include<conio.h>
void main()
int a[3][2],b[3][2],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
C PROGRAMS WITH SOLUTIONS
printf(“ %d ”,a[i][j]);
printf(“\n”);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
printf(“ %d ”,b[i][j]);
printf(“\n”);
getch();
Output:
1
4
a matrix is
6
                                           INTRODUCTION—C PROGRAMS   33
b matrix is
#include<stdio.h>
#include<conio.h>
void main()
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
for(j=0;j<2;j++)
printf(“%d\t”,c[i][j]);
}
C PROGRAMS WITH SOLUTIONS
printf(“\n”);
getch();
Output:
Sum of matrix is
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],c[5],i;
clrscr();
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
for(i=0;i<5;i++)
scanf(“%d”,&b[i]);
for(i=0;i<5;i++)
                                                      INTRODUCTION—C PROGRAMS   35
c[i]=a[i]-b[i];
printf(“subtraction”);
for(i=0;i<5;i++)
printf(“ %d ”,c[i]);
getch();
Output:
subtraction 3 3 3 3 3
#include<stdio.h>
#include<conio.h>
void main()
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
C PROGRAMS WITH SOLUTIONS
scanf(“%d”,&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]*b[i][j];
printf(“matrix is\n”);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
printf(“ %d ”,c[i][j]);
printf(“\n”);
getch();
Output:
matrix is
16
36
                                             INTRODUCTION—C PROGRAMS   37
#include<stdio.h>
#include<conio.h>
void main()
int a[3][2],b[2][3],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf(„%d”,&a[i][j]);
printf(“Matrix:\n”);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
printf(“ %d ”,a[i][j]);
printf(“\n”);
for(i=0;i<3;i++)
for(j=0;j<2;j++)
b[j][i]=a[i][j];
printf(“Transpose matrix:\n”);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf(“ %d ”,b[i][j]);
printf(“\n”);
getch();
}
C PROGRAMS WITH SOLUTIONS
Output:
Matrix:
Transpose matrix:
62
13
#include<stdio.h>
#include<conio.h>
void main()
int max,i,*a[5];
clrscr();
for(i=0;i<5;i++)
scanf(“%d”,&*a[i]);
max=*a[0];
for(i=1;i<5;i++)
{
if(max<*a[i])
max=*a[i];
getch();
}
                                                      INTRODUCTION—C PROGRAMS   39
Output:
maximum no= 7
#include<stdio.h>
#include<conio.h>
void main()
char a[50];
clrscr();
gets(a);
puts(a);
getch();
Output:
hi everyone
#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int r,a;
clrscr();
scanf(“%d”,&a);
r=rev(a);
printf(“square is : %d”,r);
C PROGRAMS WITH SOLUTIONS
getch();
int rev(int x)
return(x*x);
Output:
square is : 25
#include<stdio.h>
#include<conio.h>
void main()
void swap(int,int);
int a,b,r;
clrscr();
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
int temp;
temp=a;
a=b;
b=temp;
Output:
#include<stdio.h>
#include<conio.h>
void main()
int a,f;
int fact(int);
clrscr();
scanf(“%d”,&a);
f=fact(a);
printf(“factorial= %d”,f);
getch();
int fact(int x)
int fac=1,i;
for(i=x;i>=1;i--)
fac=fac*i;
return(fac);
Output:
enter a no: 5
factorial=120
#include<stdio.h>
#include<conio.h>
void main()
void table();
clrscr();
table();
getch();
}
C PROGRAMS WITH SOLUTIONS
void table()
int n,i,r;
scanf(“%d”,&n);
for(i=1;i<=10;i++)
r=n*i;
printf(“%d*%d=%d\n”,n,i,r);
Output:
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
#include<stdio.h>
#include<conio.h>
void main()
int a,b,swap();
clrscr();
a=5;
b=10;
swap(a,b);
                                                        INTRODUCTION—C PROGRAMS   43
getch();
int temp;
temp=x;
x=y;
y=temp;
Output:
#include<stdio.h>
#include<conio.h>
void main()
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
getch();
int temp;
temp=*x;
C PROGRAMS WITH SOLUTIONS
*x=*y;
*y=temp;
Output:
#include<stdio.h>
#include<conio.h>
void main()
void max();
clrscr();
max();
getch();
void max()
int a[5],max,n,i;
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
max=a[0];
for(i=1;i<5;i++)
if(max<a[i])
max=a[i];
INTRODUCTION—C PROGRAMS 45
Output:
maximum no: 6
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf(“enter number: ”);
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
getch();
int fact(int x)
if(x==0)
return 1;
else
return(x*fact(x-1));
Output:
enter number: 5
5!=120
#include<stdio.h>
#include<conio.h>
C PROGRAMS WITH SOLUTIONS
void main()
char s1[20],s2[20];
clrscr();
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“string is a palindrome”);
else
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
file *fp,*fp1;
char c;
clrscr();
fp=fopen(“test.c”,”w”);
printf(“\nenter the contents for file1(#.end)\n”);
c=getchar();
while(c!=‟#‟)
fputc(c,fp);
c=getchar();
rewind(fp);
fp=fopen(“test.c”,”r”);
                                                  INTRODUCTION—C PROGRAMS   47
fp1=fopen(“tes.c”,”w”);
c=fgetc(fp);
while(c!=eof)
fputc(c,fp);
c=fgetc(fp);
fclose(fp);
fclose(fp1);
fp1=fopen(“tes.c”,”r”);
c=fgetc(fp1);
while(c!=eof)
putchar(c);
c=fgetc(fp1);
fclose(fp1);
getch();
Output:
good morning#
good morning
#include<conio.h>
void main()
int a[50],b[50],n1,n2,i,x;
clrscr();
scanf(“%d”,&n1);
for(i=0;i<n1;i++)
printf(“enter a[%d]”,i+1);
scanf(“%d”,&a[i]);
scanf(“%d”,&n2);
for(i=0;i<n2;i++)
printf(“enter b[%d]”,i+1);
scanf(“%d”,&b[i]);
for(x=0;x<n1;x++)
for(i=0;i<n2;i++)
if(b[i]==a[x])
b[i]=‟ „;
for(i=o;i<n1;i++)
printf(“%d”,a[i]);
}
for(i=0;i<n2;i++)
if(b[i]==‟ „;)
continue;
else
printf(“%d”,b[i]);
getch();
}
                                                  INTRODUCTION—C PROGRAMS       49
Output:
3 5 7 2 9
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
void main()
char s[100];
int vow=0,cons=0,spc=0,punc=0,l,i;
clrscr();
l=strlen(s);
for(i=0;i<l;i++)
if(isalpha(s[i]))
if(s[i]==‟a‟||s[i]==‟e‟||s[i]==‟i‟||s[i]==‟o‟||s[i]==‟u‟)
{
C PROGRAMS WITH SOLUTIONS
vow++;
else
cons++;
if(isspace(s[i])
spc++;
if(ispunct(s[i]))
punc++;
printf(“\nno.of words=%d”,spc+1);
printf(“\nno.of vowels=%d”,vow);
printf(“\nno.of consonants=%d”,cons);
printf(“\nno.of space=%d”,spc);
getch();
Output:
No.of vowels=10
No.of consonants=19
No.of space=5
50. Write a program to create enumerated data type for 12 months. Display
#include<stdio.h>
#include<conio.h>
void main()
Enum month(Jan, Feb, Mar, Apr, May, June, July, Aug,Sep, Oct, Nov, Dec)
clrscr();
printf(“Jan=%d”, Jan);
printf(“Feb=%d”, Feb);
printf(“June=%d”, June);
printf(“Dec=%d”, Dec);
Output:
Jan =0
Feb=1
June=5
Dec=11
3
Chapter
               FUNDAMENTALS—C PROGRAMS
y=1.0;
count=1;
while(count<=n)
y=y*x;
count++;
Output:
#include<conio.h>
52
                                                                                                    FUNDAMENTALS—C PROGRAMS   53
#define COLMAX 10
#define ROWMAX 12
void main()
{
int row, column, y;
row=1;
printf(“MULTIPLICATION TABLE ”);
printf(“----------------------------------------                                                    ”);
                              do
                              {
                              column=1;
                              do
                              {
                              y=row*column;
                              printf(“%d”, y);
                              column=column +1;
                              }
                              while(column<=COLMAX);
                              printf(“\n”);
                              row=row+1;
                              }
                              while(row<=ROWMAX);
                              printf(“---------------------------------------------                        ”)
}
Output:
                                                    MULTIPLICATION TABLE
-------------------------------------------------------------------------------------------
1                             2                     3                   4             5    6    7   8      9     10
2                             4                     6                   8             10   12   14 16      18    20
3                             6                     9                   12 15              18   21 24      27    30
4                             8                     12                  16 20              24   28 32      36    40
5                             10                    15                  20 25              30   35 40      45    50
6                             12                    18                  24 30              36   42 48      54    60
7                             14                    21                  28 35              42   49 56      63    70
8                             16                    24                  32 40              48   56 64      72    80
C PROGRAMS WITH SOLUTIONS
                 9    18      27     36      45     54      63     72     81    90
                 10 20        30     40      50     60      70     80     90    100
                 11 22        33     44      55     66      77     88     99    110
                 12 24        36     48      60     72      84     96     108   120
Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive and
negative.
#include<stdio.h>
#include<conio.h>
void main()
long int p;
int n;
double q;
printf(“------------------------------------------------------------”);
printf(“------------------------------------------------------------”);
p=1;
if(n==0)
P=1;
else
p=p*2;
q=1.0/(double)p;
printf(“10ld 10%d %20.12lf”, p,n,q);
}
printf(“---------------------------------------------”);
Output:
--------------------------------------------------------------------------------------------
2 to power n n 2 to power -n
----------------------------------------------------------------------------------------------
                                 1           0                  1.000000000000
                                 2           1                  0.500000000000
                                                        FUNDAMENTALS—C PROGRAMS               55
            4                          2                0.250000000000
            8                          3                0.125000000000
            16                         4                0.062500000000
            32                         5                0.031250000000
            64                         6                0.015625000000
            128                        7                0.007812500000
            256                        8                0.003906250000
            512                        9                0.001953125000
            1024                       10               0.000976562500
            2048                       11               0.000488281250
            4096                       12               0.000244140625
            8192                       13               0.000122070313
            16384                      14               0.000061035156
            32768                      15               0.000030517578
            65536                      16               0.000015258789
            131072                     17               0.000007629395
            262144                     18               0.000003814697
            524288                     19               0.000001907349
            1048576                    20               0.000000953674
A class of n students take an annual examination in m subjects. A program to read the marks obtained
by each student in various subjects and to compute and print the total marks obtained by each of them.
#include<stdio.h>
#include<conio.h>
int n, m, i, j, roll number, marks, total; printf(“Enter number of students and subjects”);
scanf(“%d%d”, &n,&m);
scanf(“%d”, &roll_number);
total=0;
scanf(“%d”, &marks);
total=total+marks;
if(total>=FIRST)
printf(“(First division)”);
else if (total>=SECOND)
printf(“(Second division)”);
else
printf(“(***FAIL***)”);
Output:
36
81 75 83 45 61 59
Enter roll_number:8702
40 19 31 47 39 25
The program illustrates the use of the break statement in a C program. #include<stdio.h>
int m;
printf(“This program computes the average of a set of computers”); printf(“Enter values one after
another”); printf(“Enter a NEGATIVE number at the end”);
sum=0;
for(m=1;m<=1000; ++m)
scanf(“%f”,&x);
if(x<0)
break;
sum+=x;
average=sum/(float)(m-1);
printf(“\n”);
printf(“sum=%f”, sum);
printf(“Average=%f”, average);
Output:
This program computes the average of a set of numbers Enter values one after another
21 23 24 22 26 22 -1
Number of values =6
Sum= 138.000000
Average=23.000000
                                             2 3         n
A program to evaluate the series 1/1-x= 1+x+x +x +.....+x . #include<stdio.h>
{
C PROGRAMS WITH SOLUTIONS
int n;
scanf(“%f”, &x);
sum=0;
sum+=term;
if(term<=ACCURACY)
goto output;
term*=x;
goto end;
output:
end:
Output:
#include<conio.h>
count=0;
negative=0;
while(count<=100)
printf(“enter a number:”);
scanf(“%lf”, &number);
if(number==9999)
break;
if(number<0)
negative++;
continue;
sqroot=sqrt(number);
count++;
printf(“END OF DATA”);
}
Output:
Number=25.000000
Number =40.500000
Square root=6.363961
Enter a number:-9
C PROGRAMS WITH SOLUTIONS
Number is negative
Enter a number: 16
Number= 16.000000
Square root=4.000000
Number is negative
Enter a number: 80
Number=80.000000
Square root=8.944272
Negative items=2
END OF DATA
printf(“----------------------------------------------------------”); m=0;
do
        {
        binom=binom* (m-x+1)/x;
        printf(“%4d”, binom);
        }
        x=x+1;
        }
        printf(“\n”);
        M=m+1‟
        }
        while(m<=MAX);
        printf(“-------------------------------------------------------”);
        }
        Output:
          Mx            0     1       2    3       4        5      6         7     8    9    10
        0               1
        0               1     1
        2               1     2       1
        3               1     3       3    1
        4               1     4       6    4       1
        5               1     5       10   10      5        1
        6               1     6       15   20      15       6      1
        7               1     7       21   35      35       21     7         1
        8               1     8       28   56      70       56     28        8     1
        9               1     9       36   84      126      126    84        36    9    1
        10              1     10      45 120       210      252 210          120   45   10   1
{
C PROGRAMS WITH SOLUTIONS
scanf(“%d”, &x);
value[n]=x;
printf(“%d”, value[n]);
printf(“\n”);
printf(“|\n”);
if(i= =2)
else
printf(“|”);
printf(“*”);
if(i= =2)
printf(“(%d)”, value[n]);
else
printf(“\n”);
printf(“|\n”);
Output:
Enter employees in Group -1:12
12
23
35
20
                                                     FUNDAMENTALS—C PROGRAMS   63
11
|************
|************
|***********************
Group-2 |***********************(23)
|***********************
|***********************************
Group-3 |***********************************(35)
|***********************************
|********************
Group-4 |********************(20)
|********************
|***********
Group-5 |***********(11)
|***********
void main()
{
cost1=cost;
continue;
if(cost>=cost1)
break;
cost1=cost;
p1=p;
p=(p+p1)/2.0;
cost=40-8&p+p*p;
Output:
                                                              2
Program for plotting of two functions (y1=exp(-ax); y2=exp(-ax /2)). #include<stdio.h>
#include<conio.h>
int i;
printf(“ Y-------> ” );
printf(“0---------------------------------------------------------------------”);
y1=(int) (50*exp(-a*x)+0.25);
y2=(int) (50*exp(-a*x*x/2)+0.5);
if(y1= =2)
{
if(x= = 2.5)
                              FUNDAMENTALS—C PROGRAMS   65
printf(“X |”);
else
printf(“|”);
printf(“”);
printf(“#”);
continue;
if(y1>y2)
if(x= =2.5)
printf(“X |”);
else
printf(“ |”);
printf(“ ”);
printf(“*”);
printf(“_”);
printf(“0”);
continue;
if(x==2.5)
printf(“X|”);
else
printf(“ |”);
for(i=1; i<=y1-1; ++i)
printf(“ ”);
printf(“0”);
printf(“_”);
printf(“*”);
}
C PROGRAMS WITH SOLUTIONS
          Output:
          Y----                                                          >
          0----------------------------------------------------------------------------------------------------
          ----
          |                                                                                                              #
          |                                                                                                              0---*
          |                                                                                                       0---   *
          |                                                                                                       0---   *
          |                                                              0---                                     *
          |                                                              0---                                     *
          |                                                              0---*
          |                                                              0---*
          |                                                              0---*
          |                                                              0---*
          |                                                              #
Write a program using a single –subscribed variable to evaluate the following expressions:
10
Total =   ∑             2
                       X 2           the values of x1, x2, ..... are read from the terminal.
1
#include<stdio.h>
#include<conio.h>
void main()
int i;
scanf(“%f”, &value);
x[i]=value;
}
total=0.0;
printf(“\n”);
printf(“total=%.2f”, total);
Output:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10
X[1]=1.10
X[2]=2.20
X[3]=3.30
X[4]=4.40
X[5]=5.50
X[6]=6.60
X[7]=7.70
X[8]=8.80
X[9]=9.90
X[10]=10.10
Total = 446.86
Given below is the list of marks obtained by a class of 50 students in an annual examination. 43 65 51 27 79 11
56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78 65
56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59
Write a program to count the number of students belonging to each of the following groups of marks:
0-9,10-19,20 -29,----,100.
#include<stdio.h>
#include<conio.h>
#define MAXVAL 50
#define COUNTER 11
void main()
C PROGRAMS WITH SOLUTIONS
int group[COUNTER]={0,0,0,0,0,0,0,0,0,0,0};
scanf(“%f”, &value[i]);
++group[(int)(value[i]/10)];
printf(“\n”);
low=i*10;
if(i= =10)
high=100;
else
high=low+9;
Output:
43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78
65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59 (Input data)
           8             70 to 79                   6
           9             80 to 89                   4
           10            90 to 99                   2
           11            100 to 100                 0
Write a program for sorting the elements of an array in descending order. #include<stdio.h>
for(i=0;i<n;i++)
if(arr[i]<arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
getch();
Output:
Enter a number: 32
Enter a number: 43
Enter a number: 23
C PROGRAMS WITH SOLUTIONS
Enter a number: 57
Enter a number: 47
57
47
43
32
23
if(arr[i]>LARGE)
LARGE=arr[i];
}
                                                         FUNDAMENTALS—C PROGRAMS    71
Output:
Enter a number: 32
Enter a number: 43
Enter a number: 23
Enter a number: 57
Enter a number: 47
#include<conio.h>
if(arr[i]>arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
C PROGRAMS WITH SOLUTIONS
printf(“%d”, arr[i]);
i=0;
j=1;
while(i<n)
if(arr[i]==arr[j])
arr[x]=arr[x+1];
n-;
else
i++;
j++;
printf(“%d”, arr[i]);
getch();
}
Output:
Enter a number: 3
Enter a number: 3
Enter a number: 4
Enter a number: 6
Enter a number: 4
                                                          FUNDAMENTALS—C PROGRAMS            73
                                         th
Write a program for finding the desired k smallest element in an array. #include<stdio.h>
for(i=0;i<n;i++)
if(arr[i]<arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
C PROGRAMS WITH SOLUTIONS
printf(“%d”, arr[i]);
scanf(“%d”, k);
if(k<=n)
else
printf(“Please enter a valid value for finding the particular smallest element”);
getch();
Output:
Enter a number: 33
Enter a number: 32
Enter a number: 46
Enter a number: 68
Enter a number: 47
32
33
46
47
68