enter image description here
code--
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x, a, b, c = 0;
cout << "Enter the number" << endl;
cin >> x;
for (int i = 0; i <= 7; i++)
{
a = pow(10, I);
b = 10 * ((x % a) - c) / a;
cout << b;
c = x % a;
}
}
The code is written to reverse the numbers but for 1024 vs code shows 04301000 and onlinegdb shows 04201000.
You don't even need pow:
#include <iostream>
int main()
{
unsigned int x = 0, y = 0;
std::cout << "Enter the number: ";
std::cin >> x;
while (x > 0) {
y = 10 * y + (x % 10);
x /= 10;
}
std::cout << y << std::endl;
}
Or if you don't want to remember the result and just print it, like the original:
#include <iostream>
int main()
{
unsigned int x = 0;
std::cout << "Enter the number: ";
std::cin >> x;
while (x > 0) {
std::cout << x % 10;
x /= 10;
}
std::cout << std::endl;
}
Related
#include <iostream>
#include <algorithm>
using namespace std;
void sumrange(int x, int y) {
int sum;
cout << "Enter the first number: ";
cin >> x;
cout << "Enter the second number: ";
cin >> y ;
int bnum = max(x,y);
int snum = min(x,y);
int i = snum;
while (i < bnum) { sum =+ i; i++; }
cout << sum;
}
int main() {
int x, y;
sumrange(x,y);
return 0;
}
I can't get the right answer from my input I tried 1 and 4 and the answer was 3 but it supposes to be 1+2+3+4 which is 10.
Couple of observations:
don't use using namespace std; it is a bad practice and might lead to name conflicts
variable int sum needs to be explicitly initialized before use
sum =+ i should be sum += i
you need to use <= if you want to include the upper bound
take a look at the arithmetic sequence
sumrange function doesn't need parameters, sice they are passed by value int x, y; from your main are not used at all
Following code should do what you want:
#include <iostream>
#include <algorithm>
void sumrange()
{
int x;
int y;
int sum = 0;
std::cout << "Enter the first number: ";
std::cin >> x;
std::cout << "Enter the second number: ";
std::cin >> y ;
int bnum = std::max(x,y);
int snum = std::min(x,y);
int i = snum;
while (i <= bnum) { sum += i; i++; }
std::cout << sum;
}
int main() {
sumrange();
return 0;
}
You have inverted the += sign to =+
It should be sum += i .
sum = +i assigns the number to itself
Edit: you should also initialize sum, stated in the comment
There are 3 problems with your given code snippet.
Problem 1: =+ should be += in the statement:
sum =+ i; //=+ should be +=
Problem 2: The variable sum is uninitialized and so it has a garbage value. Using that garbage value leads to undefined behavior.
Problem 3: Should use <= instead of < in the statement
while (i < bnum) //should be i<=bnum
So the modified(corrected) program looks like below:
#include <iostream>
#include <algorithm>
void sumrange(int x, int y) {
int sum=0;///INITIALIZE sum
std::cout << "Enter the first number: ";
std::cin >> x;
std::cout << "Enter the second number: ";
std::cin >> y ;
int bnum = std::max(x,y);
int snum = std::min(x,y);
int i = snum;
while (i <= bnum) //USED <=
{
sum += i; //USED +=
i++;
}
std::cout << sum;
}
int main() {
int x, y;
sumrange(x,y);
return 0;
}
Alternative solution
Note that you can find the sum without using a loop as shown below:
#include <iostream>
#include <algorithm>
void sumrange(int x, int y) {
int sum=0;//initialize sum
std::cout << "Enter the first number: ";
std::cin >> x;
std::cout << "Enter the second number: ";
std::cin >> y ;
sum = (std::abs(x - y) + 1) * (x + y) / 2; //ADDED THIS
std::cout << sum;
}
int main() {
int x, y;
sumrange(x,y);
return 0;
}
A C++20 solution :
#include <algorithm>
#include <iostream>
#include <numeric>
#include <ranges>
int main()
{
int a{ 0 };
int b{ 0 };
std::cout << "Enter the first number: ";
std::cin >> a;
std::cout << "Enter the second number: ";
std::cin >> b;
auto lower_bound = std::min(a, b);
auto upper_bound = std::max(a, b) + 1; // iota view is exclusive so add 1
auto view = std::ranges::iota_view{ lower_bound , upper_bound };
auto sum = std::accumulate(view.begin(), view.end(), 0);
std::cout << "sum = " << sum << "\n";
return 0;
}
constexpr auto sumrange(const int x, const int y) noexcept {
auto sum = 0;
if (x < y) for (auto i = x; i <= y; ++i) sum += i; else
if (y < x) for (auto i = y; i <= x; ++i) sum += i;
return sum;
}
int main() {
constexpr auto x = 1;
constexpr auto y = 3;
constexpr auto r = sumrange(x, y); // 1 + 2 + 3
std::cout << r << '\n'; // 6
}
Values of the function arguments are not used in this call
int x, y;
sumrange(x,y);
So such a call does not make a sense.
You need in main to ask the user to enter a range of numbers and pass the range to the function.
The task of the function is to return the calculated sum for the specified range in main.
Within the function sumrange the variable sum is not initialized.
int sum;
You need to initialize it to 0.
In this while loop
while (i < bnum) { sum =+ i; i++; }
^^
there is a typo. Instead of the compound assignment operator += you are using the assignment operator = and the unary + operator
Moreover as it follows from your comment you need also to add the value of bnum to the sum.
So the loop should look at least like
while (i <= bnum) { sum += i; i++; }
Pay attention to that in general an overflow can occur. So it is better to declare the variable sum as having the type long long int.
The function and the program can look the following way
#include <iostream>
#include <utility>
#include <algorithm>
long long int range_sum( int x, int y )
{
long long int sum = 0;
auto [first, last] = std::minmax( { x, y } );
do
{
sum += first;
} while (first++ != last);
return sum;
}
int main()
{
int x = 0, y = 0;
std::cout << "Enter the first number: ";
std::cin >> x;
std::cout << "Enter the second number: ";
std::cin >> y;
std::tie( x, y ) = std::minmax( { x, y } );
std::cout << "The sum of numbers in the range ["
<< x << ", " << y << "] is equal to "
<< range_sum( x, y )
<< '\n';
}
The program output might look like
Enter the first number: 1
Enter the second number: 10
The sum of numbers in the range [1, 10] is equal to 55
My code goes like this
#include <iostream>
#include <string>
using namespace std;
int main() {
string z,zz ="";
int x,y;
cin >> x >> y >> z >> zz;
for (int a = 1; a <= x; ++a) {
cout << z;
for (int b = 1; b <= y; ++b) {
cout << zz;
}
}
cout << z;
return 0;
}
If my input is
3 9 n a
It should print out
0aaaaaaaaa1aaaaaaaaa2aaaaaaaaa3
But my code prints out
naaaaaaaaanaaaaaaaaanaaaaaaaaan
If you:
Move the declaration of a outside of the outer for loop.
Change both cout << z; statements to cout << a;.
Change the outer for loop to start at 0 instead of 1, and to use < insted of <=.
Then you will get the output you want.
#include <iostream>
#include <string>
using namespace std;
int main() {
string z, zz;
int x, y;
cin >> x >> y >> z >> zz;
int a;
for (a = 0; a < x; ++a) {
cout << a;
for (int b = 1; b <= y; ++b) {
cout << zz;
}
}
cout << a;
return 0;
}
Online Demo
Note that in this case, z becomes redundant and can be eliminated if you can remove n from your input.
So after learning some Python, I decided to check out C++ and give it a try and decided to try and code Collatz Conjecture in Xcode.
Here's what I got.
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
while (x != 1) {
if (x % 2 == 0) {
x /= 2;
std::cout << x << " ";
} else {
x = (3 * x) + 1;
std::cout << x << " ";
}
}
return 0;
}
int main() {
collatz();
}
Extra Question: In Python there is condition called elif, how is it called in C++?
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x;
std::cin >> x;
while (x != 1) {
x = x % 2 ? x / 2 : x * 3 + 1;
std::cout << x << ", ";
}
std::cout << "\b\b \b\b" << std::endl;
return 0;
}
int main() {
collatz();
}
You can do something like this if you don't want comma at last
Add a check not to add comma for last digit
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
while (x != 1) {
if (x % 2 == 0) {
x /= 2;
std::cout << x ;
} else {
x = (3 * x) + 1;
std::cout << x ;
}
if (x!=1)
std::cout<<",";
}
return 0;
}
int main() {
collatz();
}
Output
Enter a number: 5
16,8,4,2,1
Using your original code (I see you've edited it), you can do this if you want to avoid an extra conditional and multiple calls through cout in your loop.
#include <iostream>
#include <iostream>
#include <sstream>
int collatz()
{
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
std::string output;
while (x != 1)
{
if (x % 2 == 0)
{
x /= 2;
output += std::to_string(x) + ", ";
}
else
{
x = (3 * x) + 1;
output += std::to_string(x) + ", ";
}
}
output.replace(output.rfind(',', output.length()), 2, "");
std::cout << output.c_str();
return 0;
}
Probably the only thing that needs explaining here is output.replace(). rfind() finds the last comma, starting at the end. replace then clobbers the two characters at that position (", ").
I wrote the following code but only works when the first number is symmetry:
symmetry is like this number: 4554 (reading from both ends is the same number)
My question is why the break only works for the first number? It happens when I run it.
#include <iostream>
using namespace std;
int main()
{
int n=0, z=0, r=0, x=0, m;
for (;;) {
cout << "Enter number: ";
cin >> x;
m = x;
while(x!=0) {
r = x % 10;
z = z * 10 + r;
x = x / 10;
}
if(m==z)
break;
else
n++;
}
cout << n;
return 0;
}
Move int z=0, r=0; inside for loop.
Why not using this code to know if the number is symmetry?
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int x;
for (;;) {
cout << "Enter number: ";
cin >> x;
m = x;
std::stringstream str;
str << x;
std::string number = str.str();
if ( number == std::string( number.rbegin(), number.rend() )
break;
else
n++;
}
cout << n;
return 0;
}
Simpler, leads to the same result and is definitely more error prone ;-)
It would have been much easier to reason if you have written like this:
#include <iostream>
bool isPalindrome(int x)
{
int y = x;
int z;
while (x) {
z = z * 10 + x % 10;
x /= 10;
}
return y == z;
}
int main()
{
int n = 0;
int x;
for (;;) {
std::cout << "Enter number: ";
std::cin >> x;
if (isPalindrome(x))
break;
else
++n;
}
std::out << "Number of non-palindromes: " << n << std::endl;
return 0;
}
functions with meaningful names always helpful!
I am currently doing a task in a book which asks me to calculate the mathematical constant e using the while loop. I managed that fairly easily, however I am having troubles calculating e^x, whereas the user inputs x and the degree of accuracy. The code I used for computing e is:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
while (x <= degreeOfAccuracy)
{
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
e += (1/number);
x++;
}
cout << endl << "The mathematical constantr e is: "
<< setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
However, when I tried e^x the following code returned a completely wrong value:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
exponent = temp;
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
int counterr = 1;
while (counterr < x)
{
exponent *= exponent;
counterr++;
}
e += (exponent/number);
x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
Any ideas where the calculations went wrong?
This line:
exponent *= exponent;
is wrong. It should be:
exponent *= temp;