Click here to Skip to main content
14,637,542 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include <iostream>
#include <cmath>
using namespace std;

unsigned numDigits(const unsigned n) {
    if (n < 10) return 1;
    return 1 + numDigits(n / 10);
}

unsigned rotated_number(const unsigned a,const unsigned n){
    
    unsigned int e,divide_by,rotated_number,f;
    divide_by = pow(10,n - 1);
    e = a%10;
    f = a/10;
    rotated_number = e * divide_by + f;
    return rotated_number;
    // std::cout << rotated_number << std::endl;
}

unsigned square_sum(unsigned n){
    int sum=0;
    while(n!=0)
    {
        sum = sum + (n%10) * (n%10);
        n = n/10;
    }
    // std::cout << sum << std::endl;
    return sum;
    
}

int main() {
    
    unsigned int a,length,ssd,number_obtained,r_n,_z,new_rpm=0,n=0,max_rpm;
    std::cin >> a;
    max_rpm = a * 8;
    
    while(n < 1 && a < max_rpm){
    // std::cout << a << std::endl;
    number_obtained = square_sum(a) * 323;
    // std::cout << number_obtained << std::endl;
    length = numDigits(a);
    // std::cout << length << std::endl;
    r_n = rotated_number(a,length)%100;
    // std::cout << r_n << std::endl;
    new_rpm = number_obtained + r_n;
    // std::cout << _z << std::endl;
    // new_rpm = new_rpm + _z;
    // std::cout << new_rpm << std::endl;
    a = new_rpm;
    // std::cout << a << std::endl;
    n = n + 1;
}

    std::cout << a << std::endl;

}


What I have tried:

convert into C language
i tried but i could not
so convert this C++ code into C code
Posted
Updated 5-Jul-20 20:13pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is not a code translation service: we are not here to do that for you.

And if you look at it, with the exception of a few cin and cout lines, it's already C.
If you can't cope with working out how to replace those ... you probably shouldn't be on your course.

But ... you should realise that your tutor is aware of sites like this, and will be very much aware that you didn't write the code you hand in? Many will treat plagiarism very seriously ...
   
Comments
CPallini 6-Jul-20 2:07am
   
5.
Rate this:
Please Sign up or sign in to vote.

Solution 2

It should be to hard you only need to use C input and output functions like print() and some scan. This is explained in this C input and output tutorial.
   
Comments
CPallini 6-Jul-20 2:13am
   
5.
Rate this:
Please Sign up or sign in to vote.

Solution 3

As already noted by OriginalGriff and KarstenK, such a code is very C-like (doesn't use the peculiar features of the C++) and is already almost C-compatible. As a matter of fact I was able, replacing the included header files and the only two active I/O calls with the corresponding C ones, to compile it (and run it) using the C compiler. You should be able to do the same.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100