assigment 2
assigment 2
1
#include <iostream>
using namespace std;
int swap(int n)
{
int l = n % 10; // last is for left
int f = n; //f is for first i wrote these as a comments so its not
confusing
while (f >= 10)
{
f /= 10;
}
n = l * 10;
while (temp > 0)
{
n = n * 10 + temp % 10;
temp /= 10;
}
n = n * 10 + f;
return n;
}
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << "After swapping the first and last digits: " << swap(n) << endl;
return 0;
}
2
#include <iostream>
using namespace std;
int main()
{
int num, tcount = 0, sum = 0;
while (true)
{
cin >> num;
if (num == 0)
{
break;
}
tcount++;
return 0;
}
3
#include <iostream>
using namespace std;
int main()
{
int n;
double pi = 0.0;
int i = 0;
while (i < n)
{
double term = 1.0 / (2 * i + 1);
if (i % 2 == 0)
{
pi += term;
}
else
{
pi -= term;
}
i++;
}
pi *= 4;
cout << "number of terms " << n << " result " << pi << endl;
return 0;
}