0% found this document useful (0 votes)
20 views9 pages

Sheet 3 - Model Answer

The document contains a series of programming exercises for CSC1100, covering basic concepts in C++ such as calculating age, temperature conversion, number swapping, and determining even/odd numbers. It includes sample code snippets for each exercise, along with instructions for tasks like checking triangle types, validating baby weight, and identifying weekdays. Additionally, it addresses syntax corrections and output expectations for given code segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Sheet 3 - Model Answer

The document contains a series of programming exercises for CSC1100, covering basic concepts in C++ such as calculating age, temperature conversion, number swapping, and determining even/odd numbers. It includes sample code snippets for each exercise, along with instructions for tasks like checking triangle types, validating baby weight, and identifying weekdays. Additionally, it addresses syntax corrections and output expectations for given code segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSC1100: Lab Sheet3

1. Write a program that takes your year of birth and then


calculates the age

int yearOfBirth;

int age;

cout << "enter year of birth:" << endl;

cin >> yearOfBirth;

age = 2023 - yearOfBirth;

cout << "your age is:";

cout << age;

2. Write a program that converts the temperature from Celsius to


Fahrenheit

Note That:
F = (9.0/5.0) * C + 32

float F , C ;

cout << "Enter The Temperature In Celsius\n";

cin >> C;

F = (9.0 / 5.0 )*C + 32;

cout << "Temperature In Fahrenheit Is = " << F;

3. Write a program that swaps two numbers

int num1 , num2 , temp ;

cout << "enter two numbers:" << endl;

cin >> num1 >> num2;

1 CSC1100: Computer Programming I 2024


cout<<"Before:\nNumber_1 = "<<num1 << " Number_2 =
"<<num2<<"\n\n" ;

temp = num1 ;

num1= num2 ;

num1 = temp ;

cout<<"After:\nNumber_1 = "<<num2 << " Number_2 = "<<num1 ;

4. Given two numbers num1 , num2. Print the summation of their


last digit of the two numbers.

Input:
12 13

Output:
5

int num1 , num2 ;

cin>>num1>>num2;

int sum = num1%10 + num2%10 ;

cout<<" Summation = " << sum <<endl ;

5. Exercise on Prefix and Postfix

a) 20
b) 21
c) 22
d) 23 1 CSC1100: Computer Programming I 2024
a) 45
b) 55
c) 64
d) 46

a) 9
b) 10
c) 11
d) 12
a) 11
b) 12
1 CSC1100: Computer Programming I 2024
c) 13
int x=5, y=5,z;

x=++x;

y=--y;

z=x+ ++x;

cout << z;

6. program 5

Write a program that takes a number and returns if number even or


odd

int number;

cout << "enter a number:" << endl;

cin >> number;

if (number%2 == 0){

cout << "even";

}else{

14 CSC1100: Computer Programming I 2024


cout << "odd";

7. Program 6

Write a program that takes a number and displays “positive” ,


“negative” or “zero”

int number;

cout << "enter a number:" << endl;

cin >> number;

if (number == 0)

cout << "zero";

else if(number > 0)

cout << "positive";

else

cout << "negative";

5
1 CSC1100: Computer Programming I 2024
8. Correct the syntax errors in the following C++ program
#include iostream
main() {
Float x,y,z;
Cout < “enter two numbers”;
Cin>>a>>b
Cout<<‟the numbers in reverse order are‟<<b,a;}

9. Show the form of output displayed by the following statements


when total has the value 352.74
Cout << “the final total is: ”<<endl;
cout << “$” << total <<endl;
10.Write a program which accepts a character and display its ASCII
value.

11.Is the following a correct C ++ statement ?


if ( char C == „ Y „ ) then
cout << “\n you typed Y ” ;

12.What is the output of following program?


int result = 4 + 5 * 6 + 2;
cout<<result;
int a = 5 + 7 % 2;
cout<<a;

13.Draw a flowchart to show how to convert between degrees


Fahrenheit (F) to and degrees Celsius (C).
14.To display the following output using a single display
statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69
15.To accept two numbers and print their sum.
16.Write a program that reserves the t-shirt for each player
except number 10 because it is reserved.

Please enter t shirt number

16 CSC1100: Computer Programming I 2024


10
this t shirt is reserved

Int tshirt;
Cout<<“Enter the T shirt number :”<<endl;
Cin>>tshirt;
If(tshirt !=10){
Cout<<“Okay you have choosed the tshirt
number:”<<tshirt<<endl;
Else
Cout<<“this t shirt are reserved”<<endl;

17.PROGRAM 2
Write a program to check whether a triangle is Equilateral,
Isosceles or Scalene.

Example:
Please Enter Three Sides of a Triangle :
5 5 5
This is an Equilateral Triangle.

int side1, side2, side3;


cout << "Please Enter Three Sides of a Triangle = ";
cin >> side1 >> side2 >> side3;
if(side1 == side2 && side2 == side3) {
cout << "This is an Equilateral Triangle.\n";
} else if(side1 == side2 || side2 == side3 || side1 == side3) {
cout << "This is an Isosceles Triangle.\n"; }
else
cout << "This is a Scalene Triangle.\n";

18.PROGRAM 3

Write a program that checks whether an input number lies


between specified range.

Example :
Please enter your range:

17 CSC1100: Computer Programming I 2024


20 50
Please enter Number.
34
In range

int StartRange ,EndRange,Number;


cout<<"Please enter your range: ";
cin>>StartRange>>EndRange;
cout<<"PLease enter your number: ";
cin>>Number;
if(Number >= StartRange && Number <= EndRange ){
cout<<"In range\n";
} else{ cout<<"Not in range \n";}

19.PROGRAM 4

Write a program that determines whether a baby’s weight is


normal or not. For girls, normal babies weight are 2.5 to 4.5
KG. On the other hand, for boys the normal weights are 4 to
5.5 KG.
Example:
Please enter gender(M|F)
M
Please enter Weight:
5
Normal

char gender;
int weight;
cout<<"Please enter gender( M |F ):";
cin>>gender;
cout<<"Please enter Weight : ";
cin>>weight;
if((gender == 'F' || gender == 'f') &&(weight >= 2.5 && weight <=
4.5)){
cout<<"Normal \n" ; }
else if((gender == 'M' || gender == 'm') &&(weight >= 4 &&
weight <= 5.5)){
cout<<"Normal \n" ; }
else{ cout<<"NOT Normal \n"; }

18 CSC1100: Computer Programming I 2024


20.PROGRAM 5
Write a Program Decide if input day is weekday or part of
weekend based on its number.

Example:
Enter day.
1
Weekday

int day;
cout << "Enter day\n";
cin >> day;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
cout << "Weekday\n";
break;
case 6:
case 7:
cout << "Weekend\n";
break;
default:
cout << "Invalid day\n";
break; }

91 CSC1100: Computer Programming I 2024

You might also like