Professional Documents
Culture Documents
Ccps-Module 5
Ccps-Module 5
MODULE 5
(S.VINITA – I BCA –B CLASS TEACHER)
Factoring methods
FINDING
What THE SQUARE
is square ROOT
root of any OF A NUMBER
number ?
Square root of any number is just the reverse of squaring of that number. We already know that
square of any number is the number multiplied by itself. i.e., if we suppose to find the square of
any number say 3, then its square is 9.
Coming to square root,
Square root of any number (say) 81 is 9. Lets find it graphically.
Finding square root of any number is pretty simple. In this post, will explain you step-by-
step to find square root of any given number using a predefined function sqrt().
1.Start
2.Accept one number from user.
3.Use the function sqrt() to find square root.
4.Print the result.
5.End
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,result;
printf("Enter any number :\t");
result=sqrt(n);
printf("\nSquare root of given number is : %d",result);
getch();
}
Generating prime numbers
Prime Numbers
A prime number is a natural number greater than 1, which is only divisible by 1
and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 …..
#include<stdio.h> f=1;
#include<conio.h> break;
void main( ) }
{ }
clrscr();
printf("Enter the number: "); if(f==0)
int n,i,f=0; printf("The given number is prime");
scanf("%d",&n); else
for(i=2;i<n;i++) printf("The given number is not
prime");
{
if(n%i==0) getch();
{ }
Simple interest
5!=5*4*3*2*1
5! = 5 * 4 !
N ! = N * (N-1)!
You will learn to find the factorial of a number using recursion in this
example. Visit this page to learn how you can find the factorial of a
number using a loop.
C program for factorial using
recursion
Factorial of a Number Using Recursion long int multiplyNumbers(int n) {
#include<stdio.h> if (n>=1)
long int multiplyNumbers(int n); return n*multiplyNumbers(n-1);
int main() { else
int n; return 1;
printf("Enter a positive integer: "); }
scanf("%d",&n); Output
printf("Factorial of %d = %ld", n,
multiplyNumbers(n));
Enter a positive integer: 6
return 0;
Factorial of 6 = 720
}
Fibonacci series
Algorithm
Step 1:StartStep
2:Initialize counter=0
Step 3:Read term as num
Step 4:Read the first number as first
Step 5:Read the second number as second
Step 6:Repeat steps 7 to 10 till counter is less than num
Step 7:Declare sum=first + second
Step 8:Print the value of sum
Step 9:Assign the value of second to first and sum to second
Step 10:Increment the counter by 1
Step 11:Print the Fibonacci series
logic for Fibonacci numbers
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers
is defined by the recurrence relation
Fn = Fn-1 + Fn-2
with seed values
F0 = 0 and F1 = 1.
Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,…..
Fibonacci series – C program
#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
Arrays
Array is a container which Array Representation illustration, following are the
can hold a fix number of Arrays can be declared in important points to be
items and these items various ways in different considered.
should be of the same type. languages. For illustration,
Most of the data structures let's take C array
make use of arrays to Index starts with 0.
declaration.
implement their algorithms.
Following are the important
terms to understand the Array length is 10 which
Array Declaration means it can store 10
concept of Array.
Arrays can be declared in elements.
Element − Each item stored various ways in different
in an array is called an languages. For illustration,
element. let's take C array Each element can be
Index − Each location of an declaration. accessed via its index. For
element in an array has a example, we can fetch an
numerical index, which is element at index 6 as 9.
Array Representation
used to identify the
element. As per the above
Array declaration
Array Representation
Arrays can be declared in various ways in different languages.
For illustration, let's take C array declaration.
As per the above illustration, following are the important points to be considered.
•Index starts with 0.
•Array length is 10 which means it can store 10 elements.
Removal of duplicates from an
array
1. Create an auxiliary array temp[] to store unique
elements.
2. Traverse input array and one by one copy unique
elements of arr[] to temp[]. Also keep track of count
of unique elements. ...
3. Copy j elements from temp[] to arr[] and return j.
Find the kth element in an array