#include <stdio.h>
#include <string.h>
#include <math.h>
/*this function convers decimals to binary*/
int convert (int decimal)
{
int remain, result, bittest = 2147483647;
int counter1 =0;
for (counter1=0; counter1<32; counter1++)
{
/*if num is greater or equal to bittest then 1*/
if(decimal>=bittest)
{
printf("1");
decimal=decimal-bittest;
}
else
/*if num is less then bittest then 0*/
printf("0");
bittest=bittest/2;
/*prints space after every 8th number*/
if (counter1 == 7 || counter1 == 15 || counter1 == 23)
printf(" ");
}
printf("\n");
counter1=0;
}
/*check that the input is between 0 and 2147483647*/
int input_check (int correct)
{
while (correct < 0 || correct > 2147483648)
{
printf("\nWrong entry. Number must be between 0 and 2147483647");
scanf("%d", &correct);
}
}
/*this function calculates the fibonacci sequence*/
int fibonacci (int num1, int num2)
{
int sum, counter2;
sum, counter2 =0;
while (counter2 < 8)
{
sum=num1+num2;
/*prints the last number without coma*/
if (counter2 == 7)
printf("%d", sum);
else
printf("%d,", sum);
num1=num2;
num2=sum;
counter2++;
}
}
/*this function calculates the 30th fibonacci number and return the value to main*/
int fibonacci30 (int num1, int num2)
{
int sum, counter2;
sum, counter2 =0;
while (counter2 < 29)
{
sum=num1+num2;
num1=num2;
num2=sum;
counter2++;
}
return sum;
}
/*main program*/
main()
{
int selection, binary, fib1, fib2;
printf("\nWelcome to the Math Function Menu");
printf("\n1.) Integer to Binary converter");
printf("\n2.) Fibonacci generator");
printf("\n999.) Exit");
printf("\nEnter selection ---> ");
scanf("%d", &selection);
printf("\n");
while (selection != 999)
{
/*if selection is wrong it will prompt for it again*/
while (selection != 1 && selection != 2 && selection != 999)
{
printf("\nWrong selection. Enter 1, 2 or 999 to Exit: ");
scanf("%d", &selection);
}
switch(selection)
{
/*case 1 will be integer to binary conversion*/
case 1:
printf("\nEnter a number between 0 and 2147483647 inclusive: ");
scanf("%d", &binary);
/*calls a function that checks corrent input in case 1*/
input_check(binary);
/*calling a function convert that converts decimals to binary*/
convert(binary);
break;
case 2:
printf("Enter first number: ");
scanf("%d", &fib1);
printf("Enter second number: ");
scanf("%d", &fib2);
/*while first number is greater then second number this loop will run*/
while (fib1 > fib2)
{
printf(" Second number must be greater then first number.\n");
printf("Enter first number: ");
scanf("%d", &fib1);
printf("Enter second number: ");
scanf("%d", &fib2);
}
printf("%d,", fib1);
printf("%d,", fib2);
/*call function to calculate the finonacci sequence*/
fibonacci(fib1, fib2);
printf("Fib [30, {%d,%d}] = %d", fib1, fib2, fibonacci30(fib1, fib2));
break;
default:
printf("\nDefaul in the case\n");
break;
}
printf("\nWelcome to the Math Function Menu");
printf("\n1.) Integer to Binary converter");
printf("\n2.) Fibonacci generator");
printf("\n999.) Exit");
printf("\nEnter selection ---> ");
scanf("%d", &selection);
printf("\n");
}
}
decimal to 32 bit binary and Fibonacciconversion from decimal to binary and Fibonacci sequence
Page 1 of 1
2 Replies - 6672 Views - Last Post: 31 October 2007 - 09:48 AM
#1
decimal to 32 bit binary and Fibonacci
Posted 30 October 2007 - 09:37 AM
I have to create a program that does 2 things. 1. it convers any decimal to a 32 bit binary and 2. do fibonacci sequence with any inputed numbers. I have figured out the fibonacci sequence but I cant seem to get the binary conversion. Im stuck on getting the correct BITTEST number. I currently have it equal to 2147483647 in my convert function, even though i know its wrong. Can anyone please help me with this?
Replies To: decimal to 32 bit binary and Fibonacci
#2
Re: decimal to 32 bit binary and Fibonacci
Posted 30 October 2007 - 11:27 PM
#3
Re: decimal to 32 bit binary and Fibonacci
Posted 31 October 2007 - 09:48 AM
Thank you very much jpw1991! You were absolutely right. I was using variable bittest as an int instead of a double. I got it working.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|