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

C

PROGRAMMING AND
(DATASTRUCTURES
Special problem: Strong Numbers
NESO ACADEMY
Strong number: is a number in which the sum of factorial of individual
digits of a number is equal to the original number.

For example:
145. = 1! + 4! + 5!= 1 + 24 + 12O = 145

NESO ACADEMY
Factorial basics
Definition: Factorial of a positive integer the number which
n, is is obtained by
multiplying allpositive integers less than or equal to n.

3! =3*2* 1=6
5! =5*4 *3 *2 * 1 = 120

In general,

n! =n* (n-1)
(n-1) * (n-2) *... * 3 * 2 * 1

NESO ACADEMY
Problem statement:
Write a program to check whether a number is a Strong number
or not?

NESO ACADEMY
Step #1-calculate thefactorial of each digit of a number and add them

q=n,fact=1, result=0; q= 145


while(q != e) rem = 5
{ fact = 1*2*3*4*5 = 120
rem = q%10; result = 120
for(i=1; i<=rem; i++)
q = 14
fact = fact*i; rem = 4
} fact = 1*2*3*4 = 24
result = result + fact; result = 120 + 24 = 144
fact = 1;
q = q/10; q= 1
} rem = 1
fact = 1 = 1
result = 144 + 1 = 145

NESO ACADEMY
Step #2-Check whether the calculated result is equal to the actual number
or not.

if(result == number)
printf("%d is a strong number", number);
else
printf("%d is not a strong number", number);

NESO ACADEMY
strong.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management X
Start here strong.c x
Projects Symbols
1 #include <stdio.h>
OWorkspace 2
3 int main ()
4

int n, q, rem, fact=1, i, result=0;


printf ("Pleaseenter a number: ");
7 scanf ("%d", &n) ;
8
g= n;
10 while(q != 0)
11
12 rem = gtl0;
13 for(i=l; i<=rem: it+)
14
15 fact = fact*i;
16
17 result = result + fact:
18 fact = l:
19 g = q/10;
20
21 if (result n)
22 printf%d is a strong number", n)
23 else
24 printf ("%d is not a strong number", n) ;
25
26 return 0;
27
28
strong.c Code:Blocks 16.01

-
File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management X
Start here strong.c x
Projects Symbols
1 #include <stdio .h>
OWorkspace 2
CAUsers\jaspr\Desktop\strong.exe

Pleaseenter a number: 678


678 is not a strong number
Process returned exe) execution time : 9.646 s
Press any key to Cotinue.

AY
C

PROGRAMMING AND
DATA STRUCTURES
Special Problem -prime numbers
NESO ACADEMY
BASICTERMINOLOGY

Prime number -A number greater than 1iscalled a prime number, if it

hasonly two factors, namely 1and the number itself.

Example: 2, 3, 5, 7, 11...

Composite number -A composite number is apositive integer which is


not prime i.e. which has factors other than 1and itself.

Example: 4, 6, 8, 9, 10.. .

NESO ACADEMY
Question - Is 1 a prime number?

Solution -No. It is not prime number because according to the definition


a

of prime numbers-A prime number is a number which has exactly two


divisors, 1and itself. But 1 has only one divisor ie. itself. Therefore it is not a
prime number.

Another reason - it violates the fundamental theorem of arithmetic

According to this theorem -


Every positive integer greaterthan one can be
written uniquely asthe product of primes.

Example: 9 =3* 3 But we can also write it as:


g= 3 *3* 1
9= 3 * 3 * 1 * 1
9= *33 * 1* 1 *1
NESO ACADEMY
The big picture

NESO ACADEMY
In order tofind whether a number is prime or not,
We first need to calculate the square root of that number and then we divide that
number by numbers less than or equal to the square root of that number. If it is
divisible by any of the numbersthan we can say that the number is not a prime
number else it is a prime number.

Example: suppose we need to find whether 23 is prime number


or not

Step 1 - Take square root of 23. Which is nearly 4.7958 =5


Step 2 - divide 23 by numbers greater than 2 and less than or
equal to 5. If it is divisible by any of those numbers than it is
not prime else it is prime.

As 23 is not divisible by any of those numbers therefore, 23 is a


prime number

NESOACADEMY
#include <stdio. h>
#include <math.h>

int main ()

int X;
int i, vall, val2, count=0;
printf ("Please enter a number (only positive integers) : ") :
scanf ("%d", &x) ;
vall = ceil (sqrt (x)) ; Part 1
val2 = X

for (i=2; i <= vall; it+)

if (val2%i == 0) Part 2
count=l;

i£( (Count == 0 && val2 != 1) ||


va12 == 2 ||val2 == 3)
else
printf ("%d is a prime number", val2) : Part 3
printf ("%d is not a prile number" ,val2) :
return 0;

NESO ACADEMY
PART 1- FINDING THE SQUARE ROOT

sqrt means square root


#include <math.t available in math.h
int main() library

{
int x, vall; Syntax: double sqrt(double x)

vall = ceil (sqrt(x)};


} * ceil function returns a smallest
integer greaterthan or equal
tox
Example: sqrt(2) = 1.414
ceil(1.414) = 2
t available in math.h library

Syntax: double ceil(double x)

NESO ACADEMY
PART 2- CHECK THE DIVISIBILITY

int val2 = X, count = 0;


for(i=2; i<=val1; i++)

if(val2 % i == 0)
count 1;

NESO ACADEMY
PART 3- CHECKWHETHER A NUMBER IS PRIME OR
NOT

if((count == 0 && val2 != 1) || val2 == 2 |||val2 == 3)


printf("%d is a prime number", val2);
else
printf( "%d is not a prime number", val2):

NESO ACADEMY
B prime.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management
Start here x prime.c x
Projects Symbols 2 #inclide <stdio.h>
OWorkspace 3 #include <math.h>
4
5 int ()|
main
6
int x;
int i, vall, val2, count=0;
printf ("Please enter a number (only positive integers): "):
10 scanf ("%d", &x) ;
11
12 vall - ceil (sqrt (x) ):
13 val2= Xi

14
15 for(i=2; i <= vall; it+)
16
17 if (val2%i == 0)
18 count=l;
19
20
21
if( (count =
0 && val2 != 1) ||val12
printf ("$d is a prime number", val2)
=2
:
I|val2 == 3)

22 else
23 printf ("%d is not a prine number", val2) :
24 return 0;
25
26

Logs & others


Code::Blocks x9 Search results x CcCC xBuild log xBuild messages x CppCheck x CppCheck messages x Cscope xDe
C

PROGRAMMING AND
DATA)STRUCTURES
Special problem -Addition
without t operator (part 1)
NESO ACADEMY
Problem statement:
Write a program to add two numbers without using 4

operator.

trvlingjwilr

THATS JUST WEIRD

NESO ACADEMY
Idea is to use increment and decrement
operators

NESO ACADEMY
X = 3, y = 4 1. x = 4, y = 3
Algorithm: 2. x = 5, y = 2
Step 1: X++; y- - ; 3. X = 6, y = 1
Step 2: repeat step 1 until y becomes
4.X = 7, y = 0 stop

NESO ACADEMY
addWoPlus.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management
Start here addWoPlus.c

x
Projects Symbols 1 1/Program to add two numbers without a plus operator
oWorkspace #include <stdio.h>
int main ()

int X, yi
printf ("Enter the twO numbers you want
scanf ("$d %d", &x, &y) :
to add: ") :
8
while (y !- 0)
10
11 x++;
12 y--i
13
14 printf ("Sum of two values is %d", x) :
15 return 0;
16
17

Logs & others

4A Code::Blocks x9 Search xACccc x Buildlog xBuild messages x A CoDCheck x A CoDCheck messages xA


addWoPlus.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools + Plugins DoxyBlocks Settings Help

Management
Start here addWoPlus.c x
Projects Symbol

OWorkspace "CUsers\jaspr\ Desktop\My Cool Programs\addWoPlus.exe"

Enter the two numbers you want to add: 68 7


Sum of two values is 75
Process returned (a)
execution time : 9.403 s
Press any key to continue.

DEMY
C

PROGRAMMING AND
DATA STRUCTURES
Special Problem- Addition
without +operator (part -2)
NESO ACADEMY
addWoPlus.c - Code:Blocks 16.01

File Edit View Search Project

|
*<|)|•D
Build Debug Fortran wxSmith
G::
Tools Tools+

MLiR|Oa|GD|aa|s
Plugins

II
DoxyBlocks Settings

kglobal>
Help

v main():

C
int

Management
Start here x addWoPlus.c x
Projects Symbols
1 /Program to add two numbers without a plus operator
OWorkspace 2 #include <stdio h> .
3 int main ()
4 {

5 int x, yi
6 printf("Enter the two numbers you want to add: ") :
7 scanf ("%d %d", &x, &y):

if (y > 0)
10
11 while (y != 0)
12
13 x++;
14 y--|
15
16
17 else i£ (y < 0)
18
1 while(y!=0)
20
21 X-
22 y++:
23
24
25 printf ("Sum of two values is %d", x):
26 return 0;
27
addWoPlus.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management
Start here x addWoPlus.c x
|
$::IKglobal>
|o|A||GO|a|s v main(0
CT
:int

Projects Symbols
1 I/ Program to add two numbers without a plus operator
OWorkspace 2 #include <stdio .h>
3 int main ()
4

5 int x, yi
6 printf("Enter the two numbers you want to add: ") :
7 scanf("$d $d", &x, &y):

10
11
if (y > 0)

while(y != 0)
X= 2
12
13
14
15
x++i
y--| +|Y= -1

}
o/p = 1
16
17 else if (y < 0)
18
10 T
while(y!=0)
20 {

21 X--;
22 y++:
23
24
25 printf("Sum of two values is %d", x) ;
26 return 0;
27
B addWoPlus.c - Code:Blocks 16.01

Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

G::;
File

Management
|<||•D
Start here x addWoPlus.c x
MLaR|OaGD|a II 8<global> v main():
a|sCT
int

Projects Symbols
1 1/Program to add two numbers without a plus operator
OWorkspace 2 #include <stdio .h>
3 int main ()
4 {

int x, m
6 printf ("Enter the twO numbers you want to add: ") :
7 scanf ("$d $d", &x, &y):

if (y > 0)
10
11 while (y != 0)
12
13 x++;
14 y-
15
16
17 else if (y < 0)
1 {

19 while (y!=0)
20
21 X--;
22 yt+i
23
24
25 printf ("Sum of two values is %d", x);
26 return 0;
27
addWoPlus.c - Code:Blocks 16.01

File Edit

Management
View

4Projects Symbols>
Search Project

|<||•D
Start
Build

here

1
x
Debug Fortran

addWoPlus.c x
wSmith Tools

|G |O|A|O|a
Tools+ Plugins

I/ Program to add two numbers without a plus operator


DoxyBlocks Settings Help

v
als
main(0:
C
int

OWorkspace 2 #include <stdio h> .


3 int main ()

CAUsers\jaspr\ Desktop\|My Cool Programs\addWo Plus.exe"

Enter the two numbers you want to add: 2 -1


Sum of two values is 1
Process returned (8x0) execution time : 6.512 s
Press any key to continue.,
addWoPlus.c - Code: Blocks 16.01

File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help

v main(): int

Management
"CAUsers\jasp\ Desktop\ MyCool Programs\addWoPlus.exe"
Projects S
OWorkspace Enter the two numbers you want to add: -2 1
Sum of two values is -1
Process returned (exgh execution time : 6.498 s
Press any key to continde.

ADEMY
addWoPlus.c - Code:Blocks 16.01

File Edit

Management
View Search Project

Start
Build

here x
Debug Fortran

addWoPlus.c
wSmith Tools

|G |O|A||O|a
Tools+ Plugins DoxyBlocks Settings Help

v
als
main(0:
C
int

x
Projects Symbols
1 1/ Program to add two numbers without a plus operator
OWorkspace #include <stdio .h>
3 int main ()

"CAUsers\jaspr\ Desktop\My Cool Programs\addWo Plus.exe"

Enter the two numbers you want to add: -90 - 80


Sum of two values is -170
Process returned e (exe\ execution time: 10.732 s
Press any key to continue.
Problem statement:
Write a program to add two numbers without using +'
operator.

Idea is to use Half Adder log

NESO ACADEMY
HALF ADDER BASICS

A -
A B C

HA
1 1
B C
1 1

1 1 1
S = A Xor B

C= A AND B

NESO ACADEMY
IMPLEMENTATION
11
5 -> 0101 7 -> 0111
while(b != e) 10 -> 1010 2 -> 0010
{ 15 <- 111 9 <- 1001
sum =
a^b;
Iteration 1:
carry = (a&b)<<1;
a = Sum; Sum 1111 Sum 0101
b= carryj Carry 0000 Carry 0010 -> 0100
Iteration 2:
Sum 0001
Carry 100 -> 1000

Iteration 3:
Sum 1001
Carry 000 STOP

NESO ACADEMY
111
7-> 0111
7 -> 0111
while (b != 0)
14 <- 1110
{
Sum = a^b;
carry = (a&b)<<1; Iteration 1:
a = sum; Sum 0000
b= carry; Carry 111 -> 1110

Iteration 2:
Sum 110
Carry 0000 STOP

NESO ACADEMY
addTwoWoPlus.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wSmith


6::
Tools Tools+ Plugins DoxyBlocks
I|
Settings

kglobal>
Help

|GOD|a sC
Management X
Start here x addWems.cxaddTwoWoPlus.c

x
Projects Symbols
1 /Programto add two numbers without using the plus operator using half adder
OWorkspace 2
3 #include <stdio.h>
4
5 int main ()
6
7 int sum, carry, a, b;
printf("Enterthe two numbers: "):
scanf ("%d %d", &a, &b) ;
10
11 while (b != 0)
12
13 sum = a^b:
14 carry= (a&b) <<l;
15 a Sum;
16 b = carryi
17
18 printf ("Sum of two numbers is: %d", sum) ;
19 return 0;
20
21
addTwoWoPlus.c - Code:Blocks 16.01

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

G::II Bkalobal>
DOlaals CE
Management
"CAUsers\jasp\ Desktop\MyCool Programs\addTwoWoPlus.exe"
4 Projects S
OWorkspace Enter the two numbers:
8 9
Sum of two numbers is: 17
Process returned (exe) execution time : 13.084 s
Press any key to continue.

ADEMY
addTwoWoPlus.c - Code:Blocks 16.01

Debug wSmith
File Edit View Search Project Build Fortran

G:::
Tools Tools+ Plugins

II
DoxyBlocks Settings

<global>
Help

Management X
Start here x addWoPlus.c xaddTwoWoPlus.c x
Projects Symbols
1 1/ Program to add two numbers without using the plus operator using half adder
OWorkspace 2
3 #include <stdio.h>

|
Enter the
"CAUsers\jaspr\

two numbers : -9 -8
Desktop\MyCool Programs\addTwoWoPlus.exe"

Sum of two numbers is:-17


Process returned (8xam execution time : 6.112 s
Press any key to continue.
addTwoWNoPlus.c - Code: Blocks 16.01

File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

Management
"CAUsers\jasp\ Desktop\My Cool Programs\addTwoWoPlus.exe"
4 Projects S
OWorkspace Enter the two numbers:
-7 9
Sum of two numbers is: 83
Process returned (a) execution time : 9.540 s
Press any key to continue.

ADEMY
C

PROGRAMMING AND
DATA STRUCTURES
Special Problem -Fibonacci
series
NESO ACADEMY
Problem statement:
Write program to print Fibonacci series upto n number
of terms.

The term Fibonacci comes from the name of an Italian


Mathematician Leonardo of Pisa, known as Fibonacci.

NESO ACADEMY
WHAT IS FIBONACCI SERIES?

In fibonacci series, next term is obtained by taking sum of


previous two terms

n 1 2 3 4 5 6
fib(n) 1 1 2 5

Mathematically,

fib(n) = fib(n-1) + fib (n-2)

NESO ACADEMY
What is so magical about Fibonacci numbers?

NESO ACADEMY
3 2
1 1

NESO ACADEMY
NESO ACADEMY
NESO ACADEMY
Problem statement:
Write a program to print Fibonacci series upto n number
of terms.

NESO ACADEMY
a = 0; Example: Iteration 3:
b= 1; 1
n = 6
result = 3
for(i=1; i<=n; it+) Iteration 1: =2
a
{
,
printf("%d ", a);
result = a + b;
result
=
=1
b= 3
Iteration 4:
Iteration
5
6:

a = b;
a 1
2 result = 13
b = result;
b= 1 a =8
result =5
Iteration 2:
a =3
b= 13
1
b=5
result = 2
Iteration 5:
a = 1 3
b= 2 result =8
a = 5
NESO ACADEMY b= 8
*fibonacci.c - Code:Blocks 16.01

|||•D
File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help

G::IBKalobal>
Management
A|O|AOolaalsCE
Start here x*fibonacci.c x

Projects Symbols 1 1/Program to print bonacci series Dt n

OWorkspace 2 #include <stdio.h>


3 int main ()
4
5 int a, b, result, n, ii
6 pantf ("Enter the number of terms: "):
7 scnf ("$d", &n) ;

a=0;
10 b=l;
11
12 for(i=1l; i<=n; it+)
13
14 printf ("%d ",a)
15 result =a + b;
16 a = b:
17 b= result:
18
19
20
fibonaccic - Code:Blocks 16.01

File Edit View Search Project

|<|)|•D
Build Debug Fortran wxSmith

G::
Tools Tools+ Plugins

I
DoxyBlocks Settings

<alobal>
Help

X
Management
Start here x fibonacci.c x
Projects Symbols>
1 1/Program to print fibonacci series upta n

OWorkspace 2 #include <atdio.h>

CAUsers\jaspr\ Desktop\MyCoolPrograms\fibonacci.exe"
Enter the number of terms: 6
e 1 1 2 35
Process returned (8xe) execution tme : 6.031 s
Press an key to continue.

You might also like