0% found this document useful (0 votes)
26 views5 pages

Task 1:: Main (

The document contains code snippets for three programming tasks. Task 1 swaps the values of two variables using pass by reference. Task 2 calculates equivalent resistance in series and parallel circuits by passing a resistance value by reference. Task 3 contains functions to (a) count digits of a number, (b) reverse a number, and (c) check if a number is a palindrome by passing values by reference.

Uploaded by

Faiza
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)
26 views5 pages

Task 1:: Main (

The document contains code snippets for three programming tasks. Task 1 swaps the values of two variables using pass by reference. Task 2 calculates equivalent resistance in series and parallel circuits by passing a resistance value by reference. Task 3 contains functions to (a) count digits of a number, (b) reverse a number, and (c) check if a number is a palindrome by passing values by reference.

Uploaded by

Faiza
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/ 5

Task 1:

#include<iostream>
using namespace std;

void swapbyref(int& x, int& y) {


int z = x;
x = y;
y = z;
}

int main() {
int firstNum = 10;
int secondNum = 20;

cout << "Before swap: " << "\n";


cout << "firstNum = " << firstNum << "\t" << "secondNum =" << secondNum << "\n";

swapbyref(firstNum, secondNum);

cout << "After swap: " << "\n";


cout << "firstNum = " << firstNum << "\t" << "secondNum = " << secondNum << "\n";

Task 2:
#include <iostream>
using namespace std;
void Series(double& R);
void Parallel(double& R);
int main()
{
int decision; double r; int Req = 0;
cout << "Press 0 to calculate resistance in Series" << endl;
cout << "Press 1 to calculate resistance in Parallel" << endl;
cin >> decision;
cout << " Enter the resistance (press -1 to end) " << endl;
if (decision == 0) {

cin >> r;
Series(r);
cout << "In series equilance resistance is : " << r << endl;
}

if (decision == 1)
{

cin >> r;
Parallel(r);
cout << "In Parallel equilance resistance is : " << r << endl;
}

return 0;
}
void Series(double& R) {
double Req = 0;

while (R != -1)
{

Req = Req + R;
cout << " Enter the resistance ( press -1 to end) " << endl;
cin >> R;
}
R = Req;
}
void Parallel(double& R) {
double Req = 0;

while (R != -1)
{

Req = Req + 1 / R;

cin >> R;
}
R = 1 / Req;
}

Task 3:

(a)
#include<iostream>
using namespace std;
void digits_counter(int&, int&);
int main()
{
int n;
int i = 0;
cout << " enter num : ";
cin >> n;
cout << endl;
while (n != 0) {
n = n / 10;
i++;
}
cout << "num of digits : ";
cout << i << endl;
return 0;
}
void digit_counter(int& n, int& i)
{
n;
}

(b)
#include<iostream>
using namespace std;

void reverse(int n, int* rev) {

int r, re = 0;
while (n > 0) {
r = n % 10;
re = re * 10 + r;
n = n / 10;
}
*rev = re;
}

int main() {

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

reverse(n, &rev);
cout << "Reverse number is:" << rev;

return 0;
}

(c)
#include<iostream>
using namespace std;

bool palindrome_checker(int n, int* p) {

int r, rev = 0;
while (n > 0) {
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
*p = rev;
}

int main() {

int n, p = 0;

cout << "Enter a number:";


cin >> n;

palindrome_checker(n, &p);
if (p == n) {
cout << "Number is palindrome:" << n;
}
else {
cout << "Number is not palindrome:" << n;
}

return 0;
}

You might also like