#Include #Include Void Int
#Include #Include Void Int
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 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.
#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
#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
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.
#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();
}
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 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