السلام عليكم ورحمة الله وبركاته ،،
اخواني انا مطلوب مني اكتب برنامج يحول اي رقم يدخل له من المستخدم بالنظام العشري decimal الى نظام الـ binary
مثال 13 ----- يطبعها -----> 1101
لكن البرنامج مطلوب ان يحل بطريقة ال recursive وليس iterative
أنا كتبت كود والمشكله انه يطبع العدد ال binary عكس يعني بدال ما يطبع الـ 13 ---> 1101 يطبعها 1011
وهذا خلل في ال function لكن عجزت وانا أحاول أعدله ممكن اي مساعده منكم في تغيير طريقة ال function ؟
#include <stdio.h>
int convert (int x); // prototype
int n = 0; // defining a global integer
int main()
{
int x,binary; // defining 2 integers
printf("Enter a decimal number: \n");
scanf("%d",&x); // decimal number entered by the user
binary = convert(x); // callin the function convert
printf("%d",binary); // printing the result
return 0;
}
int convert (int x) // function to convert from decimal to binary
{
if (x/2 == 0) // if condition
return x%2; // returns the last digit in the binary number
else
{
n = x%2; // converting
printf ("%d",n); // prints the binary number except the last digit
x = x/2; // continue converting
return convert (x); // recursive step
}
return 0;
}