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

Lab 1

This lab covers introductory C++ programming concepts like arithmetic statements, input/output, and mathematical functions. It includes 6 exercises that have students write and run simple programs to output text, perform basic math operations on integers and floats, take user input, and demonstrate pre/post-decrement operators. The programs get progressively more complex, starting with outputting simple text and ending with performing calculations on user-provided values.

Uploaded by

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

Lab 1

This lab covers introductory C++ programming concepts like arithmetic statements, input/output, and mathematical functions. It includes 6 exercises that have students write and run simple programs to output text, perform basic math operations on integers and floats, take user input, and demonstrate pre/post-decrement operators. The programs get progressively more complex, starting with outputting simple text and ending with performing calculations on user-provided values.

Uploaded by

FaIz Fauzi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 1

This lab exercise covers:


an introduction to C++ programming
arithmetic statements
Standard input output
Mathematical functions

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;

// Print the value of i and j


//Integer addition
//Integer subtraction.
//Integer multiplication.
//Integer division

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;
}

You might also like