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

Assignment 3 Part 1

The document is a programming assignment submitted by Umair Aslam, a student in the BS CS 1 (M) class at University of Narowal. The assignment contains examples of using while, for, and do-while loops in C++ programs to iterate through loops and perform tasks like printing numbers, calculating sums, and testing conditions.

Uploaded by

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

Assignment 3 Part 1

The document is a programming assignment submitted by Umair Aslam, a student in the BS CS 1 (M) class at University of Narowal. The assignment contains examples of using while, for, and do-while loops in C++ programs to iterate through loops and perform tasks like printing numbers, calculating sums, and testing conditions.

Uploaded by

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

University OF Narowal

Name:- Umair Aslam


Class:- Bs Cs 1 (M)
Subject:-Programming Fundamentals
Roll No:- 20-UON-0573
Assignment:- 3

Submitted to:-Miss Adeeba


Examples Of While
1)
#include<iostream>
using namespace std;
int main()
{
int i = 0;
while (i <= 20)
{
cout << i << " ";
i = i + 5;
}
cout << endl;
}
2)
#include<iostream>
using namespace std;
int main()
{
int i = 20; //Line 1
while (i < 20) //Line 2
{
cout << i << " "; //Line 3
i = i + 5; //Line 4
}
cout << endl;
}
3)
#include<iostream>
using namespace std;
int main()
{
int sum = 0;
int num;
cin >> num;
while (num>0)
{
sum = sum + num; //Add the number to sum
cin >> num; //Get the next number
}
cout << "Sum = " << sum << endl;
}
4)
#include<iostream>
using namespace std;
int main()
{
int i = 0;
while (i <= 50)
{
cout << i << " ";
i = i +7;
}
cout << endl;
}
5)
#include<iostream>
using namespace std;
int main()
{
int sum = 0;
int num;
cin >> num;
while (num<40)
{
sum = sum + num; //Add the number to sum
cin >> num; //Get the next number
}
cout << "Sum = " << sum << endl;
}
6)#include<iostream>
using namespace std;
int main()
{int noOfGuesses=0;
int isGuessed=0,guess=0,num=10;
while ((noOfGuesses < 5) && (!isGuessed))
{
cout << "Enter an integer greater than or equal to 0 and "
<< "less than 100: ";
cin >> guess;
cout << endl;
noOfGuesses++;
if (guess == num)
{cout << "Winner!. You guessed the correct number."
<< endl;
isGuessed = true;}
else if (guess < num)
cout << "Your guess is lower than the number.\n"<< "Guess again!" << endl;
else
cout << "Your guess is higher than the number.\n"<< "Guess again!" << endl; }}
7)
#include<iostream>
using namespace std;
int main()
{
int sum = 0;
int num;
cin >> num;
while (num<40)
{
sum = sum + num; //Add the number to sum
cin >> num; //Get the next number
}
cout << "Sum = " << sum << endl;
}
“Examples of For Loop”
1)

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

for (int i = 0; i < 10; i++)


cout << i << " ";
cout << endl;}

2)
#include<iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
cout << "Hello!" << endl;
cout << "*" << endl;
}}

3)#include<iostream>
using namespace std;
int main()
{int i;
for (i = 1; i <= 5; i++)
cout << "Hello!" << endl;
cout << "*" << endl;}

4) #include<iostream>
using namespace std;
int main()
{
int i;
for (i = 0; i < 10; i++) //Line 1
cout << "*" << endl;
}

5)
#include<iostream>
using namespace std;
int main()
{
int i;
for (i = 1; i <= 10; i++); //Line 1
cout << i << " "; //Line 2
cout << endl;
}
6)#include<iostream>
using namespace std;
int main()
{int i;
for (i = 1; ; i++)
cout << i << " ";
cout << endl;}

7) #include<iostream>
using namespace std;
int main()
{
int i,newNum, sum = 0;
for (i = 1; i <= 5; i++)
{
cin >> newNum;
sum = sum + newNum;
}
int average = sum / 5;
cout << "The sum is " << sum << endl;
cout << "The average is " << average << endl;}
“ Examples of do while”

1)
#include<iostream>
using namespace std;
int main()
{
int i = 0;
do
{
cout << i << " ";
i = i + 5;
}
while (i <= 20);}
2)
#include<iostream>
using namespace std;
int main()
{
int i = 11;

do
{
cout << i << " ";
i = i + 5;
}
while (i <= 10);
cout << endl;}

3)

#include<iostream>

using namespace std;


int main()

int score;

do

cout << "Enter a score between 0 and 50: ";

cin >> score;

cout << endl;

while (score < 0 || score > 50);

4)

#include <iostream >


using namespace std;
int main()
{
int num, temp, sum;
cout << "Enter a positive integer: ";
cin >> num;

cout << endl;


temp = num;
sum = 0;
do
{
sum = sum + num % 10; //extract the last digit
//and add it to sum
num = num / 10; //remove the last digit
}
while (num > 0);
cout << "The sum of the digits = " << sum << endl;
if (sum % 3 == 0)
cout << temp << " is divisible by 3" << endl;
else
cout << temp << " is not divisible by 3" << endl;
if (sum % 9 == 0)
cout << temp << " is divisible by 9" << endl;
else
cout << temp << " is not divisible by 9" << endl;
}
5)

#include<iostream>
using namespace std;
int main()
{
int i = 11;

do
{
cout << i << " ";
i = i + 5;
}
while (i <= 10);
cout << endl;}
7)

#include <iostream >

using namespace std;

int main()

int i = 0;

do

cout << i << " ";

i = i + 5;

while (i <= 50);

You might also like