ASSIGNMENT NO:1
ASSIGNMENT
ON
ALGORITHM DEVELOPMENT
SUBMITTED TO, SUBMITTED BY,
Mr.G.SURESH KUMAR,M.E. R.BRINTHA.
CONTENTS
1. FIBONACCI SERIES
2. PALINDROME OF A STRING
3. GENERATING PRIME NUMBERS
4. BUBBLE SORT
5. PYRAMID OF NUMBERS
ALGORITHM:
An algorithm is a finite set of well-defined instructions for
accomplishing some task which, given an initial state, will result in a
corresponding recognisable end-state. Algorithms can be implemented by
computer programs, although often in restricted forms; an error in the
design of an algorithm for solving a problem can lead to failures in the
implementing program.
Characteristics of an Algorithm:
(i) Finiteness:
It terminates after a finite number of steps
(ii) Definiteness:
It is rigorously and unambiguously specified
(iii) Input:
The valid inputs are clearly specified
(iv) Output:
It can be proved to produce the correct output given a valid
input
(v) Effectiveness:
The steps are sufficiently simple and basic.
1.FIBONACCI SERIES
1. [Initialize]
Integer ini_val <- 0,final_val <- 1,no,count,temp_val
2. [Write]
Write(‘ Enter the length of series’)
Write (‘ini_val,final_val’)
3. [Get the value]
Read no
4. [Generating fibonacci series]
Repeat for count <-1 to n-2,step count+1
Temp_val <- ini_val+final_val
Write(‘temp_val’)
Ini_val <- final_val
Final_val <- temp_val
5. [Finish]
Exit
CODING:
#include<stdio.h>
#include <conio.h>
void main()
{
float a,maxn,b,c,d;
clrscr();
printf("Enter maximumn number of values in series \n");
scanf("%f",&maxn);
a=1;b=1;
printf("\n\n") ;
printf("%1.0f\n",a);
printf("%1.0f\n",a);
for(d=1;d<=maxn-2;d++)
{
c=a+b;
b=a;
a=c;
printf("%1.0f\n",c) ;
}
getch();
}
2.PALINDROME OF A STRING
1. [Initialize]
Character strsrc[size],strtemp[size]
2. [Write]
Write(‘ Enter the string.’)
3. [Get the value]
Read strsrc
4. [Checking for palindrome]
If strcmp (strsrc,strtemp==0)
Write(‘Palindrome’)
Else
Write(‘Not a palindrome’)
5. [Finish]
Exit
CODING:
#include<stdio.h>
#include<string.h>
#define size 26
void main()
{
char strsrc[size];
char strtmp[size];
clrscr();
printf("\n Enter String:= "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" ispalindrome",strsrc);
else
printf("\n Entered string \"%s\" is not
palindrome",strsrc);
getch();
}
3.GENERATING PRIME NUMBERS
1. [Initialize]
Integer i,j,n
2. [Write]
Write(‘Enter the number upto which we have to find the prime
number.’)
3. [Get the value]
Read n
4. [Print the value]
Write n
5. [Generating prime numbers]
Repeat for j<-2 to n, step j+1
If i%j <- 0
Break
If i<-j
Write(‘i’)
6. [Finish]
Exit
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf(" Enter the number upto which we have to find the prime number: ");
scanf("%d",&n);
printf("\n");
for(i=2;i<=n;i++)
{
for(j=2;j<=i-1;j++)
if(i%j==0) break; //Number is divisble by some other number. So break out
if(i==j) printf("\t%d",i); //Number was divisible by itself (that is, i was same
as j)
}//Continue loop upto nth number
getch();
}
4.BUBBLE SORT
1 . [Initialize]
Integer a[100],n,i
2. [Write]
Write(‘Enter integer value for total number.’)
3. [Get the value]
Read n
4. [Bubble sort]
Repeat for i<-0 to n-1,step i+1
Write(‘enter integer value for i+1’)
Read(a[i]
5. [Calling the function]
Bubble(a,n)
Write(‘finally sorted array is’)
Repeat for i<-0 to n-1, step i+1
Write(‘a[i]’)
6. [Finish]
Exit
FUNCTION BUBBLE SORT
1. [Initialise]
Integer i,j,t
2. [Sorting the elements]
Repeat for I<-n-2 to 0,step i-1
Repeat for j<-0 to I,step j+1
if a[j]>a[j+1]
t<-a[j]
a[j]<-a[j+1]
a[j+1]<-t
3. [Finish]
Exit
CODING:
#include<stdio.h>
#include<conio.h>
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
} }
void main()
{
int a[100],n,i;
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
}
5.PYRAMID OF NUMBERS
1. [Initialize]
Integer n,n<-1
2. [Get the value]
Read (n)
3. [Generating the numbers]
Repeat for I<-0 to n-1, step i+1
Repeat for j<-0 to I,step j+1
Gotoxy(2*(i+1),(n-j))
Write(‘n-I’)
Gotoxy(2*(2*n-i-1),(n-j))
Write(‘n-I’)
End for
4. [Finish]
Exit
CODING:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("Enter an integer\n");
int n,n1=1;
scanf("%d",&n);
clrscr();
for(short i=0;i<n;i+=1)
{
for(short j=0;j<=i;j+=1)
{
gotoxy(2*(i+1),(n-j));
printf("%d",n-i);
gotoxy(2*(2*n-i-1),(n-j));
printf("%d",n-i);
}
}
}