Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Computer Concepts

and Problem solving

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().

What is square root of any 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.


Square root calculation
Square root algorithm in C
Algorithm of program ( Square root in C )

1.Start
2.Accept one number from user.
3.Use the function sqrt() to find square root.
4.Print the result.
5.End

Program of square root in C

#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 …..

•Two is the only even Prime number.


•Every prime number can be represented in form of 6n+1 or 6n-1 except
the prime number 2 and 3, where n is a natural number.
•Two and Three are only two consecutive natural numbers that are prime.
Algorithm for given number is prime
or not
 Step 1: Start
 Step 2: Read number n
 Step 3: Set f=0
 Step 4: For i=2 to n-1
 Step 5: If n mod i=0 then
 Step 6: Set f=1 and break
 Step 7: Loop
 Step 8: If f=0 then
 print 'The given number is prime'
 else
 print 'The given number is not prime'
 Step 9: Stop
C program for given number is prime or not

 #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

 C Program to find the simple interest


 Algorithm
 Step 1:StartStep
 2:Read Principal Amount, Rate and TimeStep
 3:Calculate Interest using formula SI= ((amount*rate*time)/100)
 Step 4:Print Simple Interest
 Step 5:Stop
Recursive function- A function is said to
be recursive when it calls itself

Ex 1. factorial and Fibonacci series

5!=5*4*3*2*1
5! = 5 * 4 !
N ! = N * (N-1)!

The factorial of a number is defined as:


f(n) = n * f(n-1) → for all n >0
f(0) = 1 → for n = 0
factorial(n):
if n is 0
return 1
return n * factorial(n-1)
 Space complexity
 For every call to the recursive function, the state is saved onto the call stack, till
the value is computed and returned to the called function.
 Here we don’t assign an explicit stack, but an implicit call stack is maintained
 f(6) → f(5) → f(4) → f(3) → f(2) → f(1) → f(0)
f(6) → f(5) → f(4) → f(3) → f(2) → f(1)
f(6) → f(5) → f(4) → f(3) → f(2)
f(6) → f(5) → f(4) → f(3)
f(6) → f(5) → f(4)
f(6) → f(5)
f(6)
 The function in bold is the one currently being executed. As you can see for f(6) a
stack of 6 is required till the call is made to f(0) and a value is finally computed.
Hence for factorial of N, a stack of size N will be implicitly allocated for storing the
state of the function calls.
 The space complexity of recursive factorial implementation is O(n)
 The factorial of a positive number n is given by:

 factorial of n (n!) = 1 * 2 * 3 * 4 *... * n


 The factorial of a negative number doesn't exist. And the factorial of
0 is 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

 C Program to print the kth element in the array.


To find a particular element in an array for example to find a particular student (Raj)
is there in a BCA class of 100 students
Here raj -> k
100-> n
Class ->BCA

1. Declare an array of some fixed capacity, say 100(n)


2. Take size from users as an input.
3. Using for loop, define the elements of the array according to the size.
4. Enter the position( k) where we want to access the element
1.#include <stdio.h>
2.int main()
3.{
4.int arr[100], len, i, j, temp, n;
5.printf("Enter the size of array");
6.
7.scanf("%d", &len);
8.printf("\n Enter the array elements");
9.
10.for (i = 0; i < len; i++)
11.{
12.scanf("%d", &arr[i]);
13.}
14.
15.printf("\n Enter Which kth Number You want");
16.scanf("%d", &n);
17.printf("\n The %d th kth number is: %d", n, arr[n - 1]);
18.return 0;
19.}
Output- find kth element in array

 Enter the size of array4



 Enter the array elements
 12
 13
 17
 20
 Enter Which kth Number You want 4

 The 4th kth number is: 20
Armstrong number
 Armstrong Number in C
 Before going to write the c program to check whether the number is Armstrong
or not, let's understand what is Armstrong number.
 Armstrong number is a number that is equal to the sum of cubes of its digits. For
example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
 Let's try to understand why 153 is an Armstrong number.
1. 153 = (1*1*1)+(5*5*5)+(3*3*3)
2. where:
3. (1*1*1)=1
4. (5*5*5)=125
5. (3*3*3)=27
6. So:
7. 1+125+27=153
 371 = (3*3*3)+(7*7*7)+(1*1*1)
 where:
 (3*3*3)=27
 (7*7*7)=343
 (1*1*1)=1
 So:
 27+343+1=371
 371 is Armstrong number
Armstrong number – c program
1.#include<stdio.h>
2. int main() 16.else
3.{ 17.printf("not armstrong numbe
4.int n,r,sum=0,temp; r");
5.printf("enter the number="); 18.return 0;
19.}
6.scanf("%d",&n);
7.temp=n; Output:
enter the number=153 armstrong number enter the
8.while(n>0) number=5 not armstrong number
enter the number=153 armstrong number
9.{ enter the number=5 not armstrong number
10.r=n%10;
11.sum=sum+(r*r*r);
12.n=n/10;
13.}
14.if(temp==sum)
15.printf("armstrong number ");
Algorithm – Armstrong number –
top- down approach
 Algorithm to find whether number is Armstrong Number or Not – Top down approach is solving a
task from variable declaration, input, processing and produce output in an effective manner
 Step 1: Start
 Step 2: Declare Variable sum, temp, num
 Step 3: Read num from User
 Step 4: Initialize Variable sum=0 and temp=num
 Step 5: Repeat Until num>=0
 5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]
 5.2 num=num/10
 Step 6: IF sum==temp
 Print "Armstrong Number"
 ELSE
 Print "Not Armstrong Number"
 Step 7: Stop
Top down
approach
Thank you ….

You might also like