ich habe ein Programm geschrieben, dass eine Dezimalzahl in eine Dualzahl (Binärzahl) umwandeln soll.
Leider gibt mir das Programm keine Werte aus. Ich mache den Programmierkurs erst seit drei Wochen, also erschlagt mich bitte nicht.

Es wäre echt nett, wenn ihr mir helfen könntet.
Code: Alles auswählen
#include <stdio.h>
int main()
{
int n;
printf("Please enter a number between 0 and 31. The number must be an integer.");
scanf("%d", &n);
if(n < 0 || n > 31)
{
printf("The number is out of range. Please retry.");
}
else
{
void d2b(int n);
}
return 0;
}
void d2b(int n)
{
int x;
for(int i = 6; i >= 0; i--)
x = n >> i;
if(x & 1)
printf("1");
else
printf("0");
}
1. Das Programm soll Dezimal- in Dualzahlen konvertieren.
2. Die Dezimalzahl soll zwischen 0 und 31 liegen. Falls nicht, soll das Programm dies feststellen und dem Benutzer mitteilen.
3. Es soll eine Funktion genutzt werden, die d2b(int n) heißen soll.
4 Was mich irritiert ist die Aufforderung "The function should return no value." Daher kam ich auf eine void Funktion.
Ich bedanke mich im voraus für eure Hilfe.

Mein erster entwurf war dierser hier, er hat aber auch nicht funktionier.
Code: Alles auswählen
#include<stdio.h>
int main()
{
int n;
int a, b, c, d, e;
printf("Please enter a number between 0 and 31. The number must be an integer!");
scanf("%d", &n);
if(n < 0 || n > 31)
{
printf("The entered value is out of range. Please enter an integer number between 0 and 31!");
}
else
{
void d2b(int n);
}
printf("%d %d %d %d %d", a, b, c, d, e);
return 0;
}
void d2b(int n) // Example: 25
{
int a, b, c, d, e;
int u, v, w;
n % 2 == e; // 25%2 = 1
n / 2 == u; // 25/2 = 12
u % 2 == d; // 12%2 = 0
u / 2 == v; // 12/2 = 6
v % 2 == c; // 6%2 = 0
v / 2 == w; // 6/2 = 3
w % 2 == b; // 3%2 = 1
w / 2 == a; // 3/1 = 1
// This leads to a b c d e = 11001 -> 16 + 8 + 0 + 0 + 1 = 25
return 0;
}