Click here to Skip to main content
15,356,641 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#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 12-Feb-22 9:40am
v2

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.
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.
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.
   
Converting is simple:

1. Rename the File from .cpp to .c
2.Replace the includes like this:
C
// #include <iostream>
// #include <cmath>
// using namespace std;
#include <stdio.h>
#include <math.h>
3. Then replace
std::cin with scanf()
and
std::cout with printf()
Check the syntax and you're done.
   
v2
Comments
Dave Kreskowiak 12-Feb-22 19:07pm
   
And almost two years late on his homework assignment. Conversion questions like this always happen because someone gets a homework assignment for class and they can't write it themselves, so they go to the web to find the code already posted but find it in another language. They end up coming here to beg for a "conversion" so they can hand it in as their own work, completely missing the point of thinking about the problem and breaking it down step-by-step.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900