Professional Documents
Culture Documents
Lab Task 2:: Output of The Following C++ Code Fragment?
Lab Task 2:: Output of The Following C++ Code Fragment?
Lab Task 2:: Output of The Following C++ Code Fragment?
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello world!";
cout<<"I'm a C++ program";
return 0;
}
Output:
Error 1 :
#include <iostream>
using namespace std;
void main()
{
cout<<'c++ programing question and answers';
Input :
#include <iostream>
using namespace std;
int main(void)
{
cout<<"c++ programing question and answers" <<endl;
return 0;
}
Output :
Error 2 :
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello world!";
cout<<'sir syed university of engineering and technology';
cout<<"computer science";
cout<<"programming fundamentals";
return 0;
}
Input :
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello world!"<<endl;
cout<<"sir syed university of engineering and technology"<<endl;
cout<<"computer science"<<endl;
cout<<"programming fundamentals"<<endl;
return 0;
}
Output :
Error 3 :
#include [iostream]
int main()
{
std::cout<<"Hello world!".return 0;
}
Input :
#include <iostream>
int main()
{
std::cout<<"Hello world!";
return 0;
}
Output :
LAB TASK 3:
1- Write a program that take four floating numbers from keyboard and
prints their
sum, product and average.
Input :
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
float n1,n2,n3,n4,sum,pro,avg;
cout<<"Please Enter Any Four Numbers.\n";
cin>>n1>>n2>>n3>>n4;
sum= n1+n2+n3+n4;
pro= n1*n2*n3*n4;
avg= (n1+n2+n3+n4)/4;
cout<<"Addition of Four Number is: "<<sum<<endl;
cout<<"Product of Four Number is : "<<pro<<endl;
cout<<"Avg of Four Number is : "<<avg<<endl;
getch();
}
Output :
2- Write a program to calculate the area of the circle, taking the value
of the radius from the user
Input :
#include <iostream>
using namespace std;
int main()
{
float radius, area;
Output :
3- Write a program that prints to calculate the age in days by using the
formula: days = years * 365.
Input :
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
void main()
{
int age,days;
cout<<"Please Enter Your Age: \n\n";
cin>>age;
days=age*365;
cout<<"\nYour Age in Days: "<<days;
getch();
Output :
4- Write a C++ program to convert temperature from degree Celsius to
Fahrenheit. /* celsius to fahrenheit conversion formula */ fahrenheit =
(celsius * 9 / 5) + 32;
Input :
return 0;
}
Output :
5- Mention output for following code fragment.
Output :
LAB TASK 1
1- Use MS Visual C++ 2013 to create a project named, Lab1 and
then add a .CPP file named, First to this project. Now add given
piece of code to file Lab1 and compile this file. Execute this file
afterwards and check the output of your program.
Input :
#include<iostream>
using namespace std;
int main ()
{
cout<< " My first program in c++" ; return 0;
}
Output :
# include<iostream>
using namespace std;
int main ( )
{
int x; x=5;
cout<< " x = " << x;
return 0;
}
Output :
Input :
#include <iostream>
using namespace std;
int main(void)
{
cout << " \n Sir Syed University of Engineering and Technology "
<<endl;
cout << " \t \n programming fundamentals using C++ "<<endl;
return 0;
}
Output :
4- What is the output from the following C++ code fragment?
Input :
#include <iostream>
using namespace std;
int main(void)
{
cout << "x =" <<5<<endl;
cout << "Y= " <<10<<endl;
cout << "x+y= " <<5+10<<endl;
return 0;
}
Output :
LAB TASK 4
#include<stdio.h>
void main()
{
int a=100;
if(a>10)
printf("Shahid Afridi");
else if(a>20)
printf("Shoaib Akhtar");
else if(a>30)
printf("Kamran Akmal");
}
Output :
2- Write a program that takes a number as input from user and
checks whether the Number is even or odd. Using if-else:
Output :
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter your roll number:";
cin>>num1;
cout<<"Enter your friend roll number:";
cin>>num2;
if(num1>num2)
{
cout<<"Your roll number "<<num1<<" is the largest";
}
else
{
cout<<"Your friend roll number "<<num2<<" is the largest";
}
return 0;
}
Output :
#include <stdio.h>
void main()
{
int vote_age;
Output :
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the three numbers"<<endl;
cin>>a>>b>>c;
if(a>b && a>c)
cout<<a<<" is Greater than : "<<b<<" and "<<c;
else if (b>a && b>c)
cout<<b<<" is Greater than : "<<a<<" and "<<c;
else
cout<<c<<" is Greater than : "<<b<<" and "<<a;
}
Output :
#include <stdio.h>
int main()
{
int phy, chem, bio, math, comp;
float per;
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);
return 0;
}
Output :
LAB TASK 5
1. What would be the output of following program ?
#include <iostream>
using namespace std;
int main()
{
int i = 2 ;
switch ( i )
{
case 1 :
cout<<"I am in case 1 \n"<<endl;
case 2 :
cout<<"I am in case 2 \n"<<endl;
case 3 :
cout<<"I am in case 3 \n"<<endl;
default :
cout<<"I am in default \n"<<endl;
}
}
Output :
int main() {
char op;
float num1, num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output :
3. Write a program print total number of days in a month using
switch case
Input :
#include <iostream>
int main()
{
int monthnumber;
switch(monthnumber)
{
case 1: cout<<"31 days";
break;
case 2: cout<<"28 or 29 days";
break;
case 3: cout<<"31 days";
break;
case 4: cout<<"30 days";
break;
case 5: cout<<"31 days";
break;
case 6: cout<<"30 days";
break;
case 7: cout<<"31 days";
break;
case 8: cout<<"31 days";
break;
case 9: cout<<"30 days";
break;
case 10: cout<<"31 days";
break;
case 11: cout<<"30 days";
break;
case 12: cout<<"31 days";
break;
default: cout<<"Invalid input!!! enter month number between 1-
12";
}
return 0;
Output :
4. Write a Program to take the value from the user as input all sides
of a triangle and check whether the triangle is valid or not. Using
switch statement
Input :
#include<iostream>
using namespace std;
int main ()
{
int sidea,sideb,sidec;
cout<<"enter side of triangle"<<endl;
cin>>sidea>>sideb>>sidec;
int sum;
sum=sidea+sideb+sidec;
switch(sum)
{
case 180:
{
cout<<"triangle is valid";
}
break;
default:
{
cout<<"the triangle is not valid";
}
}
}
Output :
5. Using switch statement Write a program to input marks of five
subjects Physics, Chemistry, Biology, Mathematics and
Computer. Calculate percentage and grade according to
following: Percentage >= 90% : Grade A Percentage >= 80% :
Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade
D Percentage >= 40% : Grade E Percentage < 40% : Grade F
Input :
#include<iostream>
using namespace std;
int main()
{
int phy,chem,bio,maths,comp;
int total;
float percent;
cout<<"Enter marks of computer";
cin>>phy;
cout<<"enter marks of Chemistry";
cin>>chem;
cout<<"enter marks of bio";
cin>>bio;
cout<<"enter marks of maths";
cin>>maths;
cout<<"enter marks of physics";
cin>>comp;
total= phy+chem+bio+maths+comp;
percent=total/5.0;
if(percent>=90)
{
cout<<"Grade=A";
}
else if(percent>=80)
{
cout<<"Grade=B";
}
else if(percent>=70)
{
cout<<"Grade=C";
}
else if(percent>=60)
{
cout<<"Grade=D";
}
else if(percent>=40)
{
cout<<"Grade=E";
}
else
{
cout<<"Grade F";
}
}
Output :