0% found this document useful (0 votes)
4 views

LAB 2 (1)

This document outlines a lab focused on C++ programming, covering data types, operators, and assignment statements. It includes sample tasks and exercises that guide users through writing programs to perform basic input and output operations, arithmetic calculations, and displaying messages. The lab aims to provide hands-on experience with fundamental programming concepts using Dev C++.

Uploaded by

sherazqasim874
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LAB 2 (1)

This document outlines a lab focused on C++ programming, covering data types, operators, and assignment statements. It includes sample tasks and exercises that guide users through writing programs to perform basic input and output operations, arithmetic calculations, and displaying messages. The lab aims to provide hands-on experience with fundamental programming concepts using Dev C++.

Uploaded by

sherazqasim874
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab No.

2
C++ programming structure, operators, and assignment
statement

Objective :
In this lab we will
• Learn about data types which are used to define any variable
• Learn about operators and learn how to combine different types of arithmetic opera
tors to form arithmetic expressions.
Tools:
Dev C++
Background Knowledge:
This lab is about the basic C programming structure included cin, cout and endl
operators. We will also learn the data types included integer, floating point, character
and string. In this practice we will implement these data types using different
examples. Together with data types you will also deal with mathematical operators
such as +,*
Assignment Statement
This statement is used to assign a value to a variable is called assignment statement
The assignment statement evaluates an expression and then assigns its value to a
variable. The assignment operator is “=” is used to assign the calculated value to a
variable.
Syntax:
var=expression ;
Where,
var àvariable name to which the value of expression is assigned
expressionàan expression may be a combination of operands and arithmetic operators
.
Sample Tasks
Task 1
Write a program to input length and width in any two variable.

#include<iostream>
using namespace std;
int main ( )
{
int length, width;
cin >> length;
cin >> width;

}
Task 2
Write a program to input length and width in any two variable and print the
result on the output screen

#include<iostream>
using namespace std
int main ( )
{
cout << "Enter the length: "<<endl;
cin >> length;
cout << "Enter the width: "<<endl;
cin >> width;

Task 3
Write a program to assign two variables by assignment statement. Assign the
values and print the result on the screen
#include<iostream>
using namespace std;
Int main ( )
{
int a,b;
a=200;
b=100;
cout<<< “value of a=”<<a<<endl;
cout<< “value of b =”<<b<<endl;

Task4
Write a program to print a message on screen by using “endl” manipulator.

Int main()
{
cout<<"THIS IS A BOOK "<<endl<<" ENGLISH";
}

Task5
Write a program in C++ to get two numbers from keyboard during program
execution. Calculate the sum and product of the numbers and then print the
result on the computer screen.
#include<iostream>
Int main()
{
int n1,n2,s,p;
cout<<"Enter first number ?";
cin>>n1;
cout<<"Enter secound number ?";
cin>>n2;
s=n1+n2;
p=n1*n2;
cout<<"sums of numbers="<<s<<endl;
cout<<"products of numbers="<<p<<endl;
}

Exercise :
Exercise 1:
Write a program to print your name and registration number on the output screen
Solution:

Marks Obtained Total Marks

Instructor Signatures:

Remarks:

Exercise 2
Write a program to print the menu as below:
This is my first program
My name is Ali
I am electrical engineer want to serve mankind
Solution:

#include<iostream>
using namespace std;
int main (){
cout<<"This is my first program"<<endl;
cout<<"My name is Ali"<<endl;
cout<<"I am electrical engineer want to serve mankind";
}

Marks Obtained Total Marks

Instructor Signatures:

Remarks:

Exercise 3
Write a program in C to input three values from user at run time and print the
summation and multiplication of these three values on the output screen
Solution:
#include<iostream>
using namespace std;
int main (){
int a,b,c,sum,multiply;

cout<<"Enter value of a= ";


cin>>a;
cout<<"Enter vlaue of b= ";
cin>>b;
cout<<"Enter value of c= ";
cin>>c;
sum=a+b+c;
multiply=a*b*c;
cout<<"Sum of a,b and c= "<<sum<<endl;
cout<<"Multiplication of a,b and c= "<<multiply<<endl;

Marks Obtained Total Marks

Instructor Signatures:

Remarks:
Exercise 4:
Write a program in c to input the marks of any three subjects from user at run time
and calculate the total and average marks. Also show the result on the output screen.
Solution:
#include<iostream>
using namespace std;
int main (){
float Math, English, Science, Total, Average;
cout<<"Marks of Math= ";
cin>>Math;
cout << "Marks of English= ";
cin>>English;
cout<<"Marks of Science= ";
cin>>Science;
Total=Math+English+Science;
Average=(Math+English+Science)/3;
cout<<"Total Marks= "<<Total<<endl;
cout<<"Average= "<<Average<<endl;

}
Marks Obtained Total Marks

Instructor Signatures:

Remarks:

Exercise 5:
Write a program to get name and age (in years) of a person. Calculate the age in
months and print the name of person and its age in months.
Solution:

#include<iostream>
using namespace std;
int main (){
string name;
cout<<"Enter Your Name= ";
cin>>name;
int age,p;
cout<<"Enter Your Age= ";
cin>>age;
p=age*12;
cout<<"Age in Months= "<<p<<endl;
}

Marks Obtained Total Marks

Instructor Signatures:

Remarks:

You might also like