Beruflich Dokumente
Kultur Dokumente
Programming
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
Chapter 03: Loops and Decisions
Relational Operators
//demonstrates relational operators
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int n;
cout << "Enter a number: ";
cin >> n;
cout<<n<<"< 10 is "<<(n < 10)<<endl;
cout<<n<<"<=10 is "<<(n <= 10)<<endl;
cout<<n<<"> 10 is "<<(n > 10)<<endl;
cout<<n<<">=10 is "<<(n >= 10)<<endl;
cout<<n<<"==10 is "<<(n == 10)<<endl;
cout<<n<<"!=10 is "<<(n != 10)<<endl;
system("PAUSE"); return 0;
}
Number Systems
Base 16
Base 10
Base 8
Base 2
0000
0001
0010
0011
0100
0101
0110
0111
10
1000
11
1001
10
12
1010
11
13
1011
12
14
1100
13
15
1101
14
16
1110
15
17
1111
bits
bits
bytes
bytes
bytes
bytes
bytes
bytes
bytes
bytes
bytes
bytes
=
=
=
=
=
=
=
=
=
=
=
=
1
2
1
1
1
1
1
1
1
1
1
1
nibble
nibble
Kilo
Mega
Giga
Tera
Peta
Exa
Zetta
Yotta
Bronto
Geo
or 1 Byte
Byte
or
Byte
or
Byte
or
Byte
or
Byte
or
Byte
or
Byte
or
Byte
or
Byte
Byte
1
1
1
1
1
1
1
1
KB
MB
GB
TB
PB
EB
ZB
YB
A|B
A&B
A^B
~A
~B
0 0
0 1
1 0
1 1
Bitwise AND
Bitwise OR
Bitwise Exclusive OR
Bitwise NOT
Left Shift
Right Shift
(
(
(
(
(
(
&
|
^
~
<<
>>
)
)
)
)
)
)
cin >> n;
1)<<endl;
2)<<endl;
3)<<endl;
4)<<endl<<endl;
1)<<endl;
2)<<endl;
3)<<endl;
4)<<endl;
Bitwise Operators
#include <iostream>
#include <iomanip>
// for setw()
#include <stdlib.h>
using namespace std;
int main(void) {
short var1 = 0x0035 & 0x000F; //
short var2 = 0x0004 | 0x0068; //
short var3 = 0x0054 ^ 0x00F0; //
short var4 = ~ 0x0055;
//
cout << setw(4) <<hex<<
<< setw(4) <<hex<<
<< setw(4) <<hex<<
<< setw(4) <<hex<<
system("PAUSE");
return 0;
}
var1
var2
var3
var4
bitwise
bitwise
bitwise
bitwise
<<endl
<<endl
<<endl
<<endl;
AND
OR
XOR
NOT
Bitwise Operators
//swap two numbers without using extra variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int A,B;
cout << "Enter two Numbers: ";
cin >> A >> B;
cout<<"A="<< A <<", B="<< B <<endl;
A = A ^ B; // A XOR B
B = A ^ B;
A = A ^ B;
cout<<"A="<< A <<", B="<< B <<endl;
system("PAUSE");
return 0;
}
<< (
3 ||
0)<<endl; // logical
<< ( -3 ||
5)<<endl; // logical
<< ('0' ||
0)<<endl; // logical
<<(
3 &&
0)<<endl; //logical
<<( -3 &&
5)<<endl; //logical
execution.
if(condition)
statement;
where condition is an integral expression and
statement is any executable statement.
The statement will be executed only if the value
of the integral expression is nonzero (TRUE).
When we want to run more than one statements
based on some condition, the syntax looks like
this:
if(condition){
many_statement_terminated_with ;
}
upon
system("PAUSE");
return 0;
Assignment #3
1. White a program to calculate the GPA in
First semester.
The program asks the marks out of 100 in
each subject and calculates GPA.
It also calculates the percentage marks
in the first semester.
Output should be like this
Loops
while loop
// Using Compound Statements
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int x,loop_count=0;
cout << "How much counting you want? ";
cin >> x ;
while(loop_count <= x)
{
cout << loop_count <<" ";
loop_count++;
}
system("PAUSE");
return 0;
}
while loop
// computes the sum 1 + 2 + 3 + + n,
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int last_no, sum = 0, current_no = 1;
cout << "Enter a Positive Number: ";
cin >> last_no;
cout << "0";
while (current_no <= last_no){
sum = sum + current_no;
cout << "+" << current_no;
current_no++;
}
cout << " = " << sum <<endl;
system("PAUSE");
return 0;
}
"
Assignment #4
1. Write a program that prints the minimum
of four input integers.
2. Write a program that finds the median of
three input integers.
3. Write a program to find the min of 3
numbers using ? : operator
4. Using a while loop write a program to
Compute a Sum of Reciprocals of first n
numbers This program computes the sum of
reciprocals s = 1 + 1/2 + 1/3 + 1/3 + +
1/n,
5. Using nested for loops write a program to
show all prime numbers in range given by
user.
6. Write a program using do..while loop to
compute the sum of the first n squares,
where n is input.
Assignment #4
7. Write a program to print out all
Armstrong numbers between 1 and 500. If
sum of cubes of each digit of the number
is equal to the number itself, then the
number is called an Armstrong number.
For example:
153 = (1*1*1) + (5*5*5) + (3*3*3)
8. Write a program to produce the following
output using loops:
1
1
1
1
1
1
1
2
2
2
2
2
2
3
3
3
3
3
4
4
4
4
5
5
5
6
6
6
6
5
5
5
4
4
4
4
3
3
3
3
3
2
2
2
2
2
2
1
1
1
1
1
1
1
Assignment #4
9. Write a program to produce
the following output
using loop:*
*
* *
* *
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* *
* *
*
Assignment #4
10.Write a program to produce the following
output using for loop:A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F
A B C D E
A B C D
A B C
A B
A
Assignment #4
11. Write a program to produce the following output using
loop:A
A B
A
A
A
F
F
A
E
A
D
A
C
A B
A