0% found this document useful (0 votes)
146 views

#Include #Include Void Int

The document contains algorithms for several C programs: 1. A program to find the sum of digits of a positive integer. It reads an integer, initializes a sum variable, uses modulo and division to separate each digit and add it to the sum. 2. A program to calculate the Fibonacci series up to a given number. It initializes two variables to store previous numbers, uses a loop to calculate the next number as the sum of the previous two, and displays the results. 3. A program to determine if a number is prime. It uses nested loops to check for divisibility by numbers from 2 to the half of the input number.

Uploaded by

sathireddyk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

#Include #Include Void Int

The document contains algorithms for several C programs: 1. A program to find the sum of digits of a positive integer. It reads an integer, initializes a sum variable, uses modulo and division to separate each digit and add it to the sum. 2. A program to calculate the Fibonacci series up to a given number. It initializes two variables to store previous numbers, uses a loop to calculate the next number as the sum of the previous two, and displays the results. 3. A program to determine if a number is prime. It uses nested loops to check for divisibility by numbers from 2 to the half of the input number.

Uploaded by

sathireddyk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

Aim:-write a c program to find sum of individual digits of a positive integers.

Algorithm:-
Step 1: start
Step 2: read n
Step 3: initialize sum=0;
Step 4: if n<0
Then display “you enter negative number”
(and then goto step7 otherwise goto step5)
Step 5: while n>0 do the following.
5.1 : sum=sum+n%10.
5.2 : n=n/10
Step 6: display the result sum
Step 7 : stop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,x;
clrscr();
printf("enter any positive integer:");
scanf("%d",&n);
if(n<0)
{
printf("you entered negitive number");
}
else
{
x=n;
while(n>0)
{
sum=sum+n%10;
n=n/10;
}
printf("sum of digites of %d is %d",x,sum);
}
getch();
}

Aim :- write a c program to find the fibanacci series.


Algorithm:-
Step 1: start.
Step2 : read n
Step 3 : Assign a=0 and b=1
Step 4: Initialize i=2
Step 5 : while i<=n do the following
5.1 : temp=a+b.
5.2 : display temp.
5.3 : Assign a=b
5.4 : Assign b=temp
5.5 : i=i+1
Step 6 : stop
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,i=2,temp,n;
clrscr();
printf("enter number:");
scanf("%d",&n);
a=0;
b=1;
printf("%d\t%d",a,b);
while(i<=n)
{
temp=a+b;
a=b;
b=temp;
printf("\t%d",temp);
i++;
}
getch();
}

Aim:- write a c program to find the prime number


Algorithm:-
Step 1 : start.
Step 2 : Read n.
Step 3 : initialize k=1.
Step 4 : for i=2 to n-1 do the following
4.1 : for j=2 to i/2 do the following
4.1.1 : if i%j is equal to zero then k=0 and goto step 4.2
4.1.2 : k=k+1
4.1.3 : j=j+1
4.2 : if k!=0 then display the value of I otherwise goto step4.3
4.3 i=i+1
Step 5 : stop
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=1;
clrscr();
printf("enter range:");
scanf("%d",&n);
i=2;
printf("the prime numbers between 1 and %d is",n);
for(i=2;i<n;i++)
{
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
k=0;
break;
}
else
k++;
}
if(k!=0)
printf("%d\n",i);
}
getch();
}_

Aim :- write a program to calculating sum of following sum


Algorithm :-
Step 1:- start
Step 2:- read x
Step 3 :- initialize k=1, sum=1
Step 4 :- calculation of sum=1-x2/2!+ x4/4! –x6/6!+ x8/8!- x10/10!
For i=2 to i<=10 do the following
4.1:- fact=1
4.2 :- for j=1to j<=I do
4.2.2:- j=j+1
4.3 :- sum=sum+pow(-1,k)*pow(x,i)/fact
4.4 :- k=k+1
Step 5:-stop
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
float x,fact;
double sum=1;
clrscr();
printf("enter x:");
scanf("%d",&x);
for(i=2;i<10;i=i+2)
{
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
printf("%f\n",fact);
sum=sum+pow(-1,k)*pow(x,i)/fact;
k=k+1;
}
printf("\n sum=%lf",sum);
getch();
}_

Aim :- to write a c program to find the roots of quadratic equation


Algorithm :-
Step 1:- start
Step 2:- read a,b,c
Step 3 :- if(b*b-4*a*c)<0 then goto step 4
Otherwise goto step 5
Step 4 :- Display the roots are imaginary and goto step 7
Step 5 :- Compute
R1=(-b+sqrt(b*b-4*a*c))/(2*a)
R2=(-b-sqrt(b*b-4*a*c))/(2*a)
Step 6 :- Display r1,r2
Step 7 :- stop
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float r1,r2;
clrscr();
printf("enter the values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
if(b*b-4*a*c<0)
{
printf("roots are imaginary:");
}
else
{
r1=(-b+sqrt(b*b-4*a*c))/(2*a);
r2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("root r1=%f",r1);
printf("\nroot r2=%f",r2);
}
getch();
}

Aim :- write a program to find the distance traveled by the vehicle in t seconds using the
formula distance(s)=ut+1/2 at2 where u and a are the initial velocity (m/s) and
acceleration (m/s2)
Algorithm :-
Step 1:- start
Step 2:- read initial velocity(m/sec)
Step 3:- read acceleration (m/sec2)
Step 4 :- read time ‘t’ in seconds
Step 5 :- s=(u*t)+0.5*a*t*t
Step 6 :- display the distance s.
Step 7 :- stop.
#include<stdio.h>
#include<conio.h>
void main()
{
float u,t,s,a;
clrscr();
printf("enter initial velocity(m/sec)");
scanf("%f",&u);
printf("enter acceleration(m/sec2):");
scanf("%f",&a);
printf("enter time t in seconds:");
scanf("%f",&t);
s=(u*t)+0.5*a*t*t;
printf("\ndistance s=%f",s);
getch();
}
_
Aim :- to write a c program to design a calculator using switch case
Algorithm :-
Step 1:- start
Step 2:- read a,b,ch
Step 3 :-if ch=’+’ then compute a+b and display the result and then goto step 8.
Step 4 :-if ch=’-’ then compute a-b and display the result and then goto step 8.
Step 5 :-if ch=’*’ then compute a*b and display the result and then goto step 8.
Step 6 :-if ch=’/’ then compute a/b and display the result and then goto step 8.
Step 7 :-if ch=’%’ then compute a%b and display the result and then goto step 8.
Step 8:- stop.

Aim :-write a c program to construct a pyramid of numbers


Algorithm :-
Step 1:- start
Step 2 :- read n
Step 3 :- for i=0 to i<n do the following
3.1:- for j=1 to j<=(i*2)+1 do
3.1.1 :- print i+1
3.1.2 :- j=j+1
3.2 :- i=i+1
Step 4 :- stop

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("enter number of rows:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=1;j<=n*3-i;j++)
printf(" ");
for(j=1;j<=(i*2)+1;j++)
printf("%4d",i+1);
printf("\n");
}
getch();
}_

Aim :- write a c program to read in 2 numbers, x and n and then compute the sum of this
geometric progression 1+x2+x3+……….+xn
Algorithm:-
Step 1 :- start
Step 2 :- initialize sum=0
Step 3 :- read x,n
Step 4 :- if x==0 ||n<0 then goto step 5
Otherwise goto step 6
Step 5:- print “read values again” and goto step3
Step 6:- for i=0 to i<=n do
Step 6.1:- sum=sum+pow(x,i)
Step 7 :- print sum
Step 8:-stop

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n,i,sum=0;
clrscr();
read:
printf("\n enter x and n");
scanf("%d%d",&x,&n);
if(n<0||x==0)
{
printf("\nread values again:");
goto read;
}
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("\nsum=%d",sum);
getch();
}

_
Aim :- write a c program to find the factorial of a given integer using a non-recursive
function
Algorithm
Step 1 :- start
Step 2 :- read n
Step 3:- call the function factorial
Fact=factorial(n)
Step 4 :- display fact
Step 5 :- stop

Algorithm for factorial function:-


Step 1 :- assign the value of argument in function call factorial to x
Step 2:- initialize f=1
Step 3:- for i=1 to i<=x do
3.1 f=f*i;
3.2 i=i+1
Step 4 :- return f

#include<stdio.h>
#include<conio.h>
long int factorial(int);
void main()
{
int n;
long int fact;
clrscr();
printf("enter n:");
scanf("%d",&n);
fact=factorial(n);
printf("\nthe factorial of %d is %d",n,fact);
getch();
}
long int factorial(int x)
{
int i;
long int f=1;
for(i=1;i<=x;i++)
f=f*i;
return(f);
}
_
Aim :- write a c program to find the factorial of a given integer using a recursion
Algorithm
Step 1 :- start
Step 2 :- read n
Step 3:- recursively call the function factorial
Fact=factorial(n)
Step 4 :- display fact
Step 5 :- stop

Algorithm for factorial function:-


Step 1 :- assign the value of argument in function call factorial to x
Step 2:- if x==0 then goto step3 otherwise goto step 4
Step 3:- return 1
Step 4 return x*factorial(x-1)
#include<stdio.h>
#include<conio.h>
long int factorial(int);
void main()
{
int n;
long int fact=0;
clrscr();
printf("enter n:");
scanf("%d",&n);
fact=factorial(n);
printf("\nthe factorial of %d is %d",n,fact);
getch();
}
long int factorial(int x)
{
if(x==0)
return (1);
else
return (x*factorial(x-1));
}
_

Aim :- write a c program to find the gcd (greatest common divisor) of two given integers
using a non-recursion function
Algorithm
Step 1 :- start
Step 2 :- Read integer a,b
Step 3 :- call the function gcd
g=gcd(a,b).
Step 4 :-display g
Step 5:- stop
Algorithm for gcd function:-
Step 1:- Assign the values of arguments in function call gcd to x,y i.e. x=a,y=b.
Step 2 :- initialize k=1
Step 3:- while k!=0 do the following
3.1 :- k=x%y
3.2 :-x=y
3.3 y=k
Step 4 :- return x
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,g;
clrscr();
printf("enter any two integers:");
scanf("%d%d",&a,&b);
g=gcd(a,b);
printf("\nthe gcd of %d and %d is %d",a,b,g);
getch();
}
int gcd(int x,int y)
{
int k=1;
while(k!=0)
{
k=x%y;
x=y;
y=k;
}
return (x);
}
_

Aim :- write a c program to find the gcd (greatest common divisor) of two given integers
using a recursion function
Algorithm
Step 1 :- start
Step 2 :- Read integer a,b
Step 3 :- Recursively call the function gcd
g=gcd(a,b).
step 4 :-display g
Step 5:- stop.

Algorithm for gcd function:-


Step 1:- Assign the values of arguments in function call gcd to x and y
Step 2 :- if x%y==0 then goto step 3
Otherwise goto step 4
Step 3:- return y.
Step 4 :- call the function gcd gcd(y,x%y)

#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,g;
clrscr();
printf("enter any two integers:");
scanf("%d%d",&a,&b);
g=gcd(a,b);
printf("\nthe gcd of %d and %d is %d",a,b,g);
getch();
}
int gcd(int x,int y)
{
if(x%y==0)
return (y);
else
gcd(y,x%y);
}
_

Aim :- writea c program to find both the largest and smallest number is in a list of
integers
Algorithm:-
Step 1:- start
Step 2:- read n, the number of elements to be read
Step 3 :- for i=0 to i<n do
3.1 :- Read a[i]
3.2 i=i+1
Step 4:- initialize big=a[0]
Step 5 :- for i=1 to i<n do
5.1 :- if a[i]>big then big=a[i]
5.2 :- i=i+1
Step 6 :- initialize small=a[0]
Step 7 :- for i=1 to i<n do
7.1 :- if a[i]<small then small=a[i]
7.2 :- i=i+1
Step 8 :- display the value of big, that is the largest number among the given number
Step 9 :- display the value of small, that is the smallest number among the given number
Step 10 :- stop
#include<stdio.h>
#include<conio.h>
main()
{
int a[100],n,i,big,small;
clrscr();
printf("enter no of integers to be read:");
scanf("%d",&n);
printf("enter elements to array");
for(i=0;i<n;i++)
{
printf("\nenter element for a[%d]",i);
scanf("%d",&a[i]);
}
big=a[0];
for(i=0;i<n;i++)
{
if(a[i]>big)
big=a[i];
}
small=a[0];
for(i=0;i<n;i++)
{
if(a[i]<small)
small=a[i];
}
printf("largest element in array is:%d",big);
printf("\nsmallest element in array is:%d",small);
getche();
}

Aim :- write a program to perform the addition of two matrices


Algorithm:-
Step 1 :- start
Step 2 :- read r1,c1
Step 3 :- read r2,c2
Step 4 :- if r1==r2 and c1==c2 then goto step 5 otherwise goto step 11
Step 5 :- [read matrix a]
For i=0 to i<r1 do
Step 5.1 :- for j=0 to j<c1 do
5.1.1 :- read a[i][j]
5.1.2 :- j-j+1
Step 5.2 :- i=i+1
Step 6 :- [read matrix b]
For i=0 to i<r2 do
Step 6.1.1 :- read b[i][j]
6.1.2 :- j=j+1
Step 6.2 :- i=i+1
Step 7 :- [print matrix a]
For i=0 to i<r1 do
Step 7.1 :- for j=0 to j<c1 do
7.1.1 :- print a[i][j]
7.1.2 :- j=j+1
Step 7.2 :- i=i+1
Step 8 :- [print matrix b]
For i=0 to i<r2 do
Step 8.1 :- for j=0 to j<c2 do
Step 8.1.1 :- print b[i][j]
8.1.2 :- j=j+1
Step 8.2 :- i=i+1
Step 9 :- [calculation of addition of matrix a and b]
For i=0 to i<r1 do
Step 9.1 :- for j=0 to j<c1 do
9.1.1 :- c[i][j]=a[i][j]+b[i][j]
9.1.2 :- j=j+1
Step 9.2 :- i=i+1
Step 10 :- [print the result of addition of matrices a and b i.e. matrix c]
Step 10.1.1 :- print c[i][j]
10.1.2 :- j=j+1
Step 10.2 :- i=i+1 goto step 12
Step 11 :- print matrix addition is not possible
Step 12 :- stop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,r1,r2,c1,c2;
clrscr();

printf("enter row size and column size for matrix a:");


scanf("%d%d",&r1,&c1);
printf("enter row size and column size for matrix b:");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("enter values for matrix a:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter element for a[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}
printf("enter values for matrix b:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter element for b[%d][%d]",i,j);
scanf("%d",&b[i][j]);
}
}
printf("the matrix a is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]);
printf("\n");
}
printf("the matrix b is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",b[i][j]);
printf("\n");
}

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("addition of matrix a and b is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
else
{
printf("matrix addition is not possible");
} getch();
}

Aim :- write a program to perform the multiplication of two matrices


Algorithm:-
Step 1 :- start
Step 2 :- read r1,c1
Step 3 :- read r2,c2
Step 4 :- if c1==r2 then goto step 5 otherwise goto step 11
Step 5 :- [read matrix a]
For i=0 to i<r1 do
Step 5.1 :- for j=0 to j<c1 do
5.1.1 :- read a[i][j]
5.1.2 :- j-j+1
Step 5.2 :- i=i+1
Step 6 :- [read matrix b]
For i=0 to i<r2 do
Step 6.1.1 :- read b[i][j]
6.1.2 :- j=j+1
Step 6.2 :- i=i+1
Step 7 :- [print matrix a]
For i=0 to i<r1 do
Step 7.1 :- for j=0 to j<c1 do
7.1.1 :- print a[i][j]
7.1.2 :- j=j+1
Step 7.2 :- i=i+1
Step 8 :- [print matrix b]
For i=0 to i<r2 do
Step 8.1 :- for j=0 to j<c2 do
Step 8.1.1 :- print b[i][j]
8.1.2 :- j=j+1
Step 8.2 :- i=i+1
Step 9 :- [calculation of multiplication of matrix a and b]
For i=0 to i<r1 do
Step 9.1 :- for j=0 to j<c2 do
9.1.1 :- c[i][j]=0
9.1.2 :- for k=0 to k<r2 do
9.1.2.1 :- c[i][j]=c[i][j]+a[i][k]*b[k][j]
9.1.2.2 :- k=k+1
9.1.2 :- j=j+1
Step 9.2 :- i=i+1
Step 10 :- [print the result of multiplication of matrices a and b i.e. matrix c]
Step 10.1.1 :- print c[i][j]
10.1.2 :- j=j+1
Step 10.2 :- i=i+1 goto step 12
Step 11 :- print matrix multiplication is not possible
Step 12 :- stop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],k,i,j,r1,r2,c1,c2;
clrscr();

printf("enter row size and column size for matrix a:");


scanf("%d%d",&r1,&c1);
printf("enter row size and column size for matrix b:");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("enter values for matrix a:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter element for a[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}
printf("enter values for matrix b:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter element for b[%d][%d]",i,j);
scanf("%d",&b[i][j]);
}
}
printf("the matrix a is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]);
printf("\n");
}
printf("the matrix b is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",b[i][j]);
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("multiplication of matrix a and b is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
else
{
printf("matrix addition is not possible");
} getch();
}

Aim :-Write a program to insert a sub-string in the given main string


Algorithm :-
Step 1 :- start
Step 2 :- read main string a
Step 3 :- read sub string b
Step 4 :- read the position pos
Step 5 :- initialize i=0;
Step 6 :- Assign n,i=pos
Step 7 :- [copy the sub-string at position pos in the main string a to a temporary string]
While a[i]!=’\0’ do the following
Step 7.1 :- temp[j]=a[i]
Step 7.2 :- i=i+1
Step 7.3 :- j=j+1
Step 8 :- Assign temp[j]=’\0’
Step 9 :- Assign a[pos]=’\0’
Step 10 :- Concatenate the sub string b to the current string is a
Step 11 :- Concatenate the string in temporary to the current string a
Step 12 :- Display the string a
Step 13 :- stop

Aim :- write a program to delete n characters in a given string from given position’
Algorithm :-
Step 1 :- start
Step 2 :- read the position p
Step 3 :- read n
Step 4 :- read n
Step 5 :- initialize i=0
Step 6 :- i=p+n
Step 7 :- [ copy the sub string starting at the position I in the main string a to temporary
string b] while (a[i]!=’\0’) do the following
Step 7.1 :- b[j]=a[i]
Step 7.2 :- i=i+1
Step 7.3 :- j=j+1
Step 8 :- Assign b[j]=’\0\
Step 9 :- Assign a[p]=’\0’
Step 10 :- Concatenate the string in b to string a
Step 11 :- display string a
Step 12 :- stop
#include<stdio.h>
#include<conio.h>
void main()
{
char x[50],y[10];
int pos,n,i;
clrscr();

printf("enter a string:");
gets(x);
printf("enter the position:");
scanf("%d",&pos);
strncpy(y,x,pos);
for(i=pos+n;x[i]!='\0';i++)
{
y[pos]=x[i];
pos++;
}
y[pos]='\0';
printf("after deletion:%s",y);
getch();
}
Aim :- write a program to determine if the given string is a palindrome or not
Algorithm :-
Step 1 :- start
Step 2 :- read name
Step 3 :- initialize n=0 and k=0
Step 4 :- initialize i=0
Step 5 :- for name[i]!= ‘\0’ do the following
5.1 :- n=n+1
5.2 :- i=i+1
Step 6 :- for i=0 to i<n do the following
6.1 :- if a[i]!=a[n-1] then goto step 6.2
Otherwise goto step 6.3
6.2 :- assign k=0 and goto step 7
6.3 :- k=k+1
6.4 :- n=n-1
6.5 :- i=i+1
Step 7 :- if k!=0 then goto step 8 otherwise goto step 9
Step 8 :- display “given string is palindrome” and goto step 10
Step 9 :- display “given string is not a palindrome
Step 10 :-stop
#include<stdio.h>
#include<conio.h>
void main()
{
char s[20];
int i,j,k=10,l;
clrscr();
printf("enter a string:");
gets(s);
for(i=0;s[i]!='\0';i++);
i--;

for(j=0;j<i;j++)
{
if(s[j]==s[i])
{
i--;
}
else
{
k=0;
break;
}
}
if (k==0)
printf("it is not palindrome");
else
printf("it is palindrom");
getch();
}
Aim :-
Write a c program that displays the position (or) index in the string s. where the string t
begins (or) -1 if s does not contain t.
Algorithm :-
Step 1 :- start
Step 2 :- read main string s
Step 3 :- read sub-string t
Step 4 :- Assign q=s
Step 5:- p=strstr(s,t)
Step 6 :- if p==NULL then goto Step 7. otherwise goto Step8
Seep 7:- print “sub-string not found:-1” and then goto step 9
Step 8 :- print the position p-q
Step 9 :- stop
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char mstr[50],sub[10],*p,*q;
int pos,n,i;
clrscr();

printf("enter a string:");
gets(mstr);
printf("enter sub string:");
gets(sub);
q=mstr;
p=strstr(mstr,sub);
if(p==NULL)
printf("sub-string is not found:");
else
printf("sub-string is foundat posotion :%d",p-q);
getch();
}_

Aim :- write a program to count the lines, words and characters in given text
Algorithm :-
Step 1:- start
Step 2:- initialize w=0,l=0,c=0
Step 3:- while(1) do the following
Step 3.1:- initialize i=0
3.2 :- while(ch=getchar())!= ‘\n’ do
3.2.1 :- text[i]=ch
3.2.2 :- i=i+1
Step 3.3 :- text[i]= ‘\0’
3.4:- if text[0]== ‘\0’ then goto step 4
Otherwise goto step 3.5
Step 3.5 :- initialize j=0
3.6 :- w=w+1
3.7 :- while text[j]!= ‘\0’ do
Step 3.7.1 :- if text[i]= ‘ ’ ||text[i]== ‘\t’ then goto step 3.7.2 otherwise goto step 3.7.3
3.7.2 :- w=w+1
Step 3.7.3 :- j=j+1
Step 3.8 :- i=i+1
3.9 :- c=c+strlen(text)
Step 4 :- print number of characters c
Step 5 :- print number of words w
Step 6 :- print number of lines l
Step 7 :- stop

Aim :- write a program to define a structure student with the fields student number, name,
marks of three subjects and calculate total average marks. And print these details
Algorithm :-
Step 1:- start
Step 2:- define a structure student with following members
(i) declare no as int
(ii) declare name as char type
(iii) declare marks1,marks2,marks3 as type int
(iv) declare total as int
(v) declare arg as type struct student
Step 3 :- declare s as type struct student
Step 4 :- [reading student details]
Read sno
Step 5 :- read s.name
Step 6 :- read s.marks1,s.marks2,s.marks3
Step 7 :- calculate s.total=s.marks1+s.marks2+s.marks3
Step 8 :- calculate s.avg=s.total/3
[printing student details]
Step 9 :- print s.no,s.name,s.marks1,s.marks2,s.marks3,s.total,s.avg
Step 10 :- stop

Aim :-
Write a c program to copy one file to another file
Algorithm :-
Step 1:- start
Step 2:- create a file data.txt in write mode
2.1 :- open file data.txt in write mode fopen=(“data.txt”, “w”)
2.2 :- put(c,fp1)
2.3 :- close the file data.txt
Step 3 :- read file2
Step 4 :- open the file “data.txt” in read mode
Fp1=fopen(“data.txt”,r)
Step 5 :- open the file in write mode
Fp2=fopen(file2, “w”)
Step 6:- while(!feof(fp1)) do
Step 6.1 :- c=getc(fp1)
Step 6.2 :- putc(c,fp2)
Step 7 :- close file data.txt,file2
Step 8 :- open file2 in read mode
Step 8.1 :- while(!feof(fp2)) do
Step 8.1.1 :- c=getc(fp2)
Step 8.1.2 :- print c
Step 9 :- close the file file 2
Step 10 :- stop

You might also like