100% found this document useful (1 vote)
142 views

C Programs 1

The document provides examples of C programming experiments involving finding the greatest of 3 numbers, performing arithmetic expressions using switch statements, calculating factorials, generating prime numbers up to a given value, calculating the sum of natural numbers, and finding the total of even integers in an array. The examples include the aim, algorithm, flowchart, program code, and sample result for each experiment.

Uploaded by

Malathi Sankar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
142 views

C Programs 1

The document provides examples of C programming experiments involving finding the greatest of 3 numbers, performing arithmetic expressions using switch statements, calculating factorials, generating prime numbers up to a given value, calculating the sum of natural numbers, and finding the total of even integers in an array. The examples include the aim, algorithm, flowchart, program code, and sample result for each experiment.

Uploaded by

Malathi Sankar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 37

PROGRAMMING IN C

Experiment 5: Write a program to find greatest among 3 numbers


1) AIM: Program to find greatest among 3 numbers
Start
2) ALGORITHM:
Step1:start Take
a,b,c
Step2:input a,b,c
Step3:if(a>b) &&(a>c)
If
Step4:display a is grater (a>b)&&(
a>c)
Step 5:else Display a

Step6:if(b>c)
Step7: display b is grater
Step 8:else If
(b>c)
Step: display c is grater
Step10:stop Display b
Display
c

3) FLOWCHART:

To find greatest among 3 numbers

4) PROGRAM: Stop

Program to find greatest among 3 numbers

void main()
{
int a,b,c; 5) Result:
clrscr();
printf("enter the values of a,b and c"); Enter the values of a,b and c
scanf("%d%d%d",&a,&b,&c); 10
if(a>b && a>c) 30
printf("a is greatest of %d %d %d", a,b,c); 20
else 30 is greatest of 10 30 20
if(b>c)
printf("b is greatest of %d %d %d",a,b,c);
else
printf("c is gratest of %d %d %d",a,b,c);
getch();
}

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 0


PROGRAMMING IN C

Experiment 6: Write a Program to perform the arithmetic expression


using switch statement
1) AIM: Program to perform the arithmetic expression using switch statement
2) ALGORITHM:
Step1:start
Step2:input a,b
Step3:switch(result)
Step4:case ‘+’:print num of a& b is a+b
Step5: case ‘-’:print num of a& b is a-b
Step6: case ‘*’:print num of a& b is a*b
Step7: case ‘/’:print num of a& b is a/b
Step8: case ‘%’:print num of a& b is a%b
Step9: default: invalid option
Step10: stop
3) PROGRAM:
Program to perform the arithmetic expression using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();
printf(" 1.addition\n 2.subtraction\n 3.multiplication\n 4.division\n");
printf("enter the values of a & b");
scanf("%d%d",&a,&b);
printf("enter your choice : ");
scanf("%d",&op);
switch(op)
{ 5) Result:
case 1:printf("sum of %d and %d=%d",a,b,a+b);
break; 1. Addition
case 2:printf("difference of %d and %d=%d",a,b,a-b); 2. Subtraction
break; 3. Multiplication
case 3:printf("multiplication of %d and %d=%d",a,b,a*b); 4. Division
break; Enter your choice : 1
case 4:printf("Divisionn of two numbers is %d=",a/b); Enter a and b values 10
break; 20
default: printf(" Enter Your Correct Choice."); Sum of 10 and 20 = 30
break;
}
getch();
}

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 1


PROGRAMMING IN C

Experiment 7: Write a program to find the factorial of a given


number
1) AIM: Program to find the factorial of a given number
2) ALGORITHM:
Start
Step1: start
Step2: input n,I,f
Take n
Step3: f=i=1
Step4: if(i<=n)
F=i=1
Step5: f=f*i
Step6: i=i+1
Step7: repeat from step5 to step6 till steps true
If
Step8: print f (i<=n
)
tep9: stop

3) FLOWCHART:

Program to find the factorial of a given number


f=f*i; i=i+1
4) PROGRAM:

Program to find the factorial of a given number


Display f
void main()
{
int n,i,f;
f=i=1; Stop
clrscr();
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{ 5) Result:
f*=i;
i++; Enter a number 5
}
printf("the factorial of %d is %d",n,f); The factorial of 5 is 120
getch();
}

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 2


PROGRAMMING IN C

Experiment 8: Write a program to generate all prime numbers up to nth


number

1) AIM: Program to generate prime number till nth number


2) ALGORITHM:
Step1: start
Step2: read n value
Step3: for i=1 i<=n
Start
Step4:repeat a b c d e
a)factorial equal to 0
Take
b) for i=1,j<=1 repeat c,d n,i,j
c)if i percentage j equal to zero
i=1,
d) fact equal to factorial added with one
e) if factorial equal to2print as prime number
step5: display the prime no till nth num
if i<n
6: stop

3) FLOWCHART:
Program to generate prime number
till nth number. j=1, fact=0

If
j<=n

If i
%j=0

If
fact=2

Print i, j++

Print i Stop

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 3


PROGRAMMING IN C

4) PROGRAM:

Program to generate prime number till nth number

void main()
{
int n,i,fact,j;
printf("enter the range");
scanf("%d",&n);
printf(“Prime numbers are\n”);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
fact++;
if(f==2)
printf("%d “,i);
}
getch();
}

5) Result:

Enter the range 10


Prime numbers are
3 5 7

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 4


PROGRAMMING IN C

Experiment 9: Write a program to find total of first n natural numbers

1) AIM: Program to find sum of n natural numbers start

2) Algorithm:
Read n
Step1: start
Step2: read n
Step3: i=0,sum=0 i=0;sum=0
Step4: perform from step 5 to step 6 until i<=n
Step5: i++
Step6:sum+=i; While(i<=n)
Step7: write sum
Step8: stop
i++
3) Flow chart:

4) Program: Sum+=i

#include<stdio.h>
#include<conio.h>
main() Write sum
{
int n,i=0,sum=0;
clrscr( );
printf(“Enter Limit : “); stop
scanf(“%d”,&n);
while(i<=n)
{
i++;
sum+=i;
}
printf(“Sum of %d natural numbers = %d”,n,sum);
getch();
}

5) Result:

Enter Limit : 10
Sum of 10 natural numbers = 55

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 5


PROGRAMMING IN C

Experiment 10: Program to find total of even integers

1) AIM: Program to find total of even integers

2) ALGORITHM:

step1: start

step2: for i=0;i<20;i++


if(a[i]%2==0)
sum=sum+a[i];

step3: stop Start

3) FLOWCHART:
Take i, a[20],
sum
Program to find total of even integers
i=0
4) PROGRAM:

Program to find total of even integers


If
#include<stdio.h> i<=20
main()
{
int a[20],i,sum=0; If
printf("enter5 integrs"); (A[i]
%2==0)
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++) Sum=sum+a[i],i++
{
if(a[i]==0)
sum=sum+a[i]; I++
}
prinf("sum =%d",sum);
getch();
Display Sum
}

Stop
5) Result:

Enter 5 integers
24682
Sum = 22

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 6


PROGRAMMING IN C

Experiment 11: Program to find total of odd integers

1) AIM: Program to find total of odd integers

2) ALGORITHM:

step1: start
step2: for(i=0;i<20;i++)
{
if(a[i]%2==1)
sum=sum+a[i];
}
step3:stop Start

3) FLOWCHART:
Take i, a[20],
sum
Program to find total of odd integers
i=0
4) PROGRAM:

Program to find total of odd integers


If
i<=20
#include<stdio.h>
main()
{ If
(A[i]
int a[20],i,sum=0; %2==1)
printf("enter 5 integrs");
for(i=0;i<5;i++)
scanf("%d",&a[i]); Sum=sum+a[i],i++
for(i=0;i<5;i++)
{
if(a[i]==1) I++
sum=sum+a[i];
}
prinf("sum =%d",sum);
getch(); Display Sum
}

Stop
Enter 5 integers
12345
Sum=9

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 7


PROGRAMMING IN C

Experiment 12: Program to find sum of all even integers

PROGRAM: Program to find sum of all even integers

void main()
{
int i,n,sum;
sum=0;
clrscr();
printf("enter any number");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(i%2==0)
sum=sum+i;
}
printf("total no of even integer is %d",sum);
}

5) Result:

Enter any number 10


Sum = 30

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 8


PROGRAMMING IN C

Experiment 13: Program to find sum of all odd integers


1) AIM: Program to find sum of all odd integers
2) ALGORITHM:
Step1: start
Step2: read I,n
Step3: sum=0,i=0
Step4: if(i<=n) then i=i+1 else goto 2
Step5: if (i%2!=0) then sum++
Step6: print sum
Start
Step7: stop

Take i, a[20],
3) FLOWCHART: sum

Program to find sum of all odd integers


i=0

4) PROGRAM:

Program to find sum of all odd integers


void main() If
i<=2
{ 0
int i,n,sum;
sum=0;
If
clrscr(); (A[i]
printf("enter any number"); %2==1)
scanf("%d",&n);
for(i=1;i<=n;i++)
{ Sum=sum+a[i],i+
Stop
+
if(i%2!=0)
sum=sum+i; i++
}
printf("total no of even integer is %d",sum);
Display Sum
}

5) Result:

Enter any number 10


Sum = 25

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 9


PROGRAMMING IN C

Experiment 14: Program to print product of two matrices


1) AIM: Program to print product of two matrices
2) ALGORITHM:
Step1: start
Step2:read I,j,k,a[3][3],b[3][2],c[3][2]
Step3: read a[3][3] & b[3][2]
Step 4:i=0,j=0,k=0
Step5: if i<3 then i++ else goto 1
Start
Step6: if j<3 then j++ else goto 5
Step7: if k<3 then k++ else goto 6
Take a[3][3],b[3][3],c[3]
Step8: c[i][j]=c[i][j]+a[k][j]*b[i][k] [3],i,j,k

Step9: print a[i][j],b[i][j],c[i][j] I=0,j=0,k=0

Step 10: stop

3) FLOWCHART: If
i<=20
Program to print product of two matrices.
if
(i<n

if
j<n
Stop

For
(k=0;j<k.k+
+)

k++

j++

C[i][j]=c[i][j]+a[k][j]*b[i][k]

i++

Display c[i][j]

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 10


PROGRAMMING IN C

4) PROGRAM:

Program to print product of two matrices

#include<stdio.h>
void main()
{
int i,j,k,a[3][3],b[3][2],c[3][2];
printf("enter elements of matrix a");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("enter elements of matrix b");
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<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\t%d",c[i][j]);
}
printf("\n");
}
}
}
}

5) Result:

Enter the elements of matrix a


124521452
Enter the elements of matrix b
124521452
10 18 28
50 18 7
40 45 14

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 11


PROGRAMMING IN C

Experiment 15: Program to print Fibonacci series


1) AIM: Program to print Fibonacci series
Start
2) ALGORITHM:
Step1: start
Step2: read I,x,f,f1,f2
Take I, x, f, f1, f2
Step3: f=0,f1=1,f2=1
Step4: do
I++ F=0, f1=1, f2=1
F1=f2
F2=f
F=f1+f2
While (i<=n)
Step5: print f While
Step6: stop (i<=n)

3) FLOWCHART:
Program to print Fibonacci series
I++,F1=f2;f2=f

4) PROGRAM:
F=f+f2 Stop
Program to print Fibonacci series
void main()
F=f+f2
{
int i,n,f,f1,f2;
printf("enter the range"); Display F
scanf("%d",&n);
f=0;
f1=1;
f2=1;
do
{
i++;
printf("%d\n",f); 5) Result:
f1=f2; Enter the range 9
f2=f; 0 1 1 2 3 5 8 13 21
f=f1+f2;
}
while(i<=n);
}

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 12


PROGRAMMING IN C

Experiment 16 : Print the Following formats


16.1)1 16.2 ) 1 16.3 ) 1 16.4) 1
22 2 2 22 23
333 3 3 333 4 56

1) AIM: program to print the following format


1
2 2
3 3 3
4 4 4 4

2) ALGORITHM:
step1:start
step2:take I,j and n
step3:for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
printf(”%d”,i);
printf(”\n”);
}
step4: stop

3) FLOWCHART:

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 13


Start
PROGRAMMING IN C

Take I,j,n,i=1,j=1

If
i<n

4) PROGRAM:
#include<stdio.h>
If
main() j<n
Stop
{
int i,j,n;
Display i
printf(“enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++) Display “\n”

{
J++
for(j=0;j<i;j++)
printf(”%d”,i);
I++
printf(”\n”);
}
printf(”\n”);
}

5) Result:
1
2 2
3 3 3
4 4 4 4

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 14


PROGRAMMING IN C

Experiment 16.2 :
1) AIM: Program to print the following format
1
2 2
3 3 3
4 4 4 4

2) ALGORITHM:
step1: start
step2: take three integers i,j,n
step3: repeat step4 to step6 for i=1,i<=n,i++
step4: repeat step5 for j=1,j<=n,j++
step5: if j>=1 then
Display I and a space
Else
Display space
step6: transfer cursor to meet line by printing ‘\n’
step7: stop

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 15


PROGRAMMING IN C

3) FLOWCHART:

Start

Take i,j,n

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

If
(j>i)

Display i Display “\n”

I++

Stop
Display “\t”

Display “\n”

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 16


PROGRAMMING IN C

4) PROGRAM:
#include<stdio.h>
main()
{
int i,j=0,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
if(j>=i)
printf(”%d\t”,i);
else
printf(”\n”);
}
printf(”\t”);
}
printf(“\n”);
}

5) Result:
1
2 2
3 3 3
4 4 4 4

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 17


PROGRAMMING IN C

Experiment 16.3 :
1) AIM: Program to print the following format
2) ALGORITHM:
1
2 2
3 3 3
step1: start
step2: take three integers i,j,k
step3: repeat step2 to step8 for i=1,i<=n,i++
step4: repeat step3 to step4 for k=1,k<=n-i,k++
step5: display blank space
step6: repeat step 5 to step7 for j=1,j<=I,j++
step7: display blank space
step8: take cursor to new line
step9: stop

3) FLOWCHART:

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 18


Start
PROGRAMMING IN C

Take i,j,n,k=0,j=1

If
I<n

4) PROGRAM:

K<n
#include<stdio.h>
main()
Display “\n”
{
int i,j,k,n; If
J<=i
printf(“enter n value”);
scanf(“%d”,&n);
Display”\n”
for(i=0;i<=n;i++)
{
Display i
for(k=0;k<=n-i;k++)
Stop
{ J++

printf(” ”);
I++
}
for(j=1;j<=i;j++)
{
printf(” ”);
printf(” i”);
}
}

5) Result:
1
2 2
3 3 3

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 19


PROGRAMMING IN C

1
Experiment 16.4 : 23
4 5 6
1) AIM: Program to print the following format
2) ALGORITHM:
step1: start
step2: take three integers i,j,k,n and initialize k as 1
step3: repeat step4 to step7 for i=1,i<=n,i++
step4: repeat step5 to step6 for j=1,j<=i,j++
step5: display value of k
step6: increment k by 1
step7: transfer cursor to next line by printing ‘\n’
step8: stop
Start
FLOWCHART:

Take I=0,j=0,k

K=0

If
I<=n

if
j<=n

K=k+1

Display K

Display “\n” Stop

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 20


PROGRAMMING IN C

J++

I++

PROGRAM:
#include<stdio.h>
main()
{
int i,j,k=1,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
printf(”%d\t”,k++);
printf(”\n ”);
}
}

5) Result:
1
23
4 5 6

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 21


PROGRAMMING IN C

Experiment 17: program to read number of student data


1) AIM: program to read number of student data

2) ALGORITHM:

step1: take a character array a, integers r,s,I,j and n


step2: read the value of n Start
step3: for(i=0;i<n;i++)
Enter rollno,name,,,,,,
Read these and enter 5 subject marks Take n[20]
s[i][5]=0; [20],r[20],s[20],I=0
for(j=0;j<5;j++) ,j,n
{
scanf(“%d”,s[i][j]);
s[i][5]=s[i][5]+s[i][j];
} If
step4:display n[i],r[i],s[i][j] I<n
step5:stop

Read Name,
3) FLOWCHART: RollNo

Program to read Read 5 Subjects


num of student data: Marks

I++
Stop

I=0

If
I<n

Display n[i],
r[i]

J=0

If
Display s[i][j]
I<n

R. MALATHI, ASST. PROF. OF CS, HHRC,J++


PDKT 22
PROGRAMMING IN C

4) PROGRAM:

Program to read num of student data

#include<stdio.h>
#include<conio.h>
void main()
{
char n[20][10];
int i,j,r[20],s[20][6];
printf("enter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter name,rollno,....");
scanf("%s%d",&n[i],&r[i]);
printf("enter 5 subject marks");
s[i][5]=0;
for(j=0;j<5;j++)
{
scanf("%d",s[i][j]);
s[i][5]=s[i][5]+s[i][j];
}
}
printf("the data entered is \n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t",n[i],r[i]);
for(j=0;j<5;j++)
printf("%d\t",s[i][j]);
}
getch();
}

5) Result:

Enter name,rollno,….Eswar 20
Enter 5 subject marks
10 50 34 06 42

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 23


PROGRAMMING IN C

The data entered is


Eswar 20 10 50 34 06 42

Experiment 18.2 :

1) AIM: Program to find factorial of a given number


Start
2) ALGORITHM:

step1: start
Step2: take a number I and fact=1 Take n

Step3: read a number n


For(i=0;i<n;i++) Function Fact(n)
Calling
Factorial=fact*i;
Display fact
Step4: stop Fact(n)

3) FLOWCHART:
Program to find factorial of a given number F=i=1

4) PROGRAM:

Program to find factorial of a given number


#include<stdio.h> If
#include<math.h> (i<=
void main() n)
{
clrscr();
printf("enter a number");
fact();
getch();
}
f=f*i; i=i+1
fact()
{ Stop
int i,fact=1,n;
scanf("%d",&n); Display f
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nfactorial of a given no is: %d ",fact);
return fact;

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 24


PROGRAMMING IN C

5) Result:
Enter a number 5
Factorial of a given no is: 120

Experiment 19 : Write a program to convert all lower case to uppercase


characters.
1) AIM: Program on function to scan a character string and convert lower case
character to upper case
2) ALGORITHM:
step1: start
Step2: take a string a function of return value data type is void str upper
Step3: read a string
While (s[i]! =’\0’)
{
if((s[i]>=’a’) &&(s[i]<=’z’))
s[i]=s[i]-32;
i++;
}
display changed string. Start
Step4: stop
3) FLOWCHART:
Take
Program on function to scan a character str,I,j,
string and convert lower case character
to upper case

4) PROGRAM:
While
Program on function to scan a character string and S[i]!
convert lower case character to upper case =’\0’

#include<stdio.h>
#include<conio.h> If
((S[i]>=’a’)
void main() &&
{ (s[i]>=’z’))

char str;
printf("enter a string");
scanf("%s",str); S[i]=s[i]-32
to_str_upper(char[]);
printf("changed to %s",str);
} Stop
void to_str_upper(char[]) I++
{
int i=0;
while(s[i]!='\0')
{ 5) Result:
if((s[i]>='a') && (s[i]>='z'))
Enter a string
gnec
changed to GNEC
R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 25
PROGRAMMING IN C

s[i]=s[i]-32;
i++;
}
}
}

Experiment 22: Write a program to find the factorial of a number using recursion

1) AIM: Program to find the factorial of a number using recursion

2) ALGORITHM:

step1: start
Step2: enter f and n
Step3: read a number n Start
F=factorial (n);
Step4: inside the functional(x) define a local variable ‘x’
If(x==l) Take n
Return (l);
Else
Fact=x*factorial(x-l);
Function Fact(n) Calling
Return(fact);
Step5: stop

3) FLOWCHART:
Fact(n) Display f
To find the factorial of a number using recursion
F=i=1
4) PROGRAM:

To find the factorial of a number using recursion


#include<stdio.h>
If
main() (i<=
{ n)
Stop
int f,n;
clrscr();
printf("enter n");
scanf("%d",&n); f=f*i; i=i+1

f=factorial(n);
printf("%d",f);

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 26


PROGRAMMING IN C

getch();
}
factorial(x)
{
int i,fact=1;
if(x==1)
5) Result:
return(1);
Enter n 4
else 24
fact=x*factorial(x-1);
return(fact);
}

Experiment 29: Program to display student information by initializing


structures

AIM: Program to display student information by initializing structures


ALGORITHM:
step1: take name, roll no and age inside the student structure
step2: enter the required data Start

step3: stop

FLOWCHART:
Struct Student
A program to display student information by
initializing structures
Take name, Rollno , age
PROGRAM:
A program to display student information by initializing structures
#include<stdio.h>
Display name, rollno, age
struct student
{
char name[10]; Stop

int rollno;
int age;

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 27


PROGRAMMING IN C

};
main()
{
static struct student s1;
clrscr(); 5) Result:
printf("enter the name,rollno,age");
Enter name, rollno,age
scanf("%s%d%d\n",&s1.name,&s1.rollno,&s1.age); Ravi 11 25
Ravi 11 25
printf("%s %d %d",s1.name,s1.rollno,s1.age);
getch();
}

Experiment 30: Program to find the total no. of marks


AIM: Program to find the total no. of marks
ALGORITHM:
step1: take name, roll no and total inside the structure
step2: enter the marks of five subjects
for(i=0;i<n;i++)
printf("enter s[%d] student marks" ,i);
s[i].total=0;
for(j=0;j<5;j++)
read the value of s[i].subject[j]
s[i].total=s[i].total+s[i].subject[j];
step3: display s[i].total
step4: stop
FLOWCHART:
A program to find the total no. of marks

PROGRAM:
A program to find the total no. of marks
#include<stdio.h>
struct student
{
char name[10];
int rollno;

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 28


PROGRAMMING IN C

int subject[5],total;
};
main ( )
{
Start
static struct student s[100];
int n,i,j; Struct student s

clrscr();
Enter NO.of Students
printf("enter the no.of students");
scanf("%d",&n);
Enter Marks of Five
printf("enter the marks of five subjects"); Subjects,
I=0
for(i=0;i<n;i++)
{
If
printf("enter s[%d] student marks”, i); I<n

s[i].total=0;
for(j=0;j<5;j++)
{ S[i].total=0

scanf("%d",&s[i].subject[j]);
s[i].total=s[i].total+s[i].subject[j]; J=0

}
printf("%d",s[i].total);
If
} j<5

5) Result:
enter the no.of students2 S[i]=s[i].total+s[i].subjec
t[j]
enter the marks of five subjects enter s[0] student marks1
2 J++

3
Display Total
4
5
I++

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT Start 29


PROGRAMMING IN C

15enter s[1] student marks12


32
14
15
65
138

Experiment 31: Program to find the salary of employee and salary details
1) AIM: Program to find the salary of employee and salary details

2) ALGORITHM:
step1: take a character array of name, an id inside the structure
step2: take another structure of inside the structure name that salary take, basic, pf, hra,
da, gross
Start
step3: enter the name, id of an employee and read these
step4: use dot operator to access these variables
Struct employee e1
step5: display gross salary
step6: stop Struct salary s1

3) FLOWCHART:
A program to find the salary of employee and salary details
Enter name ,id, salary

4) PROGRAM:
A program to find the salary of employee and salary details
E1.s1.hra=15%*basic
#include<stdio.h> E1.s1.da=45%*basic
E1.s1.gross=e1.s1.basic+e1.s1.hra
struct employee +e1.s1.da+e1.s1.pf

{
char name[10]; Display Gross Salary
Display Basic
int id; Display Hra
Display DA
Display Pf
struct salary
{
int basic,pf; Stop

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 30


PROGRAMMING IN C

float hra,ta,da,gross;
}s1;
}e1;
main()
{
printf("enter name & id of emp");
scanf("%s%d",&e1.name,&e1.id);
printf("enter salary of emp");
scanf("%d%f%f%d",&e1.s1.basic,&e1.s1.hra,&e1.s1.da,&e1.s1.pf);
e1.s1.hra=15% * basic;
e1.s1.da=45%*basic;
e1.s1.gross=e1.s1.basic+e1.s1.hra+e1.s1.da+-e1.s1.pf;
printf("%s\n%d",e1.name,e1.s1.gross);
printf("\n%d\n%f\n%d\n%f\n",e1.s1.basic,e1.s1.hra,e1.s1.da,e1.s1.pf,e1.s1.gross);
}

5) Result:
Enter name and id of emp
Eswar
101
Enter salary of Emp
5000
Gross salary : 8000

Experiment 32: Program to pass structure as an argument to function


Calculate total marks

1) AIM: Program to pass structure as an argument to function Calculate total


marks

2) ALGORITHM:
step1: take a structure ex2
step2: inside the structure declare 6 integers
step3: declare structure ex2 as s1
step4: declare structure ex2 as s2,ex2 as fun();

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 31


PROGRAMMING IN C

step5: display the message enter the marks


step6: take value of the subjects from the user
step7: store the return value in s2.total
step8: print the value of s2.total
step9: stop
3) PROGRAM:
A program to pass structure as arguments to function and calculate total marks of
5 students
#include<stdio.h>
struct ex2
{
int m1,m2,m3,m4,m5,total;
};
main()
{
struct ex2 s1;
struct ex2 s2;
struct ex2 fun();
printf("enter the marks");
scanf("%d%d%d%d%d",&s1.m1,&s1.m2,&s1.m3,&s1.m4,&s1.m5);
s2=fun(s3);
printf("%d",s1.total);
}
struct ex2 fun(s3)
struct ex2 s3;
{
s3.total=s3.m1+s3.m2+s3.m3+s3.m4+s3.m5;
return(s3);
}

4) Result:
Enter the marks

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 32


PROGRAMMING IN C

10 20 30 40 50
150

Start

Experiment 34: Program to write data file and read data from file
FILE *f2
1) AIM: Program to write data file and read data from file
2) ALGORITHM: Open Data file to
step1: start write

step2: take a character ch and define a file pointer f2


step3: open a file data.dat for writing
step4: while ((ch=getch()!=eof)
read a character ch
step5: close the file data.dat
While
step6: open the same file for reading ((Ch==get
while((ch=get(f2)!=EOF) char())!
=EOF)
display character on monitor
step7: close the data.dat
step8:stop
3) FLOWCHART:
Putc(ch,f2)
Programs to write data file and read data from file

4) PROGRAM: Close (f2)

A program to write data file and read data from file


#include<stdio.h>
Open data file to read
main()
{
charch;
While
((Ch==
FILE *f2; getc())!
==EOF)
f2=fopen("data.dat","w");
while((ch=getchar())!=EOF)
putc(ch,f2);
Putchar (ch, f2)
fclose(f2);

Close (f2)

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 33


Stop
PROGRAMMING IN C

f2=fopen("data.dat","r");
5) Result:
while((ch=getc(f2))!=EOF)
Gurunanak Engineering
putchar(ch); College, Ibrahimpatnam, RR
Dist.
fclose(f2); }
Gurunanak Engineering
College, Ibrahimpatnam, RR
Dist.
Experiment 35: Program to write integer data into file and read it from file

1) AIM: Program to write integer data into file and read it from file
2) ALGORITHM:
step1: start
Start
step2: take a number and define a file pointer
step3: open a file data.dat for writing
step4: read on integer and also read an integer into file
step5: close the file data.dat FILE *f2
step6: open the same file for reading
display an integer
step7: stop Open Data file to
write
3) FLOWCHART:
A program to write integer data into file and read it from file
Read num
4) PROGRAM
A program to write integer data into file and read it from file
Putw( num,f2)
#include<stdio.h>
main()
{ Close (f2)

int num;
FILE *f2; Open data file to read

f2=fopen("data.int","w");
scanf("%d",&num); Num=getw(f2)

putw(num,f2);
fclose(f2); Display Num

f2=fopen("data.int","r");
num=getw(f2); 5) Result: Close (f2)

12
12
R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 34
Stop
PROGRAMMING IN C

printf("%d",num);
fclose(f2);
}

Experiment 36: Program to write product details


1) AIM: Program to write product details
2) ALGORITHM:
step1: start
Start
step2: take a character array c
step3: take three integers p,q,b
step4: define a file pointer fp FILE *f2
step5: open a file data.dat for writing
step6: accept c from user and p,q
step7: write string in c andvalues ofp,q into file F2=Open data file to
step8: close the file data.dat write
step9: open the same file for reading
step10: evaluate p*q and store in b Enter name,price,quality
display c,p,q,b to the user
step11: closethe data.dat
step8"stop b=p*q

3) FLOWCHART:
Display c,p,q.b
A program to write product details

4) PROGRAM:
Close (f2)
A program to write product details
#include<stdio.h>
main() Stop

{
char c[20];
int p,q,b;
FILE *f2; 5) Result:
f2=fopen("data.dat","w");
Enter item name, price, quality
printf("enter item name,price,quality"); Rice 25 1
Rice 25 1 25
scanf("%s%d%d",&c,&p,&q);

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 35


PROGRAMMING IN C

b=p*q;
printf("%s%d%d%d",c,p,q,b);
fclose(f2);}

R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 36

You might also like