Lab Task 2:: Output of The Following C++ Code Fragment?

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 24

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;

cout << "Enter the radius of circle : ";


cin >> radius;
area = 3.14 * radius * radius;
cout << "Area of circle with radius "
<< radius << " is " << 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 :

// C++ program to convert temperature from celsius to fahrenheit


#include <iostream>
using namespace std;
int main() {
float fahren, celsius;

cout << "Enter the temperature in celsius\n";


cin >> celsius;
fahren = (celsius * 9 / 5) + 32;
cout << celsius <<"Centigrade is equal to " << fahren
<<"Fahrenheit";

return 0;
}
Output :
5- Mention output for following code fragment.

cout << "Size of char : " << sizeof(char) << endl;


cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

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 :

2- Use MS Visual C++ 2013 to create a project named, Prog2 and


then add a .CPP file named, Second to this project. Now add
given piece of code to file Prog2 and compile this file. Execute
this file afterwards and check the output of your program.
Input :

# include<iostream>
using namespace std;
int main ( )
{
int x; x=5;
cout<< " x = " << x;
return 0;
}

Output :

3- What is the output from the following C++ code fragment?

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

1- Mention the output for the following program :

#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 :

3- Write a program that declares and initializes two numbers with


your_roll_no and your_friend_roll_no and displays the greater of
the two.

#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 :

4- Write a program to read the age of a candidate and determine


whether it is eligible for casting his/her own vote.

#include <stdio.h>
void main()
{
int vote_age;

printf("Input the age of the candidate : ");


scanf("%d",&vote_age);
if (vote_age<18)
{
printf("Sorry, You are not eligible to caste your vote.\n");
printf("You would be able to caste your vote after %d
year.\n",18-vote_age);
}
else
printf("Congratulation! You are eligible for casting your
vote.\n");
}

Output :

5- Write a program to find the maximum between three numbers by


using the if-else statement.

#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 :

6- Write a program to take a value from the user as input marks of


five subjects Physics, Chemistry, Biology, Mathematics, and
Computer. Calculate percentage and grade according to the
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, Write this program with the help of if-else statement.

#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);

per = (phy + chem + bio + math + comp) / 5.0;

printf("Percentage = %.2f\n", per);

if(per >= 90)


{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 70)
{
printf("Grade C");
}
else if(per >= 60)
{
printf("Grade D");
}
else if(per >= 40)
{
printf("Grade E");
}
else
{
printf("Grade F");
}

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 :

2. Write a program to create Simple Calculator using switch case


# include <iostream>
using namespace std;

int main() {
char op;
float num1, num2;

cout << "Enter operator: +, -, *, /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> 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>

using namespace std;

int main()
{
int monthnumber;

cout<<"Enter month number(1-12): ";


cin>>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 :

You might also like