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

Assignment 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Comsats University Islamabad

Department of Physics

Submitted By:
SYED HUSSAIN MURTAZA
SP24-ELC-097

Class Section:
B

Course Name:
PROGRAMING FUNDAMENTALS
Instructor:
DR FARZANA SHAHEEN
Question # 1 Use a while loop to write a C++ program that asks the user to
input a series of integers until the user inputs a duplicate number
#include <iostream>

using namespace std;

int main() {

int num, previousNum;

bool duplicateFound = false;

cout << "Enter an integer: ";

cin >> previousNum;

while (!duplicateFound) {

cout << "Enter an integer: ";

cin >> num;

if (num == previousNum) {

duplicateFound = true;

cout << "Duplicate number found: " << num << endl;

} else {

previousNum = num;

return 0;

}
Question # 2 Write a C++ program that displays the digits of an unsigned
number in reverse order, e.g., display 45 as 54. Use for loopto do this.

#include <iostream>

using namespace std;

int main() {

unsigned int num;

cout << "Enter an unsigned integer: ";

cin >> num;

cout << "Reversed number: ";

for (; num > 0; num /= 10) {

cout << num % 10;

cout << endl;

return 0;

}
Question # 3 Use a for loop to take signed integers as an input from user. The
program should stop taking input if the user enters negative number
otherwise it should continue. (Note: Use break and continue statement, For
loop should count at least 100 values)
#include <iostream>

using namespace std;

int main() {

int num;

int count = 0;

for (; count < 100; ) {

cout << "Enter a signed integer (" << count << "/100): ";

cin >> num;

if (num < 0) {

cout << "Negative number entered. Stopping input." << endl;

break;

cout << "You entered: " << num << endl;

count++;

continue;

cout << "Total numbers entered: " << count << endl;

return 0;

}
Question # 4

Write a C++ program to take 2 unsigned values from user. Swap


these values using

functions with

Pass by value

Pass by reference

Pass by pointers

Display the values in main before and after function call in each
of these cases.

#include<iostream>

using namespace std;

void swapByValue(unsigned int x, unsigned int y) {

unsigned int temp = x;

x = y;

y = temp;

cout << "Inside swapByValue - x: " << x << ", y: " << y << endl;

void swapByReference(unsigned int &x, unsigned int &y) {

unsigned int temp = x;

x = y;

y = temp;

cout << "Inside swapByReference - x: " << x << ", y: " << y << endl;

void swapByPointer(unsigned int *x, unsigned int *y) {

unsigned int temp = *x;

*x = *y;

*y = temp;
cout << "Inside swapByPointer - x: " << *x << ", y: " << *y << endl;

int main() {

unsigned int a, b;

cout << "Enter first unsigned integers: ";

cin >> a ;

cout << "Enter second unsigned integers: ";

cin >> b ;

cout<<endl;

cout << "Before swapping (in main) - a: " << a << ", b: " << b <<
endl;

cout<<endl;

swapByValue(a, b);

cout << "After swapByValue (in main) - a: " << a << ", b: " << b <<
endl;

swapByReference(a, b);

cout << "After swapByReference (in main) - a: " << a << ", b: " << b
<< endl;

swapByPointer(&a, &b);

cout << "After swapByPointer (in main) - a: " << a << ", b: " << b <<
endl;

return 0;

You might also like