0% found this document useful (0 votes)
4 views

Week_01-Learn Dsa With c++

The document provides an introduction to programming concepts, focusing on data structures and algorithms (DSA) using C++. It covers the evolution of computing from basic counting methods to the use of binary numbers, transistors, and high-level programming languages. Additionally, it explains fundamental programming constructs, including data types, variables, flowcharts, pseudocode, and control structures in C++.

Uploaded by

Priyanshu Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week_01-Learn Dsa With c++

The document provides an introduction to programming concepts, focusing on data structures and algorithms (DSA) using C++. It covers the evolution of computing from basic counting methods to the use of binary numbers, transistors, and high-level programming languages. Additionally, it explains fundamental programming constructs, including data types, variables, flowcharts, pseudocode, and control structures in C++.

Uploaded by

Priyanshu Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

WEEK :: 01

LEARN DSA
WITH C++

Check Profile My profile


LEARN DSA WITH C++
WEEK :: 01 DAY: 01 DATE: - 17-04-2023

Introduction To Programming
10 years ago, Once upon a time there was a man who had 5 pet animals such as 2 Goats, 2 Cows and
one Dog. He was counting these animals using stone. He sees an animal and holds a stone. Thus he
counted those animals.

Then time changed, people invented decimal numbers for calculations. Decimal numbers are 0 to 9
(0,1,2,3,4,5,6,7,8, 9). Now people can count from 0 to 1000. But that time was impossible.

Now consider one man doing business. He has four notebooks for calculation. And every notebook
has 1000 pages. Now, if you want to calculate those notebooks one by one. Again it's impossible. If you
calculate it’s a high chance to make a mistake.

Then, Charles Babbage credited a computer called a mechanical computer in 1822. Basic meaning of
Computer - To Calculator.
That time, A decimal number was used on the computer. Computer size was one room.

A few years later, the transistor was invented. A transistor is a semiconductor device used to amplify or
switch electrical signals and power. Transistors provide two numbers 0 and 1 (Binary number). Generate
1 when the transistor is on and 0 when the transistor is off.

Decimal Calculation:-

15 47 56
+ 34 +26 +3 2
—--------- —------ —------
49 73 88

Binary Calculation:-

0 0 1 1
+ 0 + 1 + 0 + 1
—------------- —------------- —------------- —-------------
0 1 1 10
Decimal To Binary :

0 1 2 3 4 5 6 7

0 1 10 111 100 101 110 111


Binary To Decimal: Exp:- 1 (1 0 1 1 0)

1 0 1 1 0

1 X 2⁴ 0 X 2³ 1 X 2² 1 X 2¹ 0 X 2⁰

16 0 4 2 0

Result = 16 + 0 + 4 + 2 + 0 = 22

Exp:- 2 (1 1 0 1 0 1)

1 1 0 1 0 1

1 X 2⁵ 1 X 2⁴ 0 X 2³ 1 X 2² 0 X 2¹ 1 X 2⁰

32 16 0 4 2 1

Result = 32 + 16 + 0 +4 + 2 +1 = 55

Decimal To Binary : Exp:- 1 [ 35 ]

2 35

2 17 1

2 8 1

2 4 0

2 2 0

2 1 0

2 0 1

Result = 1 0 0 0 1 1 [ **Note: Result take reverse. ]


Exp:- 2 [67]

2 67

2 33 1

2 16 1

2 8 0

2 4 0
2 2 0

2 1 0

2 0 1

Result = 1 0 0 0 0 1 1

Transistor ::
One transistor 2 value 1 either 0.
0 1

So, One memory space: we can put 2 different pieces of data.

1 0

Two memory spaces: we can put 4 different pieces of data.

0 0 0 1 1 0 1 1

Four memory spaces: we can put 16 different pieces of data.

0000 0001 0010 0011 010 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0

Moore's Law : Moore's law is the observation that the number of transistors in an integrated
circuit (IC) doubles about every two years.

Simple meaning: If there is a transistor in one place. After 2 years there should be 2 transistors in the
same place.

50 transistor After 2 Years 100 transistor


Communication with the computer:
At first we were using assembly language to communicate with the computer. And the assembler
converts it to binary code. It was very complicate and code lenght so long.

Now, we use high level languages like C++, Java, and Python. And the compiler or interpreter converts
the code into binary code.

Machine Language :: Machine language is a low-level language made up of binary


numbers or bits that a computer can understand. It is also known as machine code.
Assembly Language:: An assembly language is a type of low-level programming language that
is intended to communicate directly with a computer's hardware.

High Level Language:: A high-level language is any programming language that enables
development of a program in a much more user-friendly programming context and is generally
independent of the computer's hardware architecture.

Assembler: Assembler is a program for converting instructions written in low-level assembly code
into relocatable machine code and generating long information for the loader.

Compiler: A compiler is a special program that translates a programming language's source


code into machine code, bytecode or another programming language.

Interpreters : interpreter is a computer program that directly executes instructions written in a


programming or scripting language, without requiring them previously to have been compiled into a
machine language program.

Importance of Algorithms?
Using Algorithms, our code executes very fast and takes less space for store.

Time Complexity: Total time taken by the algorithms with respect to input size.
Space Complexity: the amount of memory space required to solve an instance of the
computational problem as a function of characteristics of the input.

Importance of DSA?
1. Take less Time
2. Take less space

FLOWCHART

FlowChar : A flowchart is a type of diagram that represents a workflow or process.

Start / End Input / Output Process Decision Flow

Exp:- 01 How did you decide whether to go for a walk or stay alone on a rainy day
FLOWCHART PSEUDO CODE

1. Know about whether


2. It is rainy or not
3. No rain, go for a walk, It’s rainy so,
don't go walking.

PSEUDO CODE : Pseudocode is a "text-based" detail (algorithmic) design tool.


Exp:- 02 :: Add two Number

FLOWCHART PSEUDO CODE

1. Read Two number


2. number + number
3. Output Sum

Exp:- 03 :: Find the Cube of Number?


FLOWCHART PSEUDO CODE

1. Read number
2. number * number * number
3. Output cube

Exp:- 04 :: MNC offer letter?

FLOWCHART PSEUDO CODE

1. See Letter
2. Take decision according to package
3. Package suitable so accept or not
satisfy don't go.

Exp:- 05 :: Check Number Even and Odd?


FLOWCHART PSEUDO CODE

1. Read num
2. Check remainder
3. Remainder is 0 = Even
4. Remainder is not 0 = Odd
5. Print result.

Exp:- 06 :: Sum of n natural numbers?

FLOWCHART PSEUDO CODE

1. Read num
2. Take variable sum = 0, int = 1
3. Check condition int <= n
4. If break loop Print sum

Exp:- 07 :: Find it is prime or not?


FLOWCHART PSEUDO CODE

1. Read num
2. Number divided by 2
3. Check condition num < divider
4. Agine check condition num%divider
=0
5. It is 0= print Not Prime, 0 != divider
increase 1
6. When condition divider < num false
print Prime

Click Here:: Pradum Singha


INTRODUCTION to C++ and Write First Code

WEEK :: 01 DAY: 02 DATE: - 18-04-2023

COMPUTER MEMORY UNIT ::


One Transistor represents one 1 bit. Every bit stores two values 1 or 0. Smallest unit of memory is a bit.

1 Lak Transistor 1 Lak bit memory

1 byte = 8 bit

0 1 0 1 1 0 0 1

Memory Unit:

1 Byte 1 KB 1 MB 1 GB

8 bit 2^10 Byte 2^10 KB 2^10 MB

Binary number:
Computers understand only binary numbers.
INTRODUCTION TO C++

C++ was developed as an extension of C, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ supports classes and objects, while C does
not.

First code::

int main() int main()


{ {
// code cout<<” Hello Coder Army”; << :- Insertion operator
Return 0; return 0; >> :- Extraction operator
}; }; cout:- Print Output
#include<iostream> #include<iostream>
using namespace std; the iostream library is an object-oriented library that
provides input and output functionality using streams
using namespace std;
int main() we can use names for objects and variables from the
{ standard library. helps in avoiding the ambiguity that
may occur when two identifiers have the same name.
cout<<"Hello CoderArmy";
Int main()
return 0; int main represents that the function returns some
}; integer even '0' at the end of the program execution.
Starting point of Program.
Return 0
'0' represents the successful execution of a program.

VARIABLE & DATA TYPES:


Data Types is a Type of data.
Number System = 1, 3, 5, 2.1, 3.1, 0.123, 1.0235

int(integer) = 1, 3, 5,
Float(Floating Point )= 2.1, 3.1
Double( Double Floating Point)= 0.123, 1.0235

Alphabet :- A,V,R,E,J
char(Character )= A,V,R,E,J
bool (Boolean) = Yes/no true/false
Differents types of Data Types:

Size of Data Types in Memory::


Reserved Words in c++::

VARIABLE ::
Variables are containers for storing data values.
int num = 500;

int = Data type


num = Variable name
500 = Value
Print ➖

Print int Print Char

#include<iostream> #include<iostream>
using namespace std; using namespace std;

int main() int main()


{ {
// integer // Character
int num = 500; char c = 'a';
cout<<num; cout<<c;
return 0;
}; return 0;
};

Print double Print float

#include <iostream> #include<iostream>


using namespace std; using namespace std;

int main() int main()


{ {
// double // float
double num = 93.3265; float d = 1.32;
cout << num; cout<<d;

return 0; return 0;
}; };

Comment :- Comments can be used to explain code, and to make it more readable. Complier is not execute
the comment.
// This is a comment Single line comment
/* The code below will print the words Hello World!
to the screen, and it is amazing */ Multiline Comment

How to store Negative Number(-) in memory :-


Num = 9 => 1001
= - 9 => Binary ?
2’s complement 1001
=> 0110 ←- 1’s Complement
=> 0110
+1
—-------
0111 ←— 2’s Complement Store this Binary digit for (-9).

sizeof() :- The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.

int main() int main() int main()


{ { {
double num = 93.3265; int num = 93; bool num = 1;
cout << cout << sizeof(num); cout << sizeof(num);
sizeof(num); return 0; return 0;
return 0; }; };
};

Size 8 Size 4 Size 1

Typecasting: A type cast is basically a conversion from one type to another.

int main() int main()


{ {
int num = 'A'; char num = 65;
cout<<(num); cout<<(num);
return 0; return 0;
}; };

Output : 65 Output : A
Here, At first the compiler converts the code into binary numbers. Then, Binary numbers convert into output
according to ASCII TABLE.

IF-ELSE Condition
The C++ if statement tests the condition. It is executed if the condition is true.
Comparison Operators:

Operator Name Example

== Equal to x==y

!= Not equal x!=y

< Less than x<y

> Greater than x>y


<= Less than or equal to x<=y

>= Greater than or equal to >=

Exp :: 01 Print big number. 20, 30


#include <iostream>
using namespace std;

int main()
{
int first_num = 20;
int second_num = 30;

if(first_num>second_num)
{
cout<<first_num;
}
else
{
cout<<second_num;
}
return 0;
};

Exp :: 02 Check number ODD and EVEN.


#include <iostream>
using namespace std;

int main()
{
int num = 25;

if(num%2==0)
{
cout<<"EVEN";
}
else
{
cout<<"ODD";
}
return 0;
};
Exp :: 03 Check number Positive, Negetive or Zero?
#include <iostream>
using namespace std;

int main()
{
int num = 0;

if(num>0)
{
cout<<"It is Positive Number";
}
else if(num<0)
{
cout<<"It is Negative Number";
}
else
{
cout<<"It is zero Number";
}
return 0;
};

Logical Operator::
&& ⇒ Logical AND return True if both statements are true x<5 && x < 10
|| ⇒ Logical OR return True if one statements are true x<5 || x < 4
! ⇒ Logical NOT Reverse the result return false if the result is true !( x<5 && x < 10)

Exp :: 04 Print big number? 20, 40, 60


#include <iostream>
using namespace std;

int main()
{
int a= 20, b= 40, c= 60;

if(a>=b && a>=c)


cout<<a;

if(b>=a && b>=c)


cout<<b;
else
cout<<c;
return 0;
};

HOMEWORK

Exp :: 04 Find the given year is leap year or not?


#include <iostream>
using namespace std;

int main()
{
int year;
cout<<"Enter Your Year: ";
cin>>year;

if((year%400==0) ||(year%4==0 && year%100!=0))


cout<<"Leap Year";
else
cout<<"Not a Leap Year";
return 0;
};
If-Else Condition, Operators and Basic of Loops

WEEK :: 01 DAY: 03 DATE: - 19-04-2023


Exp :: 01 Print Vowel and Consonant?

#include <iostream>
using namespace std;

int main()
{
char c = 'h';
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
cout<<"Vowel";
else
cout<<"Consonant";
/*if(c=='a')
cout<<"Vowel";
else if(c=='e')
cout<<"Vowel";
else if(c=='i')
cout<<"Vowel";
else if(c=='o')
cout<<"Vowel";
else if(c=='u')
cout<<"Vowel";
else
cout<<"Consonant"; */
return 0;
};

Exp :: 02 Given a no. check if it is divisible by 3 & 5?


#include <iostream>
using namespace std;

int main()
{
int num = 27;
if(num%3==0 && num%5==0)
cout<<"Perfect";
else
cout<<"Not Perfect";
return 0;
};
TAKE INPUT FROM USER cin>>”Your Value”;

Exp :: 03 :: Add two numbers and Take input from the user?

#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout<<"Enter num1: ";
cin>>num1;
cout<<"Enter num2: ";
cin>>num2;

int sum;
sum=num1+num2;

cout<<"Your Sum is :"<<sum;


return 0;
};

LOOP
A loop statement allows us to execute a statement or group of statements multiple times.

For loop::

for(initialize; condition; operation) initialize : This statement gets executed only once, at the
{ beginning of the for loop.
// code Condition: This statement use for break the loop when condition
false
Operation: It is used for updating a number.( Num++ ← Increase)
};

Exp :: 04 :: Print 10 natural numbers?


#include <iostream>
using namespace std;

int main()
{
for(int num=1; num<=10; num++)
cout<<num<<" ";
return 0;
};
num ++ —>> Post Increment
++num —>> Pre increment
Why use :: ++num, num++, --num, num-- ⇒ less memory used.
num++ :- ++num

num = 0 num = 0
sum = num ++ sum = ++num
[store old value then increment] [store new value]
sum=0 sum = 1
(Store old value)

Exp :: 05 :: Print all even numbers from 1 to 20?


#include <iostream>
using namespace std;

int main()
{
for(int num=1; num<=20; num++)
{
if(num%2==0)
cout<<num<<" ";
}
return 0;
};
HOMEWORK

Exp:6 :: Print Prime Number? (10)


#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter Number: ";
cin >> n;

if(n<2)
{
cout<<"Not a Prime";
return 0;
}

for (int num = 2; num <= n - 1; num++)


{
if (n % num == 0)
{
cout << "Not a Prime";
return 0;
}
};
cout << "Prime Number";
return 0;
};
FOR LOOP + WHILE LOOP + PATTERN PRINTING

WEEK :: 01 DAY: 04 DATE: - 20-04-2023


Exp : 01 :: Print Table of 3?
#include <iostream>
using namespace std;

int main()
{
int num=3;

for(int i=1; i<=10; i++)


cout<<num<<" * "<<i<<" ="<<num*i<<endl;

return 0;
};

\n, endl ⇒ use for next line;

Exp : 02 :: Print Fibonacci Series? [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


#Explanation:
First_num = 0, Second_num = 1, Current_num = First_num + Second_num = 0 + 1= 1
Fourth_num = New_First_num + New_Second_num New_First_num = Second_num
= 1 + 1 New_Second_num = Current_num
= 2
#Code::-
#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter Number: ";
cin >> n;

int first_num = 0;
int second_num = 1;
int current_num;

if (n == 1)
{
cout << 0;
return 0;
}
if (n == 2)
{
cout << 1;
return 0;
}

for (int i = 3; i <= n; i++)


{
current_num = first_num + second_num;
first_num = second_num;
second_num = current_num;
}
cout << current_num;

return 0;
};

Exp : 03 :: Print Yes / No 3>2>1


#Explanation:
3>2 = yes= print 1
2>1= yes = print 1
1>1= No = print 0 ← output
#Code::
#include <iostream>
using namespace std;

int main()
{
int n = 3 > 2 > 1;
cout << n;

return 0;
};

C++ Operator Precedence

Precedence Operator Associativity

1 a*b a/b a%b Left-to-right →

2 a+b a-b Left-to-right →

3 << >> ‘’
4 < <= > >= ‘’

5 == != ‘’

6 && ‘’

7 || ‘’

# More Information —> CLICK HERE

Exp : 04 :: Calculete Multiple operations. [ n = 3*2 - 10/5]

#Explanation: 3*2 - 10/5


= 6 - 10/5 = 6 - 2 = 4 (Output)
#Code::
#include <iostream>
using namespace std;

int main()
{
int n = 3*2 - 10/5;
cout << n;
return 0;
};

Nested Loop:: Nested loop means a loop statement inside another loop statement.
include <iostream>

using namespace std;

int main()

int n;

for (int row = 1; row <= 5; row++) // Outer loop

for (int col= 1; col <= 5; col++) // Nested Loop

cout << "1 ";

cout << endl;

return 0;

};
PATTERN PRINTING

Exp : 05 :: Print Pattern 12345


12345
12345
12345
12345
#Explanation:
row =5; col = 5;
Print col;
#Code ::
#include <iostream>
using namespace std;

int main()
{
int n;
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5; col++)
cout << col << " ";
cout << endl;
}
return 0;
};

Exp : 06 :: Print Pattern 54321


54321
54321
54321
54321
#Explanation:
Print col reverse.
#Code
#include <iostream>
using namespace std;

int main()
{
int n;

for (int row = 1; row <= 5; row++)


{
for (int col = 5; col >= 1; col--)
cout << col << " ";
cout << endl;
}
return 0;
};

Exp : 07 :: Print Pattern


12345
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
#Explanation: Take Counter and increment

#Code::
#include <iostream>
using namespace std;

int main()
{
int n;
int counter=1;

for (int row=1; row<=5; row++)


{
for (int col=1; col<=5; col++)
{

cout<<counter<<" ";
counter++;
}
cout<<endl;
}
return 0;
};

Exp : 08 :: Print Pattern

#Code::
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;

for (int row=1; row<=n; row++)


{
for (int col=1; col<=row; col++)
{

cout<<"*"<<" ";
}
cout<<endl;
}
return 0;
};

Exp : 09 :: Print Pattern


1
12
123
1234
12345
#Code::
#include <iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;

for (int row=1; row<=n; row++)


{
for (int col=1; col<=row; col++)
{

cout<<col<<" ";
}
cout<<endl;
}
return 0;
};

Exp : 10 :: Print Pattern


aaaaa
bbbbb
ccccc
ddddd { Watch ASCII TABLE- Better Understand}
eeeee

#Code
#include <iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;

for (int row=1; row<=n; row++)


{
char c ='a'+row-1;
for (int col=1; col<=n; col++)
cout<<c<<" ";

cout<<endl;
}
return 0;
};

Exp : 11 :: Print Pattern


a
bb
ccc
dddd
eeeee
#Code
#include <iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter Star Row: ";
cin>>n;

for (int row=1; row<=n; row++)


{
char c ='a'+row-1;
for (int col=1; col<=row; col++)
cout<<c<<" ";

cout<<endl;
}
return 0;
};
HOMEWORK

Exp : 12 :: Print Pattern 12345


2345
345
45
5
#Code::

#include <iostream>
using namespace std;

int main()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
cout << " ";

for (int j = i; j < 5; j++)


{

cout << j + 1;
}
cout << endl;
}

return 0;
};
PATTERN PRINTING + WHILE LOOP + MATHS

WEEK :: 01 DAY: 05 DATE: - 21-04-2023


Exp: 01 Pattern Print

*
**
***
****
*****

#Explanation:

row 1 print space = Total row -


Star 1 = 2 * row -1 row nu. = 4 - 1

2 3 = 2 * row -1 space = 4 - 2

3 5 = 2 * row -1 space = 4 - 3

4 7 = 2 * row -1 space = 4 - 4

Code:-
#include <iostream>
using namespace std;

int main()
{
int Total_rows;
cout << "Enter star number: ";
cin >> Total_rows;

for (int row = 1; row <= Total_rows; row++)


{

// for space
for (int col = 1; col <= Total_rows - row; col++)
cout << " ";

// For Star
for (int col = 1; col <= 2 * row - 1; col++)
cout << "*";

cout << endl;


}

return 0;
};

Exp: 02 Pattern Print

#Explanation:
row - 1 star- 1 space-4
(Nrow) (Trow-Nrow)

2 2 3

3 3 2

4 4 1

5 5 0

Nrow = row num; Trow = Total row Inner Space = Cout<<”* space”;

Code :-
#include <iostream>
using namespace std;

int main()
{
int Totat_rows;
cout << "Enter Row Number: ";
cin >> Totat_rows;

for (int row = 1; row <= Totat_rows; row++)


{
// for space
for (int col = 1; col <= Totat_rows - row; col++)
cout << " ";

// For Star and Inner Space


for (int col = 1; col <= row; col++)
cout << "* ";

cout << endl;


}

return 0;
};

Exp: 03 Pattern Print

#Explanation:
# Upper Half Star:
Left side star: For space : right side star:
row star 2 * n -- 2*row same with left side
1 1
2 2 (Total col - row star) (depend on row)
3 3 ( star=row)
4 4
5 5
(star=row)
# Lower Half Star:

Left side star: For space : right side star:


row star 2 * n - (n-row) same with left side
1 4 - (n-row) = (n-row)
2 3 (2 row)
3 2
4 1
(n-row)
#Code:
#include<iostream>
using namespace std;

int main()
{
int n;
//Total_row = n
cout<<"Enter The row Num: ";
cin>>n;
// upper side
for(int row =1; row<=n; row++)
{
// star
for(int col=1;col<=row;col++)
cout<<"*";

// space
for(int col=1;col<=2*n-2*row;col++)
cout<<" ";
// star
for(int col=1;col<=row;col++)
cout<<"*";

cout<<endl;

// lower side
for(int row =1; row<=n-1; row++)
{
// star
for(int col=1;col<= n - row; col++)
cout<<"*";

// space
for(int col=1;col<=2*row; col++)
cout<<" ";

// star
for(int col=1;col<= n - row; col++)
cout<<"*";

cout<<endl;

return 0;
};

WHILE LOOP

Exp: 01 :: Find digit any number like( 125, 220056, 65487 );


#Explanation:
If We want to find digit 125. So, We can divide it by 10, 100, 1000, …. Until result 0.
So, 125 is divided by 1000 and the remender 125. Thus We know the digit is 3 according to 1000.
Because 000 is equal to a digit.
Zero Digit

0 1

00 2

00000 5
Code::
#include<iostream>
using namespace std;

int main()
{
int n;
cin>>n;

int num=10;
int digit =1;

for(int i=1; i>0;i++) // infinite condition


{
if(n/num==0)
{
cout<<digit;
return 0;
}
num = num*10;
digit++;
}

return 0;
};

#Explanation: WHILE LOOP

while(Condition) Condition is True code is executed. When the condition is False, the loop is broken.
{
// #code
};
Same Q. Exp: 01 :: Find digit any number like( 125, 220056, 65487 );
#Explanation: Divided by 10 until we get 0 result.
#Code::
#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter Your Digit: ";
cin>>n;
int Digit = 0;

while (n)
{
Digit++;
n =n/10;
}
cout<<Digit;

return 0;
};

Exp: 02:: Reverse Number: 3 2 6 5 Output: 5 6 2 3

#Explanation:
1st part Get digit reverse one by one. 2nd part Multiply by 10 and add digits one by one.
Divide by 10 and get remainder 0 * 10 + 5 = 5
3265/10 %5 5 * 10 + 6 = 56
326/10 % 6 56 * 10 + 2 = 562
32/10 % 2 562 * 10 + 3 = 5623
3/10 % 3
#Code::
#include<iostream>
using namespace std;

int main()
{
int num;
cout<<"Enter Number: ";
cin>>num;
int sum = 0;

while(num)
{
int digit = num%10;
sum = sum*10+digit;
num = num/10;
}
cout<<sum;

return 0;
};
Exp: 03:: Convert Decimal to Binary. 37 Output: (100101)
#Explanation:
1st Part- 2nd Part- 3rd Part-
Divide by 2 and get remainder (%) According to Ex:2 According to Ex:2
One by one bin*10 + bin Reverse
#Code::
#include<iostream>
using namespace std;

int main()
{
int num, sum=0, mul=1;
cout<<"Enter Your Numbers: ";
cin>>num;

while(num>0)
{
int digit = num%2;
sum = sum + digit *mul;
num= num/2;
mul = mul*10;
};
cout<<sum;

return 0;
};

NOTE**
When input long values like (1345664). It is overflow (32bit to 64bit). So, use a long long data type.

You might also like