C Program for plotting of two functions (y1=exp(-ax); y2=exp(-ax^2/2))

/* Write a C Program for plotting of two functions (y1=exp(-ax); y2=exp(-ax^2/2))  */
==============================================================
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i;
float a, x, y1, y2;
a=0.4;
printf(“ Y-------> ” );
printf(“0---------------------------------------------------------------------”);
for(x=0; x<5; x=x+0.25)
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)
printf(“X |”);
else
printf(“|”);
for(i=1; i<=y1-1; ++i)
printf(“”);
printf(“#”);
continue;
}
if(y1>y2)
{
if(x= =2.5)
printf(“X |”);
else
printf(“ |”);
for(i=1; i<=y2-1; ++i)
printf(“ ”);
printf(“*”);
for(i=1; i<=(y1-y2-1); ++i)
printf(“_”);
printf(“0”);
continue;
}
if(x==2.5)
printf(“X|”);
else
printf(“ |”);
for(i=1; i<=y1-1; ++i)
printf(“ ”);
printf(“0”);
for(i=1; i<=(y2-y1-1); ++i)
printf(“_”);
printf(“*”);
}
}

Output:
======
                                                Y---->
0--------------------------------------------------------------------------------------------------------
|                                                                                                 #
|                                                                                               0---*
|                                                                                               0---*
|
|                                                                                       0---*
|                                                                                       0---*
|
|                                                                          0---*
|                                                                          0---*
| 0---*
|        0---*
|               0---*
|#

C Program of minimum cost problem

/* Write a C Program of minimum cost problem.  */
======================================
#include<stdio.h>
#include<conio.h>
void main()
{
float p, cost, p1, cost1;
for(p=0; p<=10; p=p+0.1)
{
cost=40-8*p+p*p;
if(p= =0)
{
cost1=cost;
continue;
}
if(cost>=cost1)
break;
cost1=cost;
p1=p;
}
p=(p+p1)/2.0;
cost=40-8&p+p*p;
printf(“MINIMUM COST=%.2f AT p=%.1f”, cost, p);
}
Output:
MINIMUM COST = 24.00 AT p=4.0

C Program to draw a histogram.

/*  Write a C Program to draw a histogram.  */
=================================
#include<stdio.h>
#include<conio.h>
#define N 5
void main()
{
int value[N];
int i, j, n, x;
for(n=0; n<N; ++n)
{
printf(“enter employees in group-%d: ”, n+1);
scanf(“%d”, &x);
value[n]=x;
printf(“%d”, value[n]);
}
printf(“\n”);
printf(“|\n”);
for(n=0; n<N; ++n)
{
for(i=1; i<=3; i++)
{
if(i= =2)
printf(“ Group-%d | “, n+1);
else
printf(“|”);
for(j=1; j<=value[n]; ++j)
printf(“*”);
if(i= =2)
printf(“(%d)”, value[n]);
else
printf(“\n”);
}
printf(“|\n”);
}
}

Output:
======
Enter employees in Group -1:12
12
Enter employees in Group -2:23
23
Enter employees in Group -3:35
35
Enter employees in Group -4:20
20
Enter employees in Group -5:11
11
|
|************
Group-1 |************ (12)
|************
|
|***********************
Group-2 |***********************(23)
|***********************
|
|***********************************
Group-3 |***********************************(35)
|***********************************
|
|********************
Group-4 |********************(20)
|********************
|
|***********
Group-5 |***********(11)
|***********
|

C Program to print binomial coefficient table

/* Write a C Program to print binomial coefficient table.  */
==========================================
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int m, x, binom;
printf(“m x”);
for (m=0; m<=10; ++m)
m=0;
do
{
printf(“%2d”, m);
X=0; biom=1;
while(x<=m)
{
if(m= =0 || x= =0)
printf(“%4d”, binom);
else
F
CP
61
UNDAMENTALS
ROGRAMS
{
binom=binom* (m-x+1)/x;
printf(“%4d”, binom);
}
x=x+1;
}
printf(“\n”);
M=m+1’
}
while(m<=MAX);
}

Output:
======
Mx0123 4 5 6 7 89 10
01
011
2121
31331
414641
515101051
61615201561
7 17213535217 1
8 1828567056288 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 Program to illustrates the use of continue statement.

/* Write a C Program to illustrates the use of continue statement.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int count, negative;
double number, sqroot;
printf(“enter 9999 to STOP”);
count=0;
negative=0;
while(count<=100)
{
printf(“enter a number:”);
scanf(“%lf”, &number);
if(number==9999)
break;
if(number<0)
{
printf(“Number is negative ”);
negative++;
continue;
}
sqroot=sqrt(number);
printf(“Number=%lf square root=%lf ”, number, sqroot);
count++;
}
printf(“Number of items done =%d”, count);
printf(“Negative items=%d”, negative);
printf(“END OF DATA”);
}

Output:
=======
Enter 9999 to STOP
Enter a number: 25.0
Number=25.000000
Square root =5.000000
Enter a number: 40.5
Number =40.500000
Square root=6.363961
Enter a number:-9
Number is negative
Enter a number: 16
Number= 16.000000
Square root=4.000000
Enter a number: -14.75
Number is negative
Enter a number: 80
Number=80.000000
Square root=8.944272
Enter a number: 9999
Number of items done=4
Negative items=2
END OF DATA

C to program to evaluate the series 1/(1-x) = 1 + x + x^2 + x^3 + ................... + x^n

/* Write a C to program to evaluate the series
1/(1-x) = 1 + x + x^2 + x^3  + ................... + x^n
====================================
#include<stdio.h>
#include<conio.h>
#define LOOP 100
#define ACCURACY 0.0001
void main()
{
int n;
float x, term, sum;
printf(“input value of x :”);
scanf(“%f”, &x);
sum=0;
for(term=1, n=1; n<=LOOP; ++n)
{
sum+=term;
if(term<=ACCURACY)
goto output;
term*=x;
}
printf(“FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED
ACCURACY”);
goto end;
output:
printf(“EXIT FROM LOOP”);
printf(“sum=%f; no. of terms=%d”, sum,n);
end:
;
}
Output:
Input value of x: .21
EXIT FROM LOOP
Sum=1.265800; No. of terms=7
Input value of x: .75
EXIT FROM LOOP
Sum=3.999774; No. of terms=34
Input value of x:.99
FINAL VALUE OF N IS NOT SUFFICIENT TO ACHIEVE DESIRED ACCURACY

C program to use of the break statement

/* Write a C program to illustrates the use of the break statement   */
=================================================
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
float x, sum, average;
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(“Number of values =%d”,m-1);
printf(“sum=%f”, sum);
printf(“Average=%f”, average);
}

Output:
=====
This program computes the average of a set of numbers
Enter values one after another
Enter a NEGATIVE number at the end
21 23 24 22 26 22 -1
Number of values =6
Sum= 138.000000
Average=23.000000

C 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

/*  Write a C Program 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>
#define FIRST 360
# define SECOND 240
void main()
{
int n, m, i, j, roll number, marks, total;
printf(“Enter number of students and subjects”);
scanf(“%d%d”, &n,&m);
printf(“\n”);
for(i=1; i<=n; ++i)
{
printf(“Enter roll number:”);
scanf(“%d”, &roll_number);
total=0;
printf(“Enter marks of %d subjects for ROLL NO”, m, roll number);
for(j=1; j<=m; j++)
{
scanf(“%d”, &marks);
total=total+marks;
}
printf(“TOTAL MARKS =%d”, total);
if(total>=FIRST)
printf(“(First division)”);
else if (total>=SECOND)
printf(“(Second division)”);
else
printf(“(***FAIL***)”);
}
}

Output:
======
Enter number of students and subjects
3 6
Enter roll_number: 8701
Enter marks of 6 subjects for ROLL NO 8701
81 75 83 45 61 59
TOTAL MARKS =404 (First division)
Enter roll_number:8702
Enter marks of 6 subjects for ROLL NO 8702
51 49 55 47 65 41
TOTAL MARKS =308(Second division)
Enter roll_number: 8704
40 19 31 47 39 25
TOTAL MARKS=201(*** FAIL ***)

C Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive

/*  Write a C 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(“ 2 to power n n 2 to power -n”);
printf(“------------------------------------------------------------”);
p=1;
for(n=0; n<21; ++n)
{
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
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

C program to print the multiplication table from 1*1 to 12*10.

Write a C program to print the multiplication table from 1*1 to 12*10.
#include<stdio.h>
#include<conio.h>
#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
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