Sie sind auf Seite 1von 30

C++ Programming Problems (LOOP STRUCTURES)

1. Write a program that will display the even numbers from 1 to 100.

#include<iostream>
#include<conio.h>
using std::cout;
using std::endl;

int main!
"
int counter;
forint counter # 1; counter <# 100; counter$$!"
ifcounter % &! ## 0!
cout<< counter<<endl;
'
getch!;
return 0;
'

&. Write a program that will compute for and display the sum of the numbers
divisible by () ranging from 1 to 100.

#include<iostream>
#include<conio.h>
using std::cout;

int main!
"
int counter)sum#0;
forint counter # 1; counter <# 100; counter$$!"
ifcounter % (! ## 0!"
sum # sum $ counter;
'
'
cout<<*+he sum of the numbers divisible by (: *<<sum;
getch!;
return 0;
'







(. Write a program that will compute and display the sum of the powers of & from
the first power to the nth power) where n is a nonnegative integer given by the user. e,. if
n # -) then compute &
1
$&
&
$&
(
$&
-
# (0; thus display (0 e,. if n # .) then compute
&
1
$&
&
$&
(
$&
-
$&
.
# /&; thus display /& .

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int counter)power)sum#0),)base;
cout<<*0nter 1 power: *;
cin>>power;
forcounter # 1; counter <# power; counter$$!"
base # 1;
for, # 1; , <# counter; ,$$!
base # base 2 &;
sum # sum $ base;
'
cout<<*+he sum of the powers of & from the first power to the nth power: *<<sum;
getch!;
return 0;
'
-. Write a program that will compute for the following given n 3 ,:

,
1
41 $ ,
&
4& $ ,
-
4( $ ... $ ,
n
4n

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int counter)n),)y;
float result#0) sum#0)base;
cout<<*0nter value for 5: *;
cin>>,;
cout<<*0nter value for 1: *;
cin>>n;

forcounter # 1; counter <# n; counter$$!"
base # 1;
fory # 1; y <# counter; y$$!
base # base 2 ,;

result # base 4 counter;
sum # sum $ result;
'
cout<<*+he result is: *<<sum;
getch!;
return 0;
'


.. 6iven an input n assumed one7digit) display a pattern. e,. if n # .) display

1
1_2
1_2_3
1_2_3_4
1_2_3_4_5
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int z,n,x,y;

cout<<"Enter value for N: ";
cin>>n;

for(x = 1; x <= n; x++){
for(z = n; z > x; z--)
cout<<" ";
for(y = 1; y <= x; y++){

if(y == x)
cout<<y;
else
cout<<y<<"_";

}
cout<<endl;
}
getch();
return 0;
}



8. Write a program that will display a pattern depending on the value of n entered by
the user.
e,. if n # () display e,. if n# -) display
2 2 2 2
2 2 2 2
2 2 2
2
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)n),)y;

cout<<*0nter value for 1: *;
cin>>n;

for, # 1; , <# n; ,$$!"
for9 # 0; 9 < ,; 9$$!
cout<<* *;
cout<<*2*;
fory # ,; y < n; y$$!"
cout<<* *;
'
fory # ,$1; y < n; y$$!"
cout<<* *;
'
if, < n!
cout<<*2*;
cout<<endl;
'
getch!;
return 0;
'










/. Write a program to display all combinations of : and ; from 1 to 100! that will
ma<e the e,pression false: . 2 :! = ( 2 ;! > (0

#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int a)b;
cout<<*:>t>t;*<<endl;
fora # 1; a <# 100; a$$!"
forb # 1; b <# 100; b$$!
if. 2 a! 7 ( 2 b!! <# (0 !
cout<<a<<*>t>t*<<b<<endl;

'

getch!;
return 0;
'
?. Write a program that reverses an input number.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int mod#0)num)rev#0;

cout<<*0nter numeric value: *;
cin>>num;

whilenum > 0!"
mod # num % 10;
rev # rev 2 10 ! $ mod;
num#num410;
'
cout<<*@everse of the given number: *<< rev;
getch!;
return 0;
'


A. Write a program that gets as input a binary number and outputs its corresponding
decimal eBuivalent. Ceclare your integer variables as long. : long declaration
accommodates ? digits for a number. 0,ample:

0nter a binary number: 1101
Cecimal eBuivalent: 1(
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int binary) digit) base # 0) decimal # 0;
cout<<*0nter a binary number: *;
cin>>binary;
whilebinary!"
digit # binary % 10;
decimal $# digit << base;
base $# 1;
binary 4# 10;
'
cout<<*Cecimal eBuivalent is: *<<decimal;
getch!;
return 0;
'

10. 6iven the value of n from the user) write a program that will display the first n
even numbers.
0,. if n # .) then display the first . even integers which are &) -) /) ?) 10.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;

int main!
"
int n),)num#&;
cout<<*0nter no. of even integers: *;
cin>>n;
for, # 1; , <# n; ,$$!"
if,##n!
cout<<num<<* *;
else
cout<<num<<* ) *;
num$#&;
'
getch!;
return 0;
'


11. Write a program that accepts a number n and displays the sum of even numbers
and the sum of odd numbers from 1 to n.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int n),)sum0ven # 0)sumDdd#0;
cout<<*0nter value for 1: *;
cin>>n;
for, # 1; , <# n; ,$$!"
if,%& ## 0!
sum0ven # sum0ven $ ,;
else
sumDdd # sumDdd $ ,;
'
cout<<*Eum of 0ven 1umbers: *<<sum0ven<<endl;
cout<<*Eum of Ddd 1umbers : *<<sumDdd;
getch!;
return 0;
'

1&. Write a program that will compute for a
,
given real value a and positive integer ,.
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int n),;
float a # 0)res #1;
cout<<*0nter value for :: *;
cin>>a;
cout<<*0nter value for ,: *;
cin>>,;
forn # 1; n <# ,; n$$!"
res # res 2 a;
'
cout<<*@esult is: *<<res;

getch!;
return 0;
'
1(. Write a program that reads in a number n and outputs the sum of the sBuares of
the numbers from 1 to n.
e,. if n # () then display 1- because 1
&
$ &
&
$ (
&
# 1-.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int n),)sum#0;

cout<<*0nter value for n: *;
cin>>n;
for, # 1; , <# n; ,$$!"
sum # sum $ ,2,!;
'
cout<<*@esult is: *<<sum;

getch!;
return 0;
'
1-. Write a program that as<s for the start month and end month and compute for the
total number of days from the start month to the end month. Co not use if7then7else) use
switch statement.
e,. Ff start month # & and end month # .) then
+otal # &? $ (1 $ (0 $ (1 # 1&0. Cisplay 1&0.
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int ,)totalCays#0)days)startGonth)endGonth;
cout<<*0nter number of start month: *;
cin>>startGonth;
cout<<*0nter number of end month: *;
cin>>endGonth;
for, # startGonth; , <# endGonth; ,$$!"
switch,!"
case 1:
days # (1;
brea<;
case &:
days # &?;
brea<;
case (:
days # (1;
brea<;
case -:
days # (0;
brea<;
case .:
days # (1;
brea<;
case /:
days # (0;
brea<;
case 8:
days # (1;
brea<;
case ?:
days # (1;
brea<;
case A:
days # (0;
brea<;
case 10:
days # (0;
brea<;
case 11:
days # (0;
brea<;
case 1&:
days # (1;
brea<;
'
totalCays # totalCays $ days;
'
cout<<*+otal 1umber of Cays: *<<totalCays;

getch!;
return 0;
'
1.. Write a program that will display the following: -) ?) 1&) 1/) ...) -A/

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int ,;
for, # -; , < .00; ,$#-!"
if, ## .007-!
cout<<,<<**;
else
cout<<,<<*) *;
'
getch!;
return 0;
'

1/. Write a program that reads in a number n) and then reads in n numbers. +he
numbers will be alternately added and subtracted.
e,. Ff n # . and the numbers entered are -) 1-) .) /) 1)
then compute for - $ 1- 7 . $ / 7 1 # 1?. Cisplay 1?.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int n),)result#0)num;
cout<<*0nter value for n: *;
cin>>n;
for, # 1; , <# n; ,$$!"
cin>>num;
if, H#1!"
if,%& ## 0!
result # result $ num;
else
result # result 7 num;
'
else" result # num;'
'
cout<<*@esult is: *<<result;
getch!;
return 0;
'
18. Write a program that will as< the user to enter 10 numbers and display the largest
number entered.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int n),)largest#0;

for, # 1; , <# 10; ,$$!"
cout<<*0nter value *<<,<<*: *;
cin>>n;
iflargest < n!
largest # n;
'
cout<<*Iargest number is: *<<largest;
getch!;
return 0;
'

1?. Write a program that will as< the user to enter 100 numbers and display on the
screen the highest and lowest of these numbers.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int n),)largest#0)smallest;

for, # 1; , <# 10; ,$$!"

cout<<*0nter value *<<,<<*: *;
cin>>n;
iflargest < n!
largest # n;
if, ## 1!"
smallest # n;
'
else"
ifsmallest > n!
smallest # n;
'
'
cout<<*Iargest number is: *<<largest<<endl;
cout<<*Emallest number is: *<<smallest;
getch!;
return 0;
'


1A. Write a program that will as< the user for a number and display all the factors of
the number.
#include<iostream>
#include<conio.h>

using std::cout;
using std::cin;

int main!
"
int n) f) counter # 0;

cout<<*0nter 1umber to be factored: *;
cin>>n;
forf # 1; f <# n; f$$!"
ifn % f ## 0!"
cout <<f<<* *;
counter$$;
ifcounter ## -!"
counter # 0;
'
'
'
getch!;
return 0;
'






&0. @ead any two numbers that represent an amount of money to be deposited and
the annual interest rate. Jompute and print the annual balances for the first 10 years.
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int ,;
float deposit)balance) interest;
cout<<*0nter amount of deposit: *;
cin>>deposit;
cout<<*0nter annual interest rate: *;
cin>>interest;
balance # deposit;
cout<<endl<<*Kears*<<*>t>t*<<*:nnual ;alances*<<endl<<endl;
for, # 1; , <# 10; ,$$!"
balance # balance $ balance 2 interest4100.0!!;
cout<<,<<*>t>t*<<balance<<endl;
'
getch!;
return 0;
'

&1. Write a program that will as< the user for a number n and display the nth
Libonacci number Ln. Ln defined as follows:
L
0
# 0
L
1
# 1
L
&
# 0 $ 1 # 1
L
(
# 1 $ 1 # &
L
-
# 1 $ & # (
L
.
# & $ ( # .
:
L
n
# L
n71
$ L
n7&


#include<iostream>
#include<conio.h>

using std::cout;
using std::cin;
using std::endl;

int main!
"
int n) first # 0) second # 1) ne,t) c#0;

cout<<*0nter a number: *;
cin>>n;
cout<<*L*<<c<<* # *<<first<<endl;
cout<<*L*<<c$1<<* # *<<second<<endl;
for c # 0 ; c <# n7(! ; c$$ !"
ne,t # first $ second;
cout<<*L*<<c$&<<* # *<<first<<* $ *<<second<<* # *<<ne,t<<endl;
first # second;
second # ne,t;
'
getch!;
return 0;
'
&&. Write a program that would print out a part of the multiplication table. 6et as input
the start number and end number.
Example: 0nter start number: (
0nter end number : .

Dutput is
1 & ( - . / 8 ? A 10
( ( / A 1& 1. 1? &1 &- &8 (0
- - ? 1& 1/ &0 &- &? (& (/ -0
. . 10 1. &0 &. (0 (. -0 -. .0
#include <iostream>
#include<conio.h>

using namespace std;
int main!"

int start1um)end1um),;
cout<<*0nter start number: *;
cin>>start1um;
cout<<*0nter end number: *;
cin>>end1um;
cout<<endl<<endl;
cout<<* *;
for, # 1; , <# 10; ,$$!"
cout<<,<<* *;
if,<#AA!cout<<* *;
if,<#A!cout<<* *;
'
cout<<endl<<endl;
forint y # start1um; y <# end1um; y$$!"
cout<<* *;
cout<<y;
cout<<* *;
forint , # 1; , <# 10; ,$$!"
cout<<,2y<<* *;
if,2y<#AA!cout<<* *;
if,2y<#A!cout<<* *;
'
cout<<endl<<endl;
'
getch!;
return 0;
'
&(. Write a program that will compute and display the sum of the factorials of the
numbers from 1 to n) where n is a nonnegative integer given by the user.
e,. if n # () then compute 1H$&H$(H # A; thus display A
e,. if n # -) then compute 1H$&H$(H$-H # ((; thus display ((

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int n) ,) counter) total # 1)sum # 0;
cout << *0nter value for n: *;
cin >> n;
for, # 1; , <# n; ,$$!"
total # 1;
for counter # 1; counter <# ,; counter$$!
total # total 2 counter;
sum # sum $ total;
'
cout <<*+otal factorial is: *<<sum;
getch!;
return 0;
'

&-. Write a program that will compute and display the sum of the powers of , from
the first power to the nth power) where , and n are nonnegative integers given by the
user.
e,. if , # ( and n # -) then compute (
1
$ (& $ (( $ (- # 1&0;
thus display 1&0
e,. if , # & and n # .) then compute &1 $ && $ &( $ &- $ &. # /&;
thus display /&

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int n) ,)y)sum # 0)9 )total#0;
cout << *0nter value for ,: *;
cin >> ,;
cout << *0nter value for n: *;
cin >> n;
fory # 1; y <# n; y$$!"
total # 1;
for9 # 1; 9 <# y; 9$$!
total # total 2 ,;
sum # sum $ total;
'
cout <<*+he sum power is: *<<sum;
getch!;
return 0;
'
&.. Write a program that will display a pattern depending on n.
e,. if n # -) display e,. if n # () display
* *
22 22
222 222
2222
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int ,)y)n;
cout << *0nter value for n: *;
cin >> n;
for, # 1; , <# n; ,$$!"
fory # 1; y <# ,; y$$!
cout<<*2*;
cout<<endl;
'
getch!;
return 0;
'

&/. 6iven an input n assumed one7digit) display a pattern. e,. if n # -) display
1
12
123
1234
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x,y,n;
cout << "Enter value for n: ";
cin >> n;
for(x = 1; x <= n; x++){
for(y = 1; y <= x; y++)
cout<<y;
cout<<endl;
}
getch();
return 0;
}

&8. 6iven an input n assumed one7digit) display a pattern. e,. if n # -) display
1234
123
12
1
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int ,)y)n;
cout << *0nter value for n: *;
cin >> n;
for, # 1; , <# n; ,$$!"
fory # 1; y <# n7,!$1; y$$!
cout<<y;
cout<<endl;
'
getch!;
return 0;
'

&?. Write a program that will display a pattern depending on n. e,. if n # -) display
e,. if n # () display
* *
* * * *
* * * * * *
* * * *

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int z,n,x,y;

cout<<"Enter value for N: ";
cin>>n;

for(x = 1; x <= n; x++){
for(z = n; z > x; z--)
cout<<" ";
for(y = 1; y <= x; y++){

if(y == x)
cout<<"*";
else
cout<<"*"<<" ";

}
cout<<endl;
}
getch();
return 0;
}
&A. 6iven an input n assumed one7digit) display a pattern. e,. if n # -) display
1
21
321
4321
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)n),)y;

cout<<*0nter value for 1: *;
cin>>n;

for, # 1; , <# n; ,$$!"
for9 # n; 9 > ,; 977!
cout<<* *;
fory # ,; y ># 1; y77!"
cout<<y;
'
cout<<endl;
'
getch!;
return 0;
'


(0. 6iven an input n assumed one7digit) display a pattern. e,. if n # -) display
4 3 2 1
4 3 2
4 3
4
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)n),)y;

cout<<*0nter value for 1: *;
cin>>n;

for, # 1; , <# n; ,$$!"
for9 # 1; 9 <# ,; 9$$!
cout<<* *;
fory # n; y ># ,; y77!"
cout<<y<<* *;
'
cout<<endl;
'
getch!;
return 0;
'

(1. Write a program that will create a rectangle based on length and width. Mse &
iterative4loop statements.
e,. if length # . width # 10 display
2222222222
2 2
2 2
2 2
2222222222
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)length)width),)y;

cout<<*0nter value length: *;
cin>>length;
cout<<*0nter value width: *;
cin>>width;
for, # 1; , <# length; ,$$!"
cout<<*2*;
for9 # 1; 9 < width71; 9$$!"
if, ## 1 NN , ## length!
cout<<*2*;
else
cout<<* *;
'
cout<<*2*;
cout<<endl;
'
getch!;
return 0;
'
((. Write a program that will display a pattern depending on the value of n entered by
the user.
e,. if n # () display e,. if n# -) display
2 2
2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2
2 2
2
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)n),)y;

cout<<*0nter value for 1: *;
cin>>n;

for, # 1; , <# n; ,$$!"
for9 # n; 9 > ,; 977!
cout<<* *;
fory # 1; y <# ,; y$$!"

ify ## ,!
cout<<*2*;
else
cout<<*2*<<* *;
'
cout<<endl;
'
for, # 1; , <# n; ,$$!"
for9 # 1; 9 <# ,; 9$$!
cout<<* *;
fory # n 7 1; y ># ,; y77!"
ify ## ,!
cout<<*2*;
else
cout<<*2*<<* *;
'
cout<<endl;
'
getch!;
return 0;
'

(-. Write a program that will display a pattern depending on the value of n entered by
the user.
e,. if n # () display e,. if n# -) display
2 2
2 2 2 2
2 2 2 2
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int 9)n),)y;

cout<<*0nter value for 1: *;
cin>>n;

for, # 1; , <# n; ,$$!"
for9 # n; 9 ># ,; 977!
cout<<* *;
cout<<*2*;
fory # 1; y < , ; y$$!"
cout<<* *;
'
fory # 1; y < ,71; y$$!"
cout<<* *;
'
if , ## 1! cout<<* *;
else if, <# n !
cout<<*2*;
cout<<endl;
'
getch!;
return 0;
'
2 2




(.. Write a program that will display a pattern depending on the value of n entered by
the user.
e,. if n # -) display e,. if n# .) display
2222 22222
2 2
2 2
2222 2
22222
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int ,)num)y)9;
cout<<*0nter value for 1: *;
cin>>num;
for, # 1;, < num; ,$$!"
if, ## 1 NN , ## num71!"
fory # 1; y <# num; y$$!
cout<<*2*;
'
cout<<*>n*;
for9 # ,$1; 9 < num; 9$$!"
cout<<* *;
'
if, ## num71!
cout<<* *;
else
cout<<*2*;
cout<<*>n*;
'
getch!;
return 0;
'

(/. Write a program that will compute for the following given n: 14&1 $ &4&& $ (4&( $
-4&- $ ... $ n4&n

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int counter)n),)y;
float result#0) sum#0)base;
cout<<*0nter value for 1: *;
cin>>n;

forcounter # 1; counter <# n; counter$$!"
base # 1;
fory # 1; y <# counter; y$$!
base # base 2 &;

result # counter 4 base;
sum # sum $ result;
'
cout<<*+he result is: *<<sum;
getch!;
return 0;
'
(8. Write a program that will compute for the following given n 3 ,: ,040H $ ,141H $
,&4&H $ ,(4(H $ ... $ ,n4nH

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int counter)n),)y;
float result#0) sum#0)base;
cout<<*0nter value for 5: *;
cin>>,;
cout<<*0nter value for 1: *;
cin>>n;

forcounter # 1; counter <# n; counter$$!"
base # 1;
total # 1;
fory # 1; y <# counter; y$$!
base # base 2 ,;
for y # 1; y <# counter; y$$!
total # total 2 y;

result # base 4 total;
sum # sum $ result;
'
cout<<*+he result is: *<<sum;
getch!;
return 0;
'

(?. Write a program that will perform prime factori9ation.
e,. 0nter number: &-
Orime factori9ation: &) &) &) (
e,. 0nter number: 8
Orime factori9ation: 8
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int n),;
int d # &;
cout<<*0nter a value: *;
cin>>n;
ifn < &! return 1;
cout<<*Orime factors of * <<n<<* is *;
whiled < n! "
ifn % d ## 0! "
cout<<d<<* ) *;
n 4# d;
'
else "
ifd ## &! d # (;
else d $# &;
'
'
cout<<d;
getch!;
return 0;
'
(A. : perfect number is a positive integer that is eBual to the sum of all those positive
integers e,cluding itself! that divide it evenly. +he first perfect number is / because its
divisors e,cluding itself! are 1) &) and () and because / # 1 $ & $ (. Write a program to
find the first three perfect numbers include / as one of the three!.

#include<iostream>
#include<conio.h>
int main!
"
int i#1) u#1) sum#0;
whilei<#.00!"
whileu<#.00!"
ifu<i!"
ifi%u##0 !
sum#sum$u;
'
u$$;
'
ifsum##i!"
cout<<i<<* is a perfect number.*<<*>n*;
'
i$$;
u#1; sum#0;
'
getch!;
'

-0. Write a program that ta<es one real value as input and computes the first integer n
such that &n is greater than or eBual to the input value. +he program should output both n
and &n.

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int ,)count # 0) power # 1;
float num # 0;

cout<<*0nter a value: *;
cin>>num;
whilepower < num!"
power # power 2 &;
count$$;
'
cout<<*1 value: *<<count<<endl;
cout<<*& raise to 1: *<<power;
getch!;
return 0;
'

-1. Write a program to read in a real value , and output the integer n closest to the
cube root of ,. +he value of , is assumed positive.
#include <iostream.h>
#include<conio.h>
#include<math.h>
int main!
"
int count # 0;
float cube@oot # 0) num;
clrscr!;
cout<<*0nter a value: *;
cin>>num;
cube@oot # pownum)14(.0!!;
whilecount < cube@oot!"
count$$;
'
cout<<*Jlosest 1th value to the cube root of *<<num<<* is: *<<count;
getch!;
return 0;
'

-&. Write a program that finds the lowest odd integer among the values entered by the
user. Etop as<ing values when a value less than 1 has been entered.
e,. if the values entered by the user are () ?) 1) /) () -) 7.
then display 1
e,. if the values entered by the user are /) -) ?) 0
then display P1o odd integerP

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;

int main!
"
int num # 1),#0)smallest#1;
cout<<*0nter a value: *;
cin>>num;
smallest # num;
whilenum ># 1!"

ifnum%& H# 0!"
, # 1;
ifsmallest > num!
smallest # num;
'
cout<<*0nter a value: *;
cin>>num;
'
if, ## 0!
cout<<*1o odd integerH*;
else
cout<<*Emallest odd value is: *<<smallest;
getch!;
return 0;
'

-(. Write a program that will as< for a long number and count how many digits are
there in the number.
e,. 0nter num: 10?.-
Dutput: .
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!"
int num)count#0;
cout<<*0nter a number: *;
cin>>num;

for;numH#0;num#num410!
count$$;

cout<<*+otal digits is: *<<count;
getch!;
return 0;
'
--. Write a program that will as< for a long number and count how many digits in the
number are even and how many are odd.
e,. 0nter num: ?0.8&
Dutput: ( digits are even
& digits are odd
-.. Write a program that will as< for a value for num and display the product of its
even positioned digits i.e.) digits at the tens unit) thousands unit)...!. Mse long for num.
e,. if num # -1&-8() then compute for 82&2-#./; thus display ./
e,. if num # 1./8?) then compute for 82.#(.; thus display (.
e,. if num # () then display 0

-/. Write a program that will as< for a value for num and display the product of its
odd positioned digits i.e.) digits at the ones unit) hundreds unit)...!. Mse long for num.
e,. if num # 1&-8() then compute for (2-21 # 1&; thus display 1&
e,. if num # ./8?) then compute for ? 2 / # -?; thus display -?
e,. if num # () then display (

-8. Write a program that as<s for an input integer and displays the digits in reverse.
e,. 0nter number: (./& e,. 0nter number: 10&-0
Dutput: &/.( Dutput: 0-&01

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
int main!
"
int mod#0)num)rev#0;

cout<<*0nter numeric value: *;
cin>>num;

whilenum > 0!"
mod # num % 10;
rev # rev 2 10 ! $ mod;
num#num410;
'
cout<<*@everse of the given number: *<< rev;
getch!;
return 0;
'

-?. Write a program that as<s for a long value and swap every two digits in this value.
e,. Ff n # 1&(-.) display &1-(.
Ff n # ?11&(0) display 1?&10(
Ff n # 10101) display 01011


-A. Write a program that as<s for values from the user and find out which number
was entered the most number of times mode! and how many times this number was
entered freBuency!. Etop as<ing for values once a 71 has been entered. :ssume that the
numbers entered are positive and in increasing order and there is only one mode in the
given inputs.
e,. Ff the following numbers were entered:
1) &) &) &) () () /) /) 8) 8) 8) 8) 71
+he mode is 8 and its freBuency is -
e,. Ff the following numbers were entered:
&) () () () -) .) .) 8) ?) ?) ?) ?) ?) 71
+he mode is ? and its freBuency is .

#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
int main!
"
int num # 0),) count#1) largest # 0)prev1um#0)mode#0;
whilenum ># 0!"
cout<<*0nter a value: *;
cin>>num;
ifnum ## prev1um!"
count$$;
'
else"
ifcount > largest!"
largest # count;
mode # prev1um;
'
count # 1;
'
prev1um # num;
'
cout<<*+he mode is *<<mode<<* and its freBuency is *<<largest;
getch!;
return 0;
'

.0. Write a program that will as< for a positive integer number and will display the octal
eBuivalent of the given number.
#include<iostream.h>
#include<conio.h>

int main!
"
int num;
int totalQ10R)i;
clrscr!;
cout << *Olease enter a decimal: *;
cin >> num;
fori # 0; numH#0;i$$!"
totalQiR # num % ?;
num 4# ?;
'
i77;
cout<<*Dctal 0Buivalent: *;
for;i>#0;i77!
cout<<totalQiR;

getch!;
return 0;
'

Das könnte Ihnen auch gefallen