FOR
# include < iostream.h >
main ( )
// declare variable
int x , y;
1) //get input
C out << " inter on integer = " ;
C in >> x;4
2) assign input
x = 5j
// loop for...
for ( y = 1; y < x; y++ )
{
C out << " " << x;
}
WHILE
# include < iostream.h >
main ( )
{
int x , y;
C out << Enter an integer = "
C in >> x ;
// while
y = 1;
while ( y<x )
{
C out << ' enter an integer =' ;
y++;
}
}
SOALAN
1) Change the if...else statement below to switch case statement
# include < iostream.h >
main ( )
{
int nom A, int no B, selection, value ;
C out << 2 number: " ;
Cout << "1. addition ";
Cout << "2.subtraction";
Cout << "3.multiply ";
Cout << "4.division ";
Cout << " enter selection = ";
Cin >> selection ;
{
if ( selection = = 1 )
{ value = nom A + nom B ; }
else if ( selection = = 2 )
{ value = nom A - nom B ; }
else if ( selection = = 3 )
{ value = nom A * nom B; }
else if ( selection = = 4 )
{ value = nom A / nom B; }
else {
Cout << " Invalid selection "; }
Cout << " the value is : ". << value ;
}
2) Change for loop statement below to while loop
# include < iostream.h >
main ( )
{
int i, power 2;
for ( i = 1; i < = 9; i ++ )
{
power 2 = i*i ;
Cout << " " << i << " power 2 " << power2;
}
}
Jawapan
# include ( iostream.h >
main ( )
int i, power 2 ;
// while
1 = 1;
while ( i < = 9 )
{
power 2 = i * i;
Cout << " " << " power2 " << power 2;
i ++ ;
}
}
3) write a program that input 2 number compare the numbers and display only the biggest number if the number are same, display " the numbers are same.
Jawapan
# include ( iostream.h >
main ( )
{
int num1, num2; numbers ;
Cout << " enter 2 numbers:;
Cin >> nom A >> nom B;
// if..else statement
if ( num2 > num 1 )
{ Cout << " num2 is bigger than num1 "; }
else if ( num2 = = num1 )
{ Cout << num2 is same as num1 "; }
else
{ ( out << " invalid " ; }
return O
}
4) write a nprogram using for loop that receive price of 3 products. Calculate the price that must be paid after 10% discount is given to every customer.
Jawapan
For Loop
# include < iostream.h )
main ( )
{
int x price , total discount;
for ( m = 1, x < 4; x++ )
Cout << " total " = << total ;
discount = total * 0.01;
Cout << discount = " << discount ;
return O ;
{
5) what is the input for the following program segment :
i) int x = 20;
for ( int i =5; i < = ; i ++ )
{
Cout << i ;
i = i + 4;
}
ii) int mul = ;
do
{
mul * 2;
Cout << " the mul is " << mul ;
mul ++ ;
}
while ( mul < 10 )
Jawapan
i) 5
10
15
20
ii) 1
3
7
#
No comments:
Post a Comment