Professional Documents
Culture Documents
C Lec 51-55
C Lec 51-55
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
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
-
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
AY
C
PROGRAMMING AND
DATA STRUCTURES
Special Problem -prime numbers
NESO ACADEMY
BASICTERMINOLOGY
Example: 2, 3, 5, 7, 11...
Example: 4, 6, 8, 9, 10.. .
NESO ACADEMY
Question - Is 1 a prime number?
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.
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
if (val2%i == 0) Part 2
count=l;
NESO ACADEMY
PART 1- FINDING THE SQUARE ROOT
{
int x, vall; Syntax: double sqrt(double x)
NESO ACADEMY
PART 2- CHECK THE DIVISIBILITY
if(val2 % i == 0)
count 1;
NESO ACADEMY
PART 3- CHECKWHETHER A NUMBER IS PRIME OR
NOT
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
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
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
File Edit View Search Project Build Debug Fortran wxSmith Tools Tools + Plugins DoxyBlocks Settings Help
Management
Start here addWoPlus.c x
Projects Symbol
DEMY
C
PROGRAMMING AND
DATA STRUCTURES
Special Problem- Addition
without +operator (part -2)
NESO ACADEMY
addWoPlus.c - Code:Blocks 16.01
|
*<|)|•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
v
als
main(0:
C
int
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 ()
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
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"
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.
NESO ACADEMY
WHAT IS FIBONACCI SERIES?
n 1 2 3 4 5 6
fib(n) 1 1 2 5
Mathematically,
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
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
|<|)|•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
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.