0% found this document useful (0 votes)
5 views16 pages

Assignment1BDue November 8

The document provides a comprehensive overview of various C++ programming concepts, including operators (remainder, arithmetic assignment, increment, decrement, relational, logical), control statements (if, if-else, nested if, else-if), and the use of conditional operators. It includes multiple code examples demonstrating these concepts and concludes with a series of exercises for practice. The exercises cover a range of topics such as arithmetic operations, conditional checks, and basic input/output operations.

Uploaded by

Zaeem Muzammil
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)
5 views16 pages

Assignment1BDue November 8

The document provides a comprehensive overview of various C++ programming concepts, including operators (remainder, arithmetic assignment, increment, decrement, relational, logical), control statements (if, if-else, nested if, else-if), and the use of conditional operators. It includes multiple code examples demonstrating these concepts and concludes with a series of exercises for practice. The exercises cover a range of topics such as arithmetic operations, conditional checks, and basic input/output operations.

Uploaded by

Zaeem Muzammil
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/ 16

Lab:

The Remainder Operator


// remaind.cpp
// demonstrates remainder operator
#include <iostream>
using namespace std;
int main()
{
cout << 6 % 8 << endl // 6
<< 7 % 8 << endl // 7
<< 8 % 8 << endl // 0
<< 9 % 8 << endl // 1
<< 10 % 8 << endl; // 2
return 0;
}

Arithmetic Assignment Operators


// assign.cpp
// demonstrates arithmetic assignment operators
#include <iostream>
using namespace std;
int main()
{
int ans = 27;
ans + = 10; //same as: ans = ans + 10;
cout << ans << “, “;
ans - = 7; //same as: ans = ans - 7;
cout << ans << “, “;
ans *= 2; //same as: ans = ans * 2;
cout << ans << “, “;
ans /= 3; //same as: ans = ans / 3;
cout << ans << “, “;
ans %= 3; //same as: ans = ans % 3;
cout << ans << endl;
return 0;
}
Increment Operators (Prefix and Postfix)

// increm.cpp
// demonstrates the increment operator
#include <iostream>
using namespace std;
int main()
{
int count = 10;
cout << “count=” << count << endl; //displays 10
cout << “count=” << ++count << endl; //displays 11 (prefix)
cout << “count=” << count << endl; //displays 11
cout << “count=” << count++ << endl; //displays 11 (postfix)
cout << “count=” << count << endl; //displays 12
return 0;
}

Decrement Operators (Prefix and Postfix)

// increm.cpp
// demonstrates the increment operator
#include <iostream>
using namespace std;
int main()
{
int count = 10;
cout << “count=” << count << endl; //displays 10
cout << “count=” << --count << endl; //displays 9 (prefix)
cout << “count=” << count << endl; //displays 9
cout << “count=” << count-- << endl; //displays 9 (postfix)
cout << “count=” << count << endl; //displays 8
return 0;
}
Relational Operators
// relat.cpp
// demonstrates relational operators
#include <iostream>
using namespace std;
int main()
{
int numb;
cout << “Enter a number: “;
cin >> numb;
cout << “numb<10 is “ << (numb < 10) << endl;
cout << “numb>10 is “ << (numb > 10) << endl;
cout << “numb==10 is “ << (numb == 10) << endl;
return 0;
}
Some expressions that use relational operators, and also look at the value
of each expression

The if Statement (Decision)


// ifdemo.cpp
// demonstrates IF statement
#include <iostream>
using namespace std;
int main()
{
int x;
cout << “Enter a number: “;
cin >> x;
if( x > 100 )
cout << “That number is greater than 100\n”;
return 0;
}

Multiple Statements in the if Body


// if2.cpp
// demonstrates IF with multiline body
#include <iostream>
using namespace std;
int main()
{
int x;
cout << “Enter a number: “;
cin >> x;
if( x > 100 )
{
cout << “The number “ << x;
cout << “ is greater than 100\n”;
}
return 0;
}

The if...else Statement


// ifelse.cpp
// demonstrates IF...ELSE statememt
#include <iostream>
using namespace std;
int main( )
{
int x;
cout << “\nEnter a number: “;
cin >> x;
if( x > 100 )
cout << “That number is greater than 100\n”;
else
cout << “That number is not greater than 100\n”;
return 0;}
// prime.cpp
// demonstrates IF statement with prime numbers
int main()
{
unsigned long n, j=2;
cout << “Enter a number: “;
cin >> n; //get number to test
if(n%j == 0) //2 on up; if remainder is 0,
cout << “It’s not prime; divisible by “ << j << endl;
else
cout << “It’s prime\n”;
return 0;
}

Nested if...else Statements


// adifelse.cpp
// demonstrates IF...ELSE with adventure program
#include <iostream>
using namespace std;
#include <conio.h> //for getche()
int main()
{
char dir=’a’;
int x=10, y=10;
cout << “Type Enter to quit\n”;
cout << “\nYour location is “ << x << “, “ << y;
cout << “\nPress direction key (n, s, e, w): “;
dir = getche(); //get character
if( dir==’n’) //go north
y--;
else
if( dir==’s’ ) //go south
y++;
else
if( dir==’e’ ) //go east
x++;
else
if( dir==’w’ ) //go west
x--;
cout << “\nYour new location is “ << x << “, “ << y;
return 0;
} //end main

Matching the else


There’s a potential problem in nested if...else statements: You
can inadvertently match an else with the wrong if. BADELSE
provides an example:
// badelse.cpp
// demonstrates ELSE matched with wrong IF
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << “Enter three numbers, a, b, and c:\n”;
cin >> a >> b >> c; // What happens if you enter 2, then 3, and then 3?
if( a==b )
if( b==c )
cout << “a, b, and c are the same\n”;
else
cout << “a and b are different\n”;
return 0;
}
Here’s a corrected version:
if(a==b)
if(b==c)
cout << “a, b, and c are the same\n”;
else
cout << “b and c are different\n”;
If you really want to pair an else with an earlier if, you can use
braces around the inner if:
if(a==b)
{
if(b==c)
cout << “a, b, and c are the same”;
}
else
cout << “a and b are different”;

The else...if Construction


// adelseif.cpp
// demonstrates ELSE...IF with adventure program
#include <iostream>
using namespace std;
#include <conio.h> //for getche()
int main()
{
char dir=’a’;
int x=10, y=10;
cout << “\nYour location is “ << x << “, “ << y;
cout << “\nPress direction key (n, s, e, w): “;
dir = getche(); //get character
if( dir==’n’) //go north
y--;
else if( dir==’s’ ) //go south
y++;
else if( dir==’e’ ) //go east
x++;
else if( dir==’w’ ) //go west
x--;
cout << “\nYour new location is “ << x << “, “ << y;
return 0;
} //end main

Learn conditional operator and some other control statements


Here’s a strange sort of decision operator. It exists because of a common
programming situation:
A variable is given one value if something is true and another value if
it’s false. For example, here’s an if...else statement that gives the
variable min the value of alpha or the value of beta, depending on which
is smaller:
if( alpha < beta )
min = alpha;
else
min = beta;
This sort of construction is so common that the designers of
C++ (actually the designers of C, long ago) invented a
compressed way to express it: the conditional operator.
This operator consists of two symbols, which operate on
three operands. It’s the only such operator in C++; other
operators operate on one or two operands. Here’s the
equivalent of the same program fragment, using a conditional
operator:
min = (alpha<beta) ? alpha : beta;
The part of this statement to the right of the equal sign is
called the conditional expression:
(alpha<beta) ? alpha : beta // conditional expression
The question mark and the colon make up the conditional
operator. The expression before the question mark
(alpha<beta) is the test expression. It and alpha and beta
are the three operands.
Write the source code describing the use of conditional operator.

Logical Operators
Logical AND Operator
// advenand.cpp
// demonstrates AND logical operator
#include <iostream>
using namespace std;
#include <process.h> //for exit()
#include <conio.h> //for getche()
int main()
{
int x,y;
cout<<”enter the value of x either 0 or 1”;
cin>>x;
cout<<”enter the value of y either 0 or 1”;
cin>>y;
if( x==0 && y==0 ) //if x is 0 and y is 0
cout << “\nboth vales are 0 !\n”;
if( x==0 && y==1 ) //if x is 0 and y is 1
cout << “\nx is 0 and y is 1!\n”;
if( x==1 && y==0 ) //if x is 1 and y is 0
cout << “\n x is 1 and y is 0!\n”;
if( x==1 && y==1 ) //if x is 0 and y is 0
cout << “\nboth values are1 !\n”;
return 0;
} //end main

Logical OR Operator
// advenor.cpp
// demonstrates OR logical operator
#include <iostream>
using namespace std;
#include <process.h> //for exit()
#include <conio.h> //for getche()
int main()
{ int acode;
char ch;
cout<<”enter a number between 0 and 6”;
cin>>acode;
cout<<”enter a letter either A or B”;
cin>>ch;
if( acode>4 || ch==`A` )

cout << “\nRoses are red. ”;


if( acode<4 || ch==`B` )
cout << “\nViolets are Blue. ”;

return 0;
} //end main()

Logical NOT Operator


cin>>x;
cin>>y;
if( !(x%7) && !(y%7) ) // if not x%7 and not y%7
cout << “There’s a mushroom here.\n”;

Precedence Summary

We should note that if there is any possibility of confusion in a relational


expression that involves multiple operators, you should use parentheses
whether they are needed or not.
Exercises

1. Write a C program to return the quotient and remainder of a division.


2. Write a C program that takes hours and minutes as input, and calculates the
total number of minutes.
3. Write a program in C that takes minutes as input, and display the total number
of hours and minutes.
Expected Output :
Input minutes: 546
9 Hours, 6 Minutes
4. Write a program in C to calculate the sum of three numbers with getting input
in one line separated by a comma.
Expected Output :
Input three numbers separated
by comma : 5,10,15
The sum of three numbers : 30
5. Write a C program to accept two integers and check whether they are equal or
not.
6. Write a C program to check whether a given number is even or odd.
7. Write a C program to read the value of an integer m and display the value of
n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.
8. Write a C program to find the largest of three numbers.
9. Write a C program to accept a coordinate point in a XY coordinate system and
determine in which quadrant the coordinate point lies.
10.Write a temperature-conversion program that gives the user the option of
converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the
conversion. Use floating-point numbers. Interaction with the program might
look like this:.

11.Create the equivalent of a four-function calculator. The program should ask


the user to enter a number, an operator, and another number. (Use floating
point.) It should then carry out the specified arithmetical operation: adding,
subtracting, multiplying, or dividing the two numbers. Use if statement to
select the operation. Finally, display the result. Some
12.Create a four-function calculator for fractions. Here are the formulas for the
four arithmetic operations applied to fractions:

The user should type the first fraction, an operator, and a second fraction.
The program should then display the result.

13.Write a C++ program to compute the perimeter and area of a rectangle with a
height of 7 inches and width of 5 inches.
14.Write a C++ program to convert specified days into years, weeks and days.
Note: Ignore leap year. For example:
Number of days: 1329
Expected Output:
Years: 3
Weeks: 33
Days: 3

15.Write a C++ program that accepts 4 integers p, q, r, s from the user where r
and s are positive and p is even. If q is greater than r and s is greater than p
and if the sum of r and s is greater than the sum of p and q print "Correct
values", otherwise print "Wrong values". For example:
Input the second integer: 35
Input the third integer: 15
Input the fourth integer: 46
Expected Output:
Wrong values
16.Write a C++ program that reads three floating values and check if it is possible
to make a triangle with them. Also calculate the perimeter of the triangle if
the said values are valid.
17.Write a C++ program that reads an integer between 1 and 12 and print the
month of the year in English.
18.Write a C program to check whether a given number is positive or negative.
19.Write a C program to find whether a given year is a leap year or not.
20.Write a C program to find the eligibility of admission for a professional course
based on the following criteria:
Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=180
or
Total in Math and Subjects >=140
Test Data :
Input the marks obtained in Physics :65
Input the marks obtained in Chemistry :51
Input the marks obtained in Mathematics :72
Expected Output :
The candidate is eligible for admission.

21.Write a C program to read roll no, name and marks of three subjects and
calculate the total, percentage and division/grade.
22.Write a C program to read temperature in centigrade and display a suitable
message according to temperature state below :
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
Test Data :
42
Expected Output :
Its very hot.

23.Write a C program to check whether a triangle is Equilateral, Isosceles or


Scalene.
24.Write a C program to check whether a triangle can be formed by the given
value for the angles.
25.Write a C program to check whether an alphabet is a vowel or consonant.
26.Simple Calculator using switch statement.
27.Program to Calculate Sum of Natural Numbers.
28.Program to Calculate Grades according to your grading system. Total
marks are supposed to be 100.
29.See your home Electricity bill and write a program to calculate
electricity bill up to 500 units.
30.Check if a year is leap year or not using if else.
31.Program to Display Fibonacci Series up to certain numbers.
32.Write following program.
a. Find Largest Number Using if...else statement
b. Find Largest Number Using Nested if...else statement

Algorithm
In programming, algorithm is the set of well-defined instruction in a sequence to
solve a program. An algorithm should always have a clear stopping point.
OR,
An algorithm specifies a series of steps that perform a particular computation or task.
Examples
 Write an algorithm to add two numbers entered by the user.

Step-1: Start
Step-2: Declare variables num1, num2, and sum.
Step-3: Read values num1 and num2.
Step-4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step-5: Display sum.
Step-6: Stop

 Write an algorithm to find the largest among three different numbers entered by
user.

Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Qualities of a good Algorithm

1. Inputs and outputs should be defined precisely.


2. Each step in algorithm should be clear and unambiguous.
3. Algorithm should be most effective among many different ways to solve a
problem.
4. An algorithm shouldn't have computer code. Instead, the algorithm should be
written in such a way that, it can be used in similar programming languages.
---------------------------

You might also like