Lab 1
Lab 1
Exercise 1.1
Write a program that prints the string Computing Systems & Programming to the
screen, reads an integer from the keyboard, and print the integer to the screen.
Exercise 1.2
Write the following program. Compile, link and run it.
#include<iostream>
using namespace std;
int main ()
{
int year;
float height;
year = 21;
height = 1.77;
cout << "Your age is: " << year << endl;
cout<< Your Height is: << height<<endl;
return 0;
}
Exercise 1.3
Write a program that demonstrates the use of the +,-,* and / operators for integers.
Type the following program. Compile, link and run it.
#include < iostream>
using namespace std;
1
int main( )
{int i=20;
int j;
cout<<i<<endl;
cout<<j<<endl;
j= i+5;
cout<<j<<endl;
i= j-10;
cout<<i<<endl;
i= i* j * 2;
cout<<i<<endl;
j= i / 10;
cout<<j<<endl;
return 0;
}
Exercise 1.4
Write a program that demonstrates the use of the +,-,* and / operators for floating point
numbers. Type the following program. Compile, link and run it.
#include <stdio.h>
using namespace std;
int main( )
{
doublei=1000.1001;
double j=123.234567;
double k;
k = i + j;
cout<<k<<endl;
k = i - j + 500.951;
cout<<k<<endl;
j = j * i;
cout<<j<<endl;
i = k / j;
cout<<i<<endl;
return 0;
}
Exercise 1.5
Write the following program. Compile, link and run it.
#include <stdio.h>
int main ()
{
char name1, name2, name3;
int age;
cout<<"Kindly enter 3 character representing your name
shortform:"<<endl;
cin>>name1>>name2>>name3;
Cout<<"Enter your age:";
Cin>>age;
cout<<"Hello<< name1<<name2<<name3<<, next year you
will be <<age+1<< years old"<<endl;
return 0;
}
Exercise 1.6
Write a program using the following algorithm:
Declare the integer variables num1, num2, num3, sum and average.
Get three numbers from the user.
Prompt the user for an integer.
Read an integer from the keyboard. Store it in num1.
Prompt the user for an integer.
Read an integer from the keyboard. Store it in num2.
Prompt the user for an integer.
Read an integer from the keyboard. Store it in num3.
Find the average of the three numbers.
sum = num1 + num2 + num3.
average = sum / 3.
Print average to the screen.
Exercise 1.7
Write the following program that illustrates the use of predecrement and postdecrement.
Compile link and run it.
#include <stdio.h>
int main ()
{
int a=10;
int b;
b=a--;
cout<<a<<a<<endl;
cout<<b<<b<<endl;
b=--a;
cout<<a<<a<<endl;
cout<<b<<b<<endl;
return 0;
}