user3731938
user3731938

Reputation: 21

Convert simple javascript program into C

Hello I have some experience with javascript but I would really like to learn how to program in C and one of the ways I am trying to learn is by converting some simple javascript code into C. My current attempt at converting a simple program compiles without any errors however doesn`t produce the answer I want it to. The javscript code produces the correct answer and I wrote it to solve project euler problem number 5 which can be found here: https://projecteuler.net/problem=5

Here is the working js code:

var number = 2520;
var count = 1;
var solved = false;

while (!solved) {
    if (number % count === 0) {
        if (count === 20) {
            solved = true;
            console.log(number);
            } else {
                count++;
            }
    } else {
      number++;
      count = 1;
    }
}

Here is the C conversion which does not work:

#include <stdio.h>

int main () {
    unsigned int number = 2520;
    unsigned int count = 1;
    unsigned int solved = 0;
    while ((solved = 0)) {
        if (number % count == 0) {
            if (count == 20) {
                solved = 1;
                printf("%number");
            } else {
                count++;
            }
        } else {
            number++;
            count = 1;
        }
    }
    return 0;
}

Upvotes: 1

Views: 141

Answers (3)

wholerabbit
wholerabbit

Reputation: 11546

while ((solved = 0)) {

You can use the same syntax you would use in js here, namely, while (!solved), or ==, but just = is an assignment.

printf("%number");

Doesn't mean what you think it means, which is why it's not an actual error (%n is a distinct specifier, and with no corresponding input, you'd get umber as the output). To reproduce console.log() you'd want:

printf("%d\n", (int)number);

Or

printf("%u\n", number);

Notice the explicit \n, since printf() does not add a newline otherwise.

Upvotes: 1

IdeaHat
IdeaHat

Reputation: 7881

I know its already answered, but you should know why.

while ((solved = 0))

Will actually set solved to 0 AND return 0 (which is interpreted as false). So the while loop is exited right away.

printf also takes a pretty strictly formatted string for the first one (just typeing what makes sense is guarenteed to be wrong). The compiler has know way to know what is inside the string is anything other than a string (C++ has (almost) NO reflection, unlike javascript: your written code dissapears into ones and zeros). printf needs to take number in as the second argument. Try printf("%i\n",number);. That says "Print an integer followed by a newline. The integer's value is number."

Welcome to C! Your biggest problem going into is is going to be my biggest problem with Java Script: C is strictly typed with no reflection, while javascript has no types with almost everything relying on some sort of reflection.

Upvotes: 0

Emanuele Paolini
Emanuele Paolini

Reputation: 10162

Replace

while (solved = 0)

with

while (!solved)

and

print("%number")

with

print("%u\n",number)

Upvotes: 0

Related Questions