Module 3
Module 3
Suppose for example we do not know the square root of 36. If we guess 9 as
the square root of 36, it is too high because 9X9 =81 as per the equation
(1).Since our guess of 9 is too high, so we may try with 8. There 8x8 =64
which is still higher than 36 but better than our initial guess of 9.
The calculated above value 6.0152 =36.6025 which is only slightly greater than
the square value 36 that we are seeking. However, we shall assume this is a
good strategy and continue with the development of the algorithm.
Condition for terminating the loop:
As observed we know that the successive iterations produces, closer and
closer approximations to the square root.
or perhaps m/2 as our initial guess.
#include <stdio.h>
#define ACCURACY 0.001 // tolerance : you can set by yourself according to
requirement
int main()
{
float number,guess,square_root;
printf("Enter a number to find square root of it:\n");
scanf("%f",&number);// storing the number
guess=number/2;// initializing guess number
while((guess*guess - number) > ACCURACY)
//loop runs until the square root is found
{
guess = (guess + number/guess )/2;// updating the guess number
}
square_root = guess;//assigning the value of guess number to square root.
printf("square root of %.2f = %.4f",number,square_root);//final answer
}
For example,
#include <stdio.h>
#include<conio.h>
int smallestDivisor(int n);
void main()
{
int n;
printf("Enter the number whose smallest divisor needs to be calculated\n");
scanf("%d",&n);
int result=smallestDivisor(n);
printf("Smallest Divisor of %d is %d\n",n,result);
getch();
}
// Function to find the smallest divisor
int smallestDivisor(int n)
{
// if divisible by 2
if (n % 2 == 0)
return 2;
Output:
Case 1: Number is even
From the above representation, thus the problem of finding the GCD of
numbers 18 and 36 can be reduced to the below problem.
Sub Problem 1:
The above sub problem can be further reduced to another sub problem as shown
below.
Sub Problem 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,rem;
clrscr();
printf("enter any two numbers:");
scanf("%d%d",&a,&b);
while(b!=0) //check for b=0 condition because in a/b ,b should not equal to
zero
{
rem=a % b;
a=b;
b=rem;
}
printf("GCD of two numbers is:%d\n",a);
getch();
}
Output:
Algorithm(Sieve of Eratosthenes for generating prime factors of a number)
#include <stdio.h>
int primes[100001];
void sieveOfEratosthenes(int N)
primes[1] = 1;
if (primes[i] == i)
for (int j=i*i; j<N; j+=i) //incrementing j with 3 to check prime number
array positions at multiples of 3 everytime
if (primes[j]==j)
}//end of function
sieveOfEratosthenes(num);
int factor;
while(num != 1)
factor = primes[num];
num /= factor;
return num;
void main()
int n, c=0;
scanf("%d",&n);
c=findPrimeFactors(n);
printf("%d",c);
getch();
Output:
Pseudo Random Number Generator
(PRNG) by linear congruential method.
There are many methods for generating pseudo random numbers,one such
method is linear congruential method.
generated before the sequence begins to repeat.
Example:
m=4096, b=853,a=109;
#include<stdio.h>
#include<conio.h>
// m=4096, a=109,b=853
scanf("%d%d%d",&m,&a,&b);
while(i<=(m-1))
f=((a*f+b)% m);
i=i+1;
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int x;
int n;
scanf("%d",&x);
scanf("%d",&n);
int result=power(x,n);
getch();
}
int power(int x,int n)
int product=1;
int psequence=x;
while(n!=0)
if(n % 2==1)
n=n/2;
psequence=psequence*psequence;
return product;
Output:
Array Techniques
Example : Above array stores the marks of 5 students.
Algorithm Development
C program to reverse the elements of an array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100], i, j, Size, Temp;
printf("\nPlease Enter the size of an array: ");
scanf("%d",&Size);
printf("Inserting %d elements into the array\n",Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}
getch();
}
Output:
C Program for array counting or histogramming
#include <stdio.h>
#include<conio.h>
void main()
{
//Initialize array
int arr[] = {100, 20, 80, 30, 21, 25, 78, 99, 100};
Output:
Write a C Program that partitions the given elements of an array
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,a[50],n,x,temp;
printf("Enter size of the array\n");
scanf("%d",&n);
printf("Entering the %d elements of the array\n",n);
for(i=0;i<n;i++)
{
printf("Enter element %d \n",i+1);
scanf("%d",&a[i]);
}
printf("Entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
printf("Enter the element of array for partitioning\n");
scanf("%d",&x);
i=0;
j=n-1;
while(a[i]<=x)
i=i+1;
while(a[j]>x)
j=j-1;
if(a[j]>x)
j=j-1;
while(n>0 && i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i=i+1;
j=j-1;
while(a[i]<=x)
i=i+1;
while(a[j]>x)
j=j-1;
p=j;
}
printf("partitioned position is %d\n",p);
printf(" Array after partitioning as per given element %d is\n",x);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Output:
C Program to partition the array as per kth smallest entered element:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,l,u,a[50],n,x,k,temp;
printf("Enter size of the array\n");
scanf("%d",&n);
printf("Entering the %d elements of the array\n",n);
for(i=0;i<n;i++)
{
printf("Enter element %d \n",i);
scanf("%d",&a[i]);
}
printf("Entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
printf("Enter position for kth smallest element\n");
scanf("%d",&k);
l=0;
u=n-1;
while(n>0 && l<u)
{
i=l;
j=u;
x=a[k];
while(i<=k && j>=k)
{
while(a[i]<x)
i=i+1;
while(x<a[j])
j=j-1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
i=i+1;
j=j-1;
}
if(j<k)
l=i;
if(i>k)
u=j;
}
printf(" Array after partitioning as per %d th smallest element\n",k);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
printf(" The %d th smallest element in the partitioned array is %d \n",k,x);
getch();
}
Output:
Multiplication of Matrices
To multiply 2 matrices, we are taking input from the user for row number, column number,
first matrix elements and second matrix elements. Then we are performing multiplication on
the matrices entered by the user.
In matrix multiplication first matrix first row elements are multiplied by all column
elements of second matrix. Similarly, second row elements of first matrix are multipled by
all column elements of second matrix and so on.
Let's try to understand the matrix multiplication of 2*2 and 2*3 matrices by the figure given
below: