[go: up one dir, main page]

0% found this document useful (0 votes)
98 views10 pages

C++ Loop Exercises and Solutions

ASSIGNMENT CSC415

Uploaded by

2022478618
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)
98 views10 pages

C++ Loop Exercises and Solutions

ASSIGNMENT CSC415

Uploaded by

2022478618
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/ 10

ASSIGNMENT 2(REPETITION)

LAB 7
NUR SALINDA BINTI SAPAREDI
2022478618

1. Using while loop, produce a timetable for a number input by the user. Example
output as follows:

#include<iostream>
using namespace std;
main()
{

int number;
int timetable = 0;
int n = 0;
cout << "Enter a number: ";
cin >> number; //input from the use

cout << " " <<endl;


while (n < 12)
{
n++;
timetable = number * n; //multiple all number

cout << " " << number << " x " << n << " = " << timetable << endl; //display the timetable
}
}
2. Write a C++ program with do..while loop to identify whether a number input by the user
is positive or negative number. This process will continue until the user wishe to stop
entering a number.

#include<iostream>
using namespace std;
main()
{

char answer = 'y';


int number = 0 ;
cout << "Enter a number : " ;
cin >> number;

do
{
if (number %2 == 0)
{
cout << number << " is a positive number " << endl;

cout<<" "<<endl;
}
else{
cout << number << " is a negative number " << endl;

cout<<" "<<endl;
}
cout << "Do you wish to continue (y/n)?" <<endl;
cin >> answer ;

cout<<" "<<endl;
cout<<"Enter a number : " ;
cin>>number;
}
while(answer == 'y');
}
3. Write a C++ program based on following pseudocode.

#include<iostream>

using namespace std;


main()
{
int total = 0;

int counter = 0;
int score;
cout << "Enter scores : "<<endl;
cin >> score;

while(score != -1)
{
total += score;
counter++;

cin >> score;


}
cout << "Average of score is : "<< total/counter<<endl;
}
4. Answer the following question.

#include<iostream>

using namespace std;


main()
{
int age = 0,total = 0,count;

char status;
cout<<"Enter your age : ";
cin>>age;
do

{
if(age > 35)
{
cout<<"\nStatus [Married 'M' ,Single 'S']: ";
cin>>status;
if(status == 'S')
count++;
}
else

{
cout<<"\nStatus [Married 'M' ,Single 'S']: "<<endl;
cin>>status;
}
cout<<" "<<endl;
cout<<"Total = "<<count<<endl;

cout<<" "<<endl;
cout<<"Enter your age : "<<endl;
cin>>age;
}

while(age != -1);
}

You might also like