Assignment1BDue November 8
Assignment1BDue November 8
// 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;
}
// 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
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` )
return 0;
} //end main()
Precedence Summary
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.
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