Category Archives: play with numbers

Narcissistic Number (Armstrong Number)

Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits.

e.g.

153 = 13+53+33
1634 = 14+64+34+44

C code for finding whether given number is Narcissistic Number or not.

Code provided by : Manoj

Print Binary representation of a number in c

C code to print binary representation of a number :

 

 

Check whether given number is palindrome or not

Approach :

store the given number into some other variable then reverse the number and compare it with stored no. If both the numbers are same then the given number is palindrome.

Example :

Given No             Reversed No                     Equal               Palindrome

123                           321                                       No                            No

121                            121                                      Yes                         Yes

————————————————————————————————————————————————————————–

C code to check whether given number is palindrome or not.

Code provided by Lalit Kumar

Print given square matrix in spiral order

Print givensquare matrix in spiral order :

Complexity : O(n^2)

Approach :

Step1 : write a 4 for loop for printing boundry

step 2: Apply the step1 for inner martrix by modifying starting point and ending point of matrix

step 3: Aplly step2 untill the matrix size becomes 1 or 0

end

Input :

—————-

1     2     3    4

12  13  14  5

11   16 15   6

10   9     8    7

—————-

Output :

——————————————————-

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

——————————————————-

C code to print given square matrix in spiral order :

 

find pair of numbers in array that add to given sum

Find pair of numbers in array that add to given sum :

Complexity: O(n logn)

Approach :

step 1: Sort an array using efficient sorting method

step 2: use two variable i & j , initialize i as starting position of array (i=0) and j as last position of array (j=n-1). Where n is number of element in array.

step 3:  if(A[i]+A[j]<x) i++

else if(A[i]+A[j]>x) j–

else (A[i]+A[j]==x) print A[i] , A[j] and  i++

step 4: Repeat step 3 untill i<j

 

Code to Find pair of numbers in array that add to given sum :

 

Calculating Factorial of large numbers in c

As we all know there is memory limitation in C. If we want to store number which is more than of 4 byte we cannot use int data type. We can use long data type to store value which is upto 8 byte. What after that?. If our value is of more than 8 bytes then we have to implement our own method to store value.

Here is the implementation of calculating factorial for large numbers like 100!

Explanation:

This program works on basic method of multiplication of a two numbers.

1234

X 15

Ans: 18510

Steps are:

Step                                       Digit                       Carry

15*4 = 60;                            0                              6

(15*3)+6=51                       1                              5

(15*2)+5=35                       5                              3

(15*1)+3=18                       8                              1

After this carry digits are extracted and stored in array.  ( if our carry was 25 then we must extract 2 and 5 then store it in array )

So output is: 18510

In program:

Array of 200 lengths is taken to store digits of multiplication result.

Index variable indicates the length of output..

tmp is used to store carry value;

————————————————————————————————————————–

Calculating Factorial of large numbers in c

Her we have taken array size of 200 so we can calculate factorial of a number whose digits are 200. if you want to calculate for more than 200 just increase the size of array.

 

 

Fibonacci number series program in c

The Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence.

0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_n = F_{n-1} + F_{n-2},\!\,

with seed values

F_0 = 0,\; F_1     = 1.
To Display Fibonacci number series program in c.

 

Factorial program in C

Factorial Program in C .

the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5  \times  4  \times  3  \times  2  \times  1 = 120.  \

The value of 0! is 1

C program to find factorial of a number using recursion .

 

C program to print pascal triangle

Pascal’s Triangle : Pascal’s triangle is a triangular array of the binomial coefficients.

Pascal's Triangle
Pascal’s Triangle

To read more about pascal’s triangle click here

C program to create pascal’s triangle

pascal triangle printing code .