You are on page 1of 40

#include <stdio.

h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO FIND OUT THE
AGGREGATE AND AVERAGE OF A STUDENT IN 8 SUBJECTS */

int mark, total, n, i;


float avg;

clrscr();
n=8;
total=0;

for(i=1; i<=n; i++)


{
printf("enter the student\'s %d subject mark \n", i);
scanf("%d", &mark);
total=total+mark;
/* also it can be written as
total+=mark
*/
}

avg=total/8.0;
printf("Aggregate is %d \n", total);
printf("Average Mark is %f \n", avg);
getch();
}
enter the student's 1 subject mark
90
enter the student's 2 subject mark
98
enter the student's 3 subject mark
97
enter the student's 4 subject mark
96
enter the student's 5 subject mark
56
enter the student's 6 subject mark
78
enter the student's 7 subject mark
89
enter the student's 8 subject mark
89
Aggregate is 693
Average Mark is 86.625000
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO FIND OUT THE
AGGREGATE AND AVERAGE OF EACH STUDENT IN 8 SUBJECTS.
IT IS DONE FOR N STUDENTS */

int mark, total, n, count,ns, i;


float avg;

clrscr();
n=8;
puts("enter the number of students \n");
scanf("%d", &ns);

for (count=1; count<=ns; count++)


{
total=0;

for(i=1; i<=n; i++)


{
printf("enter the %d student\'s %d subject mark \n",count, i);
scanf("%d", &mark);
total=total+mark;
/* also it can be written as
total+=mark
*/
}

avg=total/8.0;
printf("Aggregate is %d \n", total);
printf("Average Mark is %f \n", avg);
getch();
}
}
enter the number of students
1
enter the 1 student's 1 subject mark
90
enter the 1 student's 2 subject mark
98
enter the 1 student's 3 subject mark
97
enter the 1 student's 4 subject mark
96
enter the 1 student's 5 subject mark
56
enter the 1 student's 6 subject mark
78
enter the 1 student's 7 subject mark
89
enter the 1 student's 8 subject mark
89
Aggregate is 693
Average Mark is 86.625000
# include <stdio.h>
# include <conio.h>
/* array to function */

void printarr( int arr[], int l);

void main()
{
int a[4] = {10,20,30,40};
clrscr();
printarr(a,4);
getch();

void printarr(int arr[],int l)


{
int i;
for(i=0;i<l;i++)
printf("\t %d ",arr[i]);
}

10 20 30 40
# include <stdio.h>
# include <conio.h>

struct stud
{ int regno;
char name[20];
int m[2],t;
};

void main()
{
struct stud s[3];
int i;
clrscr();
for(i=0;i<3;i++)
{
printf("\n enter values for regno, name, m1, m2 \n");
scanf("%d",&s[i].regno);
scanf("%s",s[i].name);
scanf("%d",&s[i].m[0]);
scanf("%d",&s[i].m[1]);

s[i].t = s[i].m[0] +s[i].m[1];


}
for(i=0;i<3;i++)
{
printf("\n regno \t name \t m1 \t m2 \t total \n");
printf("\n %d \t %s \t %d \t %d \t %d \n",s[i].regno,s[i].name, s[i].m[0],s[i].m[1],s[i].t);
}
getch();
}
enter values for regno, name, m1, m2
12
sri
87
78
enter values for regno, name, m1, m2
34
ravi
90
99
enter values for regno, name, m1, m2
45
kavi
34
45
regno name m1 m2 total

12 sri 87 78 165

regno name m1 m2 total

34 ravi 90 99 189

regno name m1 m2 total

45 kavi 34 45 79
#include <stdio.h>
#include <conio.h>
void main()
{
/* THIS IS A PROGRAM TO FOR TEMPERATURE CONVERSION
WITH OPTIONS FOR ENTERING THE TEMPERATURE */

float cel, faren;


clrscr();
puts("ENTER THE TEMPERATURE IN DEGREE CENTIGRADE");
scanf("%f", &cel);
faren=9.0/5.0*cel+32;
printf("FOR THE CENTIGRADE TEMP. %f FARENHEIT TEMP. IS %f", cel, faren);
}

ENTER THE TEMPERATURE IN DEGREE CENTIGRADE


43.2

FOR THE CENTIGRADE TEMP. 43.200001 FARENHEIT TEMP. IS 109.760002


# include <stdio.h>
# include <conio.h>

/* function usage */
int fact(int value);

void main()
{
int n,r,x,y,z,result;

clrscr();
n= 7;
r =3;
x = fact(n); y = fact(r); z = fact(n-r);
printf("%d %d %d\n",x,y,z);
result = x/ (y*z);
printf( "ncr value is %d \n ", result);
getch();

int fact(int value)


{
int i,m=1;
for(i=1;i<=value;i++)
m = m*i;
return(m);
}

5040 6 24
ncr value is 35
# include <stdio.h>
# include <conio.h>

struct stud
{ int regno, m1,m2,t;
char name[20];
};

void main()
{
struct stud s;
FILE *fp;
int i;
clrscr();

/* file open */
fp = fopen("x.dat","w");
/* writing info to file */
for(i=1;i<=3;i++)
{
printf("\n enter values for regno, name, m1, m2 \n");
scanf("%d",&s.regno);
scanf("%s",s.name);
scanf("%d",&s.m1);
scanf("%d",&s.m2);
s.t = s.m1 +s.m2;
fwrite(&s,sizeof(s),1,fp);
}
/* close file */
fclose(fp);

/* file open in reading mode */


fp = fopen("x.dat","r");

while(fread(&s,sizeof(s),1,fp) != 0)
{
printf("\n regno \t name \t m1 \t m2 \t total \n");
printf("\n %d \t %s \t %d \t %d \t %d \n",s.regno,s.name, s.m1,s.m2,s.t);
}

fclose(fp);

getch();

}
enter values for regno, name, m1, m2
12
srimathi
90
98
enter values for regno, name, m1, m2
34
ravi
89
99
enter values for regno, name, m1, m2
45
mani
78
87
regno name m1 m2 total

12 srimathi 90 98 188

regno name m1 m2 total

34 ravi 89 99 188

regno name m1 m2 total

45 mani 78 87 165
# include <stdio.h>
# include <conio.h>

/* gets and getchar function */

void main()
{
char x[5],y[5];
int i;

clrscr();

puts("enter the string value with blanks for x \n");


gets(x);
getch();
printf("the value of x is %s\n",x);

puts("enter the string value one by one for y \n");

for(i=0;i<4;i++)
{
y[i]=getchar();
}
printf("\n the value of y is %s\n",y);

getch();
}

enter the string value with blanks for x


Mr. A.K
the value of x is Mr. A.K
enter the string value one by one for y
ravi

the value of y is ravi


#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO FIND OUT THE
HIGHEST AND LOWEST MARK IN A SUBJECT */

int n,min, max, mark, i;

clrscr();
min=100;
max=0;

puts("enter the value of n \n");


scanf("%d", &n);
for(i=1; i<=n; i++)
{
printf("enter the %d student\'s mark \n", i);
scanf("%d", &mark);

if (min>mark)
min=mark;

if (max<mark)
max=mark;
}

printf("given number of students is %d \n", n);


printf("Minimum Mark is %d \n", min);
printf("Maximum Mark is %d \n", max);
getch();
}
enter the value of n
5
enter the 1 student's mark
56
enter the 2 student's mark
67
enter the 3 student's mark
45
enter the 4 student's mark
78
enter the 5 student's mark
89
given number of students is 5
Minimum Mark is 45
Maximum Mark is 89
# include <stdio.h>
# include <conio.h>

void main()
{
int a=5,x,y;
clrscr();

x=a++;
printf(" a= %d,x = %d\n",a,x);

y=++a;
printf(" a= %d,y = %d\n",a,y);
getch();

a= 6,x = 5
a= 7,y = 7
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO AWARD GRADES FOR
MARKS SCORED USING IF... ELSE.. CONDITION */

int mark;

clrscr();
puts("enter the mark ");
scanf("%d", &mark);

if (mark>=90)
printf("MARK IS %d AND GRADE IS A", mark);
else
if (mark>=80 && mark<90)
printf("MARK IS %d AND GRADE IS B", mark);
else
if (mark>=70 && mark<80)
printf("MARK IS %d AND GRADE IS C", mark);
else
if (mark>=50 && mark<70)
printf("MARK IS %d AND GRADE IS D", mark);
else
printf("MARK IS %d AND GRADE IS FAIL", mark);

getch();
}

enter the mark


45
MARK IS 45 AND GRADE IS FAIL
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO AWARD GRADES FOR
MARKS SCORED USING IF... ELSE.. CONDITION */

int mark, temp;

clrscr();
puts("enter the mark ");
scanf("%d", &mark);

temp=mark/10;

switch(temp)

{
case 10:
case 9:
printf("MARK IS %d AND GRADE IS A", mark);
break;
case 8:
printf("MARK IS %d AND GRADE IS B", mark);
break;
case 7:
printf("MARK IS %d AND GRADE IS C", mark);
break;
case 6:
case 5:
printf("MARK IS %d AND GRADE IS D", mark);
break;
default:
printf("MARK IS %d AND GRADE IS FAIL", mark);
break;
}
getch();
}

enter the mark


45
MARK IS 45 AND GRADE IS FAIL
# include <stdio.h>
# include <conio.h>
/* pointer usage */

void main()
{
int a,t, *b;

clrscr();

a =5;

b = &a;
/* address of a is stored in b */

t = *b;
/* *b points value of a, however b holds address of a */

printf(" Value is in a ,*b, t : %d \t %d \t %d \n", a, *b, t);

printf("address of value is in &a, b : %d \t %d \n", &a, b);


*b+=5;
printf(" Value is in a ,*b, t : %d \t %d \t %d \n", a, *b, t);
getch();
}

Value is in a ,*b, t : 5 5 5
address of value is in &a, b : -12 -12
Value is in a ,*b, t : 10 10 5
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>

void main()
{
/* THIS IS A PROGRAM TO RANK A SET OF STUDENTS */

int mark[10], temp, i,j,rank;


char name[10][20], tname[20];

clrscr();

/* get the data */

for(i=0; i<=9; i++)


{
printf("enter %d student name & mark - \n", i+1);
scanf("%s",name[i]);
scanf("%d", &mark[i]);

clrscr();
/* sorting section */

for (i=0; i<=8; i++)


{
for (j=i+1; j<=9; j++)
if (mark[i] < mark[j])
{
temp=mark[i];
mark[i]=mark[j];
mark[j]=temp;
strcpy(tname,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],tname);
}
}
printf("\n");
printf("RANK ORDER OF THE STUDENTS GIVEN BELOW \n \n");
printf("name\t\tmark\t\trank\n");
rank =1;
for(i=0; i<=9; i++)
{
printf("%s\t\t%d\t\t%d\n",name[i], mark[i], rank);
if (mark[i] > mark[i+1])
rank++;

getch();
}
enter 1 student name & mark -
Sri 98
enter 2 student name & mark -
Ravi 99
enter 3 student name & mark -
Janani 98
enter 4 student name & mark -
Kiruba 85
enter 5 student name & mark -
Vani 100
enter 6 student name & mark -
Kani 45
enter 7 student name & mark -
Dheena 98
enter 8 student name & mark -
Priya 34
enter 9 student name & mark -
Veena 67
enter 10 student name & mark -
Marry 78
RANK ORDER OF THE STUDENTS GIVEN BELOW

name mark rank


Vani 100 1
Ravi 99 2
Sri 98 3
Dheena 98 3
Janani 97 4
Kiruba 85 5
Marry 78 6
Veena 67 7
Kani 45 8
Priya 34 9
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
/* THIS IS A PROGRAM TO FOR SI AND CI CALCULATIONS
WITH OPTIONS FOR ENTERING THE CHOICE OF INTERSEST */

int choice;
float p, n, r, si, ci;
clrscr();

puts("ENTER THE CHOICE (1 FOR SI OR 2 FOR CI)");


scanf("%d", &choice);
puts("ENTER THE VALUES OF p, n and r");
scanf("%f%f%f",&p,&n, &r);
if (choice==1)
{
si=p*n*r/100.0;
printf("SIMPLE INTEREST IS %f", si);
}
else
{
ci=p*pow((1+r/100),n)-p;
printf("COMPOUND INTEREST IS %f", ci);
}
getch();
getch();

ENTER THE CHOICE (1 FOR SI OR 2 FOR CI)


1
ENTER THE VALUES OF p, n and r
10000
2
12.5
SIMPLE INTEREST IS 2500.000000

ENTER THE CHOICE (1 FOR SI OR 2 FOR CI)


2
ENTER THE VALUES OF p, n and r
10000
2
12.5
COMPOUND INTEREST IS 2656.250000
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO SORT
10 DATA IN ASCENDING ORDER */

int a[10], temp, i,j;

clrscr();

/* get the data */

for(i=0; i<=9; i++)


{
printf("enter data- %d \n", i+1);
scanf("%d", &a[i]);
}

/* print the given data */


clrscr();
puts("DATA GIVEN LISTED BELOW \n\n");
for(i=0; i<=9; i++)
{
printf("%d ",a[i]);
}
printf("\n");

/* sorting section */

for (i=0; i<=8; i++)


{
for (j=i+1; j<=9; j++)
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n");
printf("DATA SORTED IN ASCENDING ORDER IS GIVEN BELOW \n
\n");
for(i=0; i<=9; i++)
{
printf("%d ",a[i]);
}

getch();
}
enter data- 1
34
enter data- 2
-34
enter data- 3
45
enter data- 4
56
enter data- 5
12
enter data- 6
67
enter data- 7
109
enter data- 8
67
enter data- 9
78
enter data- 10
26
DATA GIVEN LISTED BELOW

34 -34 45 56 12 67 109 67 78 26

DATA SORTED IN ASCENDING ORDER IS GIVEN BELOW

-34 12 26 34 45 56 67 67 78 109
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO SORT
10 DATA IN DESCENDING ORDER */

int a[10], temp, i,j;

clrscr();

/* get the data */

for(i=0; i<=9; i++)


{
printf("enter data- %d \n", i+1);
scanf("%d", &a[i]);
}

/* print the given data */


clrscr();
puts("DATA GIVEN LISTED BELOW \n");
for(i=0; i<=9; i++)
{
printf("%d ",a[i]);
}
printf("\n");

/* sorting section */

for (i=0; i<=8; i++)


{
for (j=i+1; j<=9; j++)
if (a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("\n");
printf("DATA SORTED IN DESCENDING ORDER IS
GIVEN BELOW \n \n");
for(i=0; i<=9; i++)
{
printf("%d ",a[i]);
}

getch();
}
enter data- 1
34
enter data- 2
45
enter data- 3
56
enter data- 4
12
enter data- 5
-34
enter data- 6
56
enter data- 7
78
enter data- 8
111
enter data- 9
78
enter data- 10
90
DATA GIVEN LISTED BELOW

34 45 56 12 -34 56 78 111 78 90

DATA SORTED IN DESCENDING ORDER IS GIVEN BELOW

111 90 78 78 56 56 45 34 12 -34
# include <stdio.h>
# include <conio.h>

/* usage static variable */


void myfunc();/* with static */
void f1();
void main()
{
clrscr();
puts("without static \n");
f1();
f1();

puts("\n with static \n");


myfunc();
myfunc();

getch();

void myfunc()
{
static int i= 5;
i++;
printf("value of i is %d\n",i);
}

void f1()
{
int i= 5;
i++;
printf("value of i is %d\n",i);
}

without static

value of i is 6
value of i is 6

with static

value of i is 6
value of i is 7
#include <stdio.h>
# include <conio.h>
# include <string.h>
/* string functions */

void main()
{
char x[5],y[5],z[5],w[5];
int len;
clrscr();

strcpy(x,"ravi");
strcpy(w,"ravi");

strcpy(y,x);
/* string copy: strcpy(destination,source) */

printf("\ncopy fn: the source string is %s, the destination string is %s \n",x,y);

strcpy(z,"mathi");

printf("\nthe source string is %s, the destination string before concat is %s \n",x,z);

strcat(z,x);
/* string concatenation : strcat(destination,source) */

printf("\nthe destination string after concatenation is %s \n ",z);

len = strlen(x);
/* string length :strlen(variable) */

printf("\nlength of the string %s is %d \n", x, len);

if (strcmp(w,x)==0)
printf("\nstrings w,x are equal\n");
else

printf("\nstrings w,x are not equal\n");

getch();

}
copy fn: the source string is ravi, the destination string is ravi

the source string is ravi, the destination string before concat is mathi

the destination string after concatenation is mathiravi

length of the string ravi is 4

strings w,x are equal


# include <stdio.h>
# include <conio.h>

struct stud
{ int regno;
char name[20];
int m1,m2,t;
};

void main()
{
struct stud s[3];
int i;
clrscr();
for(i=0;i<3;i++)
{
printf("\n enter values for regno, name, m1, m2 \n");
scanf("%d",&s[i].regno);
scanf("%s",s[i].name);
scanf("%d",&s[i].m1);
scanf("%d",&s[i].m2);

s[i].t = s[i].m1 +s[i].m2;


}
for(i=0;i<3;i++)
{
printf("\n regno \t name \t m1 \t m2 \t total \n");
printf("\n %d \t %s \t %d \t %d \t %d \n",s[i].regno,s[i].name, s[i].m1,s[i].m2,s[i].t);
}
getch();
}
enter values for regno, name, m1, m2
12
srimathi
90
98
enter values for regno, name, m1, m2
34
ravi
89
99
enter values for regno, name, m1, m2
45
mani
78
87
regno name m1 m2 total

12 srimathi 90 98 188

regno name m1 m2 total

34 ravi 89 99 188

regno name m1 m2 total

45 mani 78 87 165
# include <stdio.h>
# include <conio.h>

struct stud
{ int regno;
char name[20];
int m1,m2,t;
};

void main()
{
struct stud s;

clrscr();

printf("\n enter values for regno, name, m1, m2 \n");


scanf("%d",&s.regno);
scanf("%s",s.name);
scanf("%d",&s.m1);
scanf("%d",&s.m2);

s.t = s.m1 +s.m2;

printf("\n regno \t name \t m1 \t m2 \t total \n");


printf("\n %d \t %s \t %d \t %d \t %d \n",s.regno,s.name, s.m1,s.m2,s.t);

getch();
}

enter values for regno, name, m1, m2


12
srimathi
90
98
regno name m1 m2 total

12 srimathi 90 98 188
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
/* THIS IS A PROGRAM TO CALCULATE THE SUM OF FIRST N
NATURAL NUMBERS AND ALSO SUM OF SQUARES OF FIRST N
NATURAL NUMBERS */

int n, sum, i;
float sumsq;

clrscr();
sum=0;
sumsq=0.0;
puts("enter the value of n");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
sum=sum+i;
sumsq=sumsq+i*i;
}
printf("given number is %d \n", n);
printf("sum of first %d natural numbers is %d \n", n, sum);
printf("sum of squares of first %d natural numbers is %10.0f \n", n, sumsq);
getch();
}

enter the value of n


10
given number is 10
sum of first 10 natural numbers is 55
sum of squares of first 10 natural numbers is 385
#include <stdio.h>
#include <conio.h>
void main()
{
/* THIS IS A PROGRAM TO FOR TEMPERATURE CONVERSION
WITH OPTIONS FOR ENTERING THE TEMPERATURE */

int choice;
float cel, faren;
clrscr();

puts("ENTER THE CHOICE (1 FOR C TO F OR 2 FOR F TO C)");


scanf("%d", &choice);

if (choice==1)
{
puts("ENTER THE TEMPERATURE IN DEGREE CENTIGRADE");
scanf("%f", &cel);
faren=9.0/5.0*cel+32;
printf("FOR THE CENTIGRADE TEMP. %f FARENHEIT TEMP. IS %f", cel, faren);
}

else
{
puts("ENTER THE TEMPERATURE IN DEGREE FARENHEIT");
scanf("%f", &faren);
cel=(faren-32)*5.0/9;
printf("FOR THE FARENHEIT TEMP. %f CENTIGRADE TEMP. IS %f", faren, cel );
}
getch();

ENTER THE CHOICE (1 FOR C TO F OR 2 FOR F TO C)


1
ENTER THE TEMPERATURE IN DEGREE CENTIGRADE
43.5
FOR THE CENTIGRADE TEMP. 43.500000 FARENHEIT TEMP. IS 110.300003

ENTER THE CHOICE (1 FOR C TO F OR 2 FOR F TO C)


2
ENTER THE TEMPERATURE IN DEGREE FARENHEIT
110
FOR THE FARENHEIT TEMP. 110.000000 CENTIGRADE TEMP. IS 43.333332
# include <stdio.h>
# include <conio.h>

/* call by value and call by reference */


void fval(int a);
void fref(int *b);

void main()
{
int x=5, y =50;
clrscr();

printf("\n Val: \nbefore calling function x value is %d \n",x);


fval(x);
printf("after calling function x value is %d \n",x);

printf("\nRef: \nbefore calling function y value is %d \n",y);


fref(&y);

printf("after calling function y value is %d \n",y);


getch();
}

void fval(int a)
{
a+= 5;
printf("inside function x -> a value is %d \n",a);
}

void fref(int *b)


{
*b+=5 ;
printf("inside function y -> b value is %d \n",*b);
}

Val:
before calling function x value is 5
inside function x -> a value is 10
after calling function x value is 5

Ref:
before calling function y value is 50
inside function y -> b value is 55
after calling function y value is 55
#include<iostream.h>

void main()
{
int j,k;
float l,m;
char n[30];
cin>>j>>k>>l>>m>>n;
cout<<”j =” <<j<<k<<l<<m<<n;
}

# include <stdio.h>

void main()
{
int sum,j;
sum=0;
j = 1;
do
{
sum += j;
j++;
}
while (j <=10);
printf(“sum = %d “, sum);
}

# include <stdio.h>

void main()
{
int sum,j;
sum=0;
j = 1;
while (j<=10)
{
sum += j;
j++;
}
printf(“sum = %d “, sum);
}
# include <stdio.h>
# include <conio.h>

/* call by value and call by reference */


void swapval(int a, int b);
void swapref(int *m, int *n);

void main()
{
int x=5, y =50;
int l=10,j =20;
clrscr();

printf("\n Val: \nbefore calling function x ,yvalue is %d %d \n",x,y);


swapval(x,y);
printf("after calling function x ,yvalue is %d %d \n",x,y);

printf("\nRef: \nbefore calling function l,j value is %d %d \n",l,j);


swapref(&l, &j);

printf("after calling function l,j value is %d %d \n",l,j);


getch();
}

void swapval(int a, int b)


{
int t;
t=a;
a =b;
b=t;
printf("inside function a,b value is %d %d \n",a,b);
}

void swapref(int *m, int *n)


{
int t;
t = *m;
*m = *n;
*n =t;
printf("inside function *m, *t value is %d %d\n",*m,*n);
}

You might also like