0% found this document useful (0 votes)
1K views26 pages

Fundamentals of Programming - 2 01 - Class Notes - (DECODE DSA With C++ 2.0)

Here are the steps to find the sum of digits of a given number: 1. Take input number from user 2. Initialize a variable say sum to store the sum 3. Loop until number becomes 0 - Find the last digit of number by taking modulo 10 - Add last digit to sum - Divide number by 10 to remove last digit 4. Print the sum int main() { int n, sum = 0; cout << "Enter a number: "; cin >> n; while(n != 0) { sum += n%10; n /= 10; } cout << "Sum of digits is: " << sum; return 0; }

Uploaded by

Vishal Kumar
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)
1K views26 pages

Fundamentals of Programming - 2 01 - Class Notes - (DECODE DSA With C++ 2.0)

Here are the steps to find the sum of digits of a given number: 1. Take input number from user 2. Initialize a variable say sum to store the sum 3. Loop until number becomes 0 - Find the last digit of number by taking modulo 10 - Add last digit to sum - Divide number by 10 to remove last digit 4. Print the sum int main() { int n, sum = 0; cout << "Enter a number: "; cin >> n; while(n != 0) { sum += n%10; n /= 10; } cout << "Sum of digits is: " << sum; return 0; }

Uploaded by

Vishal Kumar
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/ 26

C++

Problems on Loops
Lecture- 6

Raghav Garg
Break;
/
break;

0; i 10; i +)d
(inti
+

for =
<

3
Ques : WAP to check if a number is prime or
not. ↓

-> P it has no factors


P other than 1 & the
->
I
number itself

hai nahi. Break;


2) Composite ya
-

3, 4, 6
1, 12, 2,
12 ->
Composite -

ne
loop i2 to n-1
=

if (n%i = 0)
=
- i is a
factor
composite
Ques : WAP to check if a number is prime or
-
-

not.
w
" Output (input
Enter a number:12
~
~
·
12 is a
composite no.

-
12 is
·
a
compo...
·12 is a
composite co

12 - 1, e,
6,12
Ques : WAP to check if a number is prime or - -
-
-
-

not. composite

for lint i 2;
=
i c n+;i
=
+
+

)2

I
if (n%i
=

=
0)[

I cont<<n" composite no";


is a

break;

3
Ques : WAP to check if a number is prime or
not.
boolean, stove the state
Using
Continue;
break;
continue;
Ques : WAP to print odd numbers from 1 to
100. Output

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

continue;
I
if (1%2 =
0)
=

3
cont< i<<endl;
L
skip this sound/
itematic
Predict the
Output
Problems
Predict the output
int main( ) {
9798
while ( 'a' < 'b' )
- -

cout << “malayalam is a palindrome" << endl ;


}

Output
-

malayalam is . . .

segmentation fault, infinite loop


Predict the outputNO
11
Output

N
·
10
int main( ) {
-int
·
10
i; -condition

while ( i = 10 ) {
cout << i << endl;
Infinite loop
i=i+1;
}
a s b
=
asb
} b a<b
a
=

a!=b

ac b =
-
I S

Predict the output ④ Y


B Output
int main( ) { I 2
· 3 I


~ int x = 4, y = 0, z ;
o l 3
while ( x >= 0 ) {
x-- ; ·O Y

y++ ;
S
if ( x == y ) 8-1

continue ;

else
cout << x << “ “ << y << endl ;
}
}
Predict the output Output
x YB2
=

·Y ⑧
int main( ) {
y 0x
=

2
int x = 4, y = 0, z ; · 3I
while ( x >= 0 ) {
if ( x == y )
break ;
else
cout << x << “ “ << y << endl ;
x-- ;
y++ ;
}
}
Predict the output

I
Output
t 105R 1
=

Hello
int main( ) {
·

Hello
int t = 10;
·

· Hello
while(t/=2){
cout << “Hello” << endl;
}
}
Questions using
Operators
+-*/ %
Ques : WAP to count digits of a given number.
intn e
= I
Arithm
i
5
45312 +
count 0
=

n =10 + n 197
+

9999881247 +10
count++
- count-1

n) 10
=
n
-
19
=

1) int' boat rahihai


#Hint ki no
count-2

k0 10 se divide bar door 1


2)
=

I n

count=3

n 0 =

count=y
O

I
719X
11+
Output
-

n
=

Enter a number:1971
count=0A&BY
a 1971
=
Y
Ques : WAP to print sum of digits of a given
number. 9874 n
=

Sym
=

9 8
+ 7
+
y
+
4
=

7
+
8
+ +
9

9874%10 Y
last Digit
=
=

condition eno
steps -

Algorithm
9749579/840 1) Last Digit
n =

last Digit 4* 89
= 2) Sum+=last digit

Sum 0
=
Y I 1928 3) n = 10

1%10 1=
Ques : WAP to print reverse of a given
number. 987 n 4r
=
4789 =

Algorithm (n
--
-eps >
0)

10;
*

1) v 2
8741879/890
=

n
=

0Y A & 9 2) last Digiten%10


last Digit
=

r 0 0
=
x 40 YA 4A0 48 3) v += last Digit

77804789 n=10
4)
n 1231 0
=

8 0B30323285213210
=

last Digit 8 BR 1
[
=

n 312X0
=

r 00 B303,
=

350321

last Digit IBR 1


[
=
Ques : Print the sum of this series :
1 - 2 + 3 - 4 + 5 - 6… upto ‘n’.
sum 0
=

for (inti=1; i <


n;i +)
= +

I if (1%2! 0)
=

Sum+= i;
else sum
-=
i;

If
1) i is even e
(1-2) (3 y)
+ -

(5 6)
+ -

(7-8).... (in-11-
+

m)

+-

interines*.
sum:
-;
Ques : Print the sum of this series :
1 - 2 + 3 - 4 + 5 - 6… upto ‘n’.
odd 7
If for ex n
=

is +
2) i

21 -

2) (3
+ -

y) (5
+
-

6) 7
+

ines"
sum
+
-

=
Ques : Print the factorial of a given number ‘n’.
A

51 5x4x3x2x1=
= 120 1ton ka
product

31 3x2 1 x
nx(n 1)!
n!
-
=
=

2x 1
2! =

1 xn 2 .
x 3x2x1
n!
...
nxn
-
-

I! I
=

0! 1
=
Ques : Print the nth fibonacci number.
1 2 3 5 8 1321 34 55 89...

9
n = 12345678 10 11

fibo(n) fibo(n-1)
= +
fibo(n-2)
#
Attm:
1) Sum a
= +
b
a xXR
=

B S

2)a b
=

b y&
=
B $8

b
0&$$8 3) sum
=

sum
=
Ques : Two numbers are entered through the
keyboard. Write a program to find the value of one
number raised to the power of another.
int a, b; ab ? int 1;
=

power:
↳in ab;

ab =

aa...
b times

b
a
in
=

2
2
2 t 0.25
=
= =

You might also like