0% found this document useful (0 votes)
27 views1 page

Sub II.1.d.cpp

The document describes an algorithm that takes a natural number n as input and reverses its digits. It initializes variables p, m, and k, then iterates while n is not equal to 0. It takes the last digit of n at each step by taking the modulo 10, multiplies it by the place value p, and adds it to the running total m. It then divides n by 10 to remove the last digit and multiplies p by 10 to increase the place value before the next iteration. Finally, it outputs the reversed number m.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Sub II.1.d.cpp

The document describes an algorithm that takes a natural number n as input and reverses its digits. It initializes variables p, m, and k, then iterates while n is not equal to 0. It takes the last digit of n at each step by taking the modulo 10, multiplies it by the place value p, and adds it to the running total m. It then divides n by 10 to remove the last digit and multiplies p by 10 to increase the place value before the next iteration. Finally, it outputs the reversed number m.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1

/

**********************************************************************
********

citeşte n (număr natural)


p1; m0; k0
┌cât timp n≠0 execută
│ citește x (număr natural)
│ x[x/10]
│┌dacă x≠0 atunci cx%10
││altfel cn%10
│└■
│ mc*p+m
│ n[n/10]
│ pp*10; kk+1
└■
scrie m

**********************************************************************
*********/
#include <iostream>

using namespace std;

int n,x,c,p,m,k;

int main()
{
cout << "n (numar natural) = ";
cin >> n;
p = 1; m = 0; k = 0;
while (n != 0)
{
cout << "x (numar natural) = ";
cin >> x;
x /= p;
if ( x != 0 )
c = x%10;
else
c = n%10;
m += c*p;
n /= 10;
p *= 10;
k++;
}
cout << endl << "m = " << m;
return 0;
}

You might also like