Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

M .

Arslan Safdar(Roll # 42)


BSCS(1st Semester) Test 1
Program#1:
/* Write a program in c++ that reads the co-
efficients a,b and c of the quadratic equation
ax^2+bx+c=0
and prints the real solutions of x using quadratic
formula.If the solutions are not real, then display
an appropriate message on the screen.*/

#include <iostream>

#include <cmath>
using namespace std;
int main()
{
float a, b, c, x1, x2, des;
cout<<"Enter the co-efficients of the equation
a,b and c ";
cin>>a>>b>>c;
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
if(a==0.0){
cout<<"UNDEFINED"<<endl;
return 1;
}
else{
des = sqrt((b*b)-4*a*c);
if(des>=0){
cout<<"The roots are real"<<endl;
}
else{
cout<<"The roots are not real"<<endl;
}
x1 = (- b + des)/(2*a);
x2 = (- b - des)/(2*a);
cout<<"The roots are x1 = "<< x1 << "
and x2 = " << x2<<endl;
return 0;
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
}
}
Output:
When roots are real:

When roots are not real:

In this example roots are not real and are complex


numbers.
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
Program#2:
/*Write a c++ program that inputs temperature and
display the message as follows:
Temperature Message
Greater than 35 Hot Day
Between 25 and 35 Pleasant Day
Less than 25 Cool Day

*/
#include <iostream>
using namespace std;
int main()
{
int temp;
cout<<"Enter the temperature ";
cin>>temp;
if(temp>35){
cout<<"Its a hot day";
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
}
else if(temp>=25 && temp<=35){
cout<<"Its a pleasant day";
}
else{
cout<<"Its a cool day";
}
}
Outputs:
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
Program#3:
/*Write a program that inputs a floating-point
number,an opeator and another floating-point
number.
It displays the result by performing he operation on
the given numbers . If the operator is division,
it should check to make sure that the divisor is not
equal to zero . If the operator is not " + , - , * or / "
then the program should display an error message.
*/
#include <iostream>
using namespace std;
int main()
{
float n1, n2, result;
char oper;
cout<<"Enter a floating-point number ";
cin>>n1;
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
cout<<"Enter a operator ";
cin>>oper;
cout<<"Enter a floating-point number ";
cin>>n2;
switch(oper){
case '+':
result=n1+n2;
cout<<n1 << " + " << n2 <<" = " <<
result<<endl;
break;
case '-':
result=n1-n2;
cout<<n1 << " - " << n2 <<" = " <<
result<<endl;
break;
case '*':
result=n1*n2;
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
cout<<n1 << " x " << n2 <<" = " <<
result<<endl;
break;
case '/':
if(n2==0){
cout<<"The divisor is zero"<<endl;
}
else{
result=n1/n2;
cout<<n1 << " / " << n2 <<" = " <<
result<<endl;
}
break;
default:
cout<<"The operator is invalid!"<<endl;
}
}
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1

Output:

Program#4:
//Write a program that inputs six integers.It finds
and print the smallest and largest integers.
#include <iostream>
using namespace std;
int main() {
int n1, n2, n3, n4, n5, n6, small, large;
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
cout << "Enter six integer ";
cin >> n1 >> n2 >> n3 >> n4 >> n5 >> n6;
small = n1;
large = n1;
// For smallest value....
if (n2 < small) {
small = n2;
}
if (n3 < small) {
small = n3;
}
if (n4 < small) {
small = n4;
}
if (n5 < small) {
small = n5;
}
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
if (n6 < small) {
small = n6;
}
// For largest value....
if (n2 > large) {
large = n2;
}
if (n3 > large) {
large = n3;
}
if (n4 > large) {
large = n4;
}
if (n5 > large) {
large = n5;
}
if (n6 > large) {
M . Arslan Safdar(Roll # 42)
BSCS(1st Semester) Test 1
large = n6;
}
// For Output....
cout << "The smallest integer is " << small <<
endl;
cout << "The largest integer is " << large << endl;
}
Output:

You might also like