C Programs 1
C Programs 1
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:
4) PROGRAM: Stop
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();
}
3) FLOWCHART:
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
4) PROGRAM:
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:
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
2) ALGORITHM:
step1: start
3) FLOWCHART:
Take i, a[20],
sum
Program to find total of even integers
i=0
4) PROGRAM:
Stop
5) Result:
Enter 5 integers
24682
Sum = 22
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:
Stop
Enter 5 integers
12345
Sum=9
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:
Take i, a[20],
3) FLOWCHART: sum
4) PROGRAM:
5) Result:
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]
4) PROGRAM:
#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:
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);
}
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:
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
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
3) FLOWCHART:
Start
Take i,j,n
For
(i=1;i<=n;i+
+)
If
(j>i)
I++
Stop
Display “\t”
Display “\n”
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
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:
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
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
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
2) ALGORITHM:
Read Name,
3) FLOWCHART: RollNo
I++
Stop
I=0
If
I<n
Display n[i],
r[i]
J=0
If
Display s[i][j]
I<n
4) PROGRAM:
#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
Experiment 18.2 :
step1: start
Step2: take a number I and fact=1 Take n
3) FLOWCHART:
Program to find factorial of a given number F=i=1
4) PROGRAM:
5) Result:
Enter a number 5
Factorial of a given no is: 120
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
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:
f=factorial(n);
printf("%d",f);
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);
}
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;
};
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();
}
PROGRAM:
A program to find the total no. of marks
#include<stdio.h>
struct student
{
char name[10];
int rollno;
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++
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
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
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();
4) Result:
Enter the marks
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
Close (f2)
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);
}
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);
b=p*q;
printf("%s%d%d%d",c,p,q,b);
fclose(f2);}