Assignment 2
Assignment 2
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>
int main() {
while (!duplicateFound) {
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>
int main() {
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>
int main() {
int num;
int count = 0;
cout << "Enter a signed integer (" << count << "/100): ";
if (num < 0) {
break;
count++;
continue;
cout << "Total numbers entered: " << count << endl;
return 0;
}
Question # 4
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>
x = y;
y = temp;
cout << "Inside swapByValue - x: " << x << ", y: " << y << endl;
x = y;
y = temp;
cout << "Inside swapByReference - x: " << x << ", y: " << y << endl;
*x = *y;
*y = temp;
cout << "Inside swapByPointer - x: " << *x << ", y: " << *y << endl;
int main() {
unsigned int a, b;
cin >> a ;
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;