Professional Documents
Culture Documents
ASSIGNMENT
ON
ALGORITHM DEVELOPMENT
1. FIBONACCI SERIES
2. PALINDROME OF A STRING
4. BUBBLE SORT
5. PYRAMID OF NUMBERS
ALGORITHM:
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’)
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.’)
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.’)
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.’)
4. [Bubble sort]
Repeat for i<-0 to n-1,step i+1
Write(‘enter integer value for i+1’)
Read(a[i]
6. [Finish]
Exit
FUNCTION BUBBLE SORT
1. [Initialise]
Integer i,j,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
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);
}
}
}