I made a code in c that converts decimal to binary and viceversa, the problem i have is that when i do the convertion from decimal to binary it prints the numbers in the wrong order, for example, if i input the number 123 as a decimal it should print 11110110 but my program converts it as 10111110
here is the code:
#include <stdio.h>
#include <stdlib.h>
main()
{
int i, no, coc, re, opt;
int dec;
int bin, p, c;
printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
printf("\nDecimal No.: ");
scanf("%d",&no);
if (no>2)
{
printf("Your binary No.: ");
for (i=1;1<=no; i++)
{
coc=no/2;
no=coc;
re=coc%2;
printf("%d", re);
}
}
break;
case 2: printf("\nBinary No.: ");
scanf("%d",&bin);
c=1;
dec=0;
while(bin>0)
{
p=0;
p=c*(bin%10);
dec+=p;
c*=2;
bin/=10;
}
printf("Your decimal No.: %d",dec);
break;
default:
printf("\a\n\nError");
break;
}
return 0;
}
If you want your program to work correctly, consider storing the bits in an array and printing it from the last element. But that will be overkill for a simple program. Consider the following simple program. It just uses bitwise operations (generally, considered fast).
#include <stdio.h>
#include <stdlib.h>
static void DecToBin(n)
{
int i=2;
while(i<n)
{
i=i<<1;
}
for(;i>0;i=i/2)
{
printf("%d",i&n);
}
}
main()
{
int no, opt;
int dec;
int bin, c;
printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
printf("\nDecimal No.: ");
scanf("%d",&no);
if (no>0)
{
printf("Your binary No.: ");
void DecToBin(n);
}
break;
case 2: printf("\nBinary No.: ");
scanf("%d",&bin);
c=1;
dec=0;
while(bin>0)
{
dec+=c*(bin%10);
c*=2;
bin/=10;
}
printf("Your decimal No.: %d",dec);
break;
default:
printf("\a\n\nError");
break;
}
return 0;
}
I have used the bitwise method. I hope it helps.
"<<" bitwise left shift
"&" bitwise and.
Related
#include<stdio.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main() {
int numeris1;
int numeris2;
int numeris3;
int octal;
int amzius;
int a=0;
char veiksmas;
char name[20];
printf("Ivesk Varda: ");
scanf("%s", name);
printf("Ivesk Amziu: ");
scanf("%d",&amzius);
printf("Tavo vardas: %s.\n", name);
printf ("Tavo Amzius: %d.\n", amzius);
do{
printf("\n1. Uzduoties salyga \n2. Uzduoties atlikimas \n3. Uzdaryti programa\n");
printf("Iveskite pasirinkima: ");
scanf("%d", &veiksmas);
switch (veiksmas)
{
case 1 :
printf("\nIvedami trys dyzelinio traukinio vagono ilgiai astuntaineje skaiciavimo sistemoje,\n");
printf("kuriuos programa sudeda ir rezultata atspauzdina sesioliktaineje skaiciu sistemoje\n");
break;
case 2 :
printf("\nIveskite pirma traukinio ilgi: ");
scanf("%o",&numeris1);
printf("\nIveskite antro traukinio ilgi: ");
scanf("%o",&numeris2);
printf("\nIveskite trecio traukinio ilgi: ");
scanf("%o",&numeris3);
octal=numeris1+numeris2+numeris3;
printf("\nVisu traukiniu vagonu ilgis...\n");
printf("%x\n",octal);
break;
case 3 :
return 0;
break;
default : printf("%c blogas pasirinkimas \n\n", veiksmas);
}
}while(1);}
Im writing code so it converts octal to hexa numbers
but while im in my menu ant enter a letter opose to a number my console just starts looping my menu over and over.
For example if i enter a wrong number in my menu i made so it says wrong number choose again. And i want the same to do for when you enter a letter to print blogas pasirinkimas
I am a bit new to c programming. For my project I have been tasked with developing a simple calculator that asks the user to input an option then that option declares what operation is to be performed (i.e. if the user enters 1, this corresponds to selecting the addition option which then allows the user to add two numbers they choose. I have the majority of the code worked out, but the catch is that I need the calculator to toggle between int and double variables. When the user enters 5, the calculator should now work with integers then if the user hits 5 again, the calculator switches back to doubles and vice versa so long as you want to switch back and forth. The calculator automatically works with doubles. So, more specifically, if I wanted to use integer variables, I would enter 5, then lets say I wanted to switch back to doubles, I should enter five and receive the message "Calculator now works with doubles." So here is my code thus far:
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
int main()
{
int integermode, doublemode, m, a, b, sum2, difference2, product2,
quotient2;
double i, j, sum1, difference1, product1, quotient1;
printf("This program implements a calculator.");
while(m !=6) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
if(m > 6) {
printf("Invalid option.\n");
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
}
switch (m) {
case 1:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The sum is: %d\n", a+b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The sum is: %.15lf\n", i+j);
}
break;
case 2:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The difference is: %d\n", a-b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The difference is: %.15lf\n", i-j);
}
break;
case 3:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The product is: %d\n", a*b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The product is: %.15lf\n", i*j);
}
break;
case 4:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
if (b != 0) printf("The quotient is: %d\n", a/b);
if (b == 0) printf("Cannot divide by zero!\n");
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
if(j != 0) printf("The quotient is: %.15lf\n", i/j);
if(j == 0) printf("Cannot divide by zero!\n");
break;
}
case 5:
if (m = 5) {
integermode = true;
printf("Calculator now works with integers.\n");
}
if (m != 5) integermode = false;
}
}
return 0;
}
General improvements
First off, you should notice your main function is massive for what it needs to accomplish, and takes a ton of indent levels (when properly indented). That is mostly because you've got a ton of duplicated functionality. You could easily extract the code that asks for the input numbers into a function and reuse it, changing only the operation performed in each case. Or probably even better, you could ask for the input numbers as a part of the main while loop, and then perform a different operation with them depending on the operation mode you're in.
You're not using <math.h>, so you can leave it out.
Another thing I've noticed is you don't initialize the m variable before using it in the while loop. That could potentially cause a lot of problems, so you should initialize it to some value that isn't a valid mode, like for example 0 or -1.
You're also using two consecutive if statements when if/else could be used.
if (integermode == true) {
.....
}
if (integermode == false) {
.....
}
is the same as
if (integermode == true) {
.....
} else {
.....
}
but harder to read and less efficient, as you're doing two comparisons instead of one.
Implementation of double/integer modes
About the functionality you want to implement, you could very well define an enumeration with the possible modes:
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
and then have a variable holding the current mode:
data_mode datamode = INTEGER_MODE /* Initializing this to the default mode */
then use integers or doubles during the calculation and output depending on the current value of the variable. Of course, this could be done in a shorter way with an integer instead of an enum and a typedef, but I find this to be a more verbose way.
For the inputs and the result, you could use the union C type, which reserves memory for the biggest type you specify inside it and lets you store any of them in it. For example:
union io {
int i;
double d;
} a, b, result;
declares three variables that have the io union as type. That way, you can use them as integers or doubles as you like (as long as you don't mix types up and, for example, store an integer and read it as a double)
Full working implementation
The following would be my implementation of the program you have to do. It could be accomplished in a much shorter way (take into account the switches dealing with enums take a fair bit of the total space) but I think this is way more elegant and verbose.
I've respected your print statements and the flow of your application, even though I think they could be better (maybe this is something your assignment imposes on you?)
Please take this as an opportunity to learn and don't just copy-paste the code.
#include <stdio.h>
#include <stdlib.h>
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
typedef enum _operationmode {
ADDITION,
SUBSTRACTION,
MULTIPLICATION,
DIVISION,
TOGGLE,
EXIT
} operation_mode;
operation_mode ask_mode () {
int input = -1;
while (1) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%i", &input);
if (input < 1 || input > 6) {
printf("Invalid option.\n");
} else {
switch (input) {
case 1:
return ADDITION;
case 2:
return SUBSTRACTION;
case 3:
return MULTIPLICATION;
case 4:
return DIVISION;
case 5:
return TOGGLE;
case 6:
return EXIT;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
}
}
}
union _io {
int i;
double d;
};
void get_inputs_integer (int *a, int *b) {
printf("Enter first term: ");
scanf("%i", a);
printf("Enter second term: ");
scanf("%i", b);
}
void get_inputs_double (double *a, double *b) {
printf("Enter first term: ");
scanf("%lf", a);
printf("Enter second term: ");
scanf("%lf", b);
}
int main (int argc, char **argv) {
union _io operand1, operand2, result;
operation_mode o_mode;
data_mode d_mode = INTEGER_MODE;
printf("This program implements a calculator.");
do {
o_mode = ask_mode();
if (o_mode == TOGGLE) {
if (d_mode == INTEGER_MODE) {
d_mode = DOUBLE_MODE;
printf("Calculator now on double mode\n");
} else {
d_mode = INTEGER_MODE;
printf("Calculator now on integer mode\n");
}
} else if (o_mode != EXIT) {
if (d_mode == INTEGER_MODE) {
get_inputs_integer(&operand1.i, &operand2.i);
switch (o_mode) {
case ADDITION:
result.i = operand1.i + operand2.i;
break;
case SUBSTRACTION:
result.i = operand1.i - operand2.i;
break;
case MULTIPLICATION:
result.i = operand1.i * operand2.i;
break;
case DIVISION:
result.i = operand1.i / operand2.i;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %i\n", result.i);
} else {
get_inputs_double(&operand1.d, &operand2.d);
switch (o_mode) {
case ADDITION:
result.d = operand1.d + operand2.d;
break;
case SUBSTRACTION:
result.d = operand1.d - operand2.d;
break;
case MULTIPLICATION:
result.d = operand1.d * operand2.d;
break;
case DIVISION:
result.d = operand1.d / operand2.d;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %lf\n", result.d);
}
}
} while (o_mode != EXIT);
}
What seems to be the problem in my code? I wish to make a program that makes allows the user to choose from a list of choices/menu. The user chooses one and a function runs. After the function executes the menu appears again until the user decides to quit.
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d, &choice");
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d, &x");
int evaluate(int x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d, &x");
int binaryPrinter(int x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d, &x");
int hexaPrinter(int x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d, &x, &y");
int militaryTime(int x, int y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
This is what is displayed after inputing the number of choice: Process returned -1073741819 <0xC0000005>
-EDIT-
I have recognized the mistake I have made. But the result's still the same.
here's my new code, but same error: Process returned -1073741819 <0xC0000005>
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("%d", &x);
hexaPrinter(x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime(x, y);
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
Try this code
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do
{
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d", &x);
hexaPrinter( x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime( x, y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
You have used wrong syntax of the scanaf()
it's not like scanf("%d,&choice");
it is like scanf("%d",&choice);
And also missed the syntax of function call
it's not like int evaluate(int x); it is like evaluate(x)
Your scanf statements are all wrong. It usually takes to parameters. A format string containing format specifiers to determine what kind of value and how many values to read and a pointer to the variable were to store the read value (multiple pointers in case of multiple specifiers).
// What you got so far
scanf("%d, &choice"); // <-- format string and pointer to variables are combined
// How it should look like
scanf("%d", &choice); // format string "%d" containing 1 format specifier to read an int
// and a pointer to the variable choice to store the read value
Ok now to the next mistake. When declaring a function you need to write down the whole function header (returnType functionName(ParameterType1 Parameter1, ParameterType2 Parameter2, ...)) however when you want to call that funtion all it needs is its name and the parameters BUT whithout theire type.
So declaring a function like this int evaluate(int num) like you did at the very beginning of your code is fine but when calling it in your switch all it needs is the name (evaluate) and the values, or variables you want to pass to the function as parameters (evaluate(x)).
So now all together:
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice); // scan an integer value and store it in choice
switch (choice)
{
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
...
}
So I wrote this program for a Programing in C Assignment. It useds a switch menu entry to pick between Randomly generated problems from Addition, Subtraction, Multiplication, and Division.
The only problem I had when I made it was the division section. How do you deal with randomly generated numbers that don't evenly divide into. So I thought, "Ok don't even deal with decimal points with them as floats and just go straight integers. then use mod of the two randomly generated digits to have the use enter in the remainder if there is any."
So this is what I cam up with.
void division(){
int a,b;
int ans, uans;
int remain, uremain;
a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;
printf("What is\n");
printf(" %.0f\n",a);
printf(" ÷ %.0f\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&uans);
printf("\n Remainder = ");
scanf("%d",&uremain);
ans = a / b;
remain = a % b;
if(ans==uans && remain==uremain){
printf("\n\nCorrect!.......\n\n");
printf("\n\nThe answer was %d with a remainder of %.0f\n",ans, remain);
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d with a remainder of %d\n",ans, remain);
}
}
Is there any better way to do this to account for maybe not having to enter in remainder if there isn't a remainder I'm assuming i could do something like if( remain > 0) to see if there is a remainder and then add it but just wondering what others think.
For context here is the full program
/*****************************
CS 50 - Programing in C
Math tutor
Write a program that displays a menu as shown in the sample run.
You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication,
or division test. After a test is finished, the menu is redisplayed.
You may choose another test or enter 5 to exit the system.
Each test generates two random single-digit numbers to form a question for addition,
subtraction, multiplication, or division.
For a subtraction such as number1 – number2,
number1 is greater than or equal to number2.
For a division question such as number1 / number2, number2 is not zero.
******************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int displaymenu();
void addition();
void subtraction();
void multiplication();
void division();
int main(){
int com;
srand(time(NULL));
do{
com = displaymenu();
switch(com){
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5: printf("Have a nice day\n");
}
}while(com != 5);
//needed for all basic programs to run for the professor
system("pause");
return(0);
}
int displaymenu(){
int choice;
printf("*-----------------------*\n");
printf("| MATH TUTOR |\n");
printf("*-----------------------*\n");
printf("* 1. Addition *\n");
printf("* 2. Subtration *\n");
printf("* 3. Multiplication *\n");
printf("* 4. Division *\n");
printf("* 5. EXIT *\n");
printf("*-----------------------*\n");
printf("Menu Choice: ");
scanf("%d",&choice);
return choice;
}
void addition(){
int a,b, ans;
a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;
printf("What is\n");
printf(" %d\n",a);
printf(" + %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);
if((a+b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a+b);
}
}
void subtraction(){
int a,b, ans;
a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;
printf("What is\n");
printf(" %d\n",a);
printf(" - %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);
if((a-b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a-b);
}
}
void multiplication(){
int a,b, ans;
a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;
printf("What is\n");
printf(" %d\n",a);
printf(" x %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);
if((a*b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a*b);
}
}
void division(){
int a,b;
int ans, uans;
int remain, uremain;
a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;
ans = a / b;
remain = a % b;
printf("What is\n");
printf(" %d\n",a);
printf(" ÷ %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&uans);
if(remain > 0){
printf("\nRemainder = ");
scanf("%d",&uremain);
}
if(ans==uans && remain==uremain){
printf("\n\nCorrect!.......\n");
printf("\n\nThe answer was %d with a remainder of %d\n",ans, remain);
}
else{
printf("\n\nIncorrect!......\n");
printf("\n\nThe answer was %d with a remainder of %d\n\n\n\n",ans, remain);
}
}
You could generate your division problems the same way you do multiplication ones, but use the product and one of the numbers for the problem, the other number being the solution.
That is, generate a and b as you do in multiplication, then ask what is a*b divided by a (so that b is the answer).
#include<stdio.h>
#include<conio.h>
void inversion(void);
void ways(void);
void prime(void);
void power(void);
void fibonacci(void);
void main(void)
{
char choice;
printf("Enter a choice:\n 1.Perform inversion of digits \n 2.calculate all the ways that a positive number can be get by adding \n 3.Calculate prime numbers in a range\n 4.Calculate power of a number. \n 5.generate a particular numebr of fibonacci");
choice==getche();
switch(choice)
{
case 1: inversion();
break;
case 2: ways();
break;
case 3: prime();
break;
case 4:power();
break;
case 5:fibonacci();
break;
}//switch ends
//printf("Do you want to perform this once more? (Y?N):");
//}
//while (choice=='y'||choice=='Y');
getch();
}
void inversion(void)
{
clrscr();
int num,i,x,y;
printf("Enter a 4 digit number:");
scanf("%d",&num);
for(i=1;i<=4;i++)
{
x=num%10;
y=num/10;
printf("%d",x);
num=y;
}// for ends
getch();
}
void ways(void)
{ clrscr();
int num,i,j;
printf("Enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i+j==num)
printf("%d+%d=%d",i,j,num);
getch();
}
void prime(void)
{
int num1,num2,i;
printf("Enter a range separated by space:");
scanf("%d %d",num1,num2);
for(i=2;i<num2;i++)
{ if(num1%i!=0)//is prime
printf("%d",num1);
}//for ends
getch();
}//prime ends
void power(void)
{
int num,index,i,result=1;
printf("Enter the number and its index (eg.2^3)");
scanf("%d %d",&num,index);
for(i=1;i<=index;i++)
result=num*result;
printf("%d",result);
}//power function ends
void fibonacci(void)
{
int num,x=1,y=1,i,z;
printf("Enter the term you want to find in fibonacci series:");
scanf("%d",&num);
if (num==1)
printf("it is the 1st & 2nd term");
for(i=1;i<=num+2;i++)
{
z=x+y;
if (i==num)
printf("%d",z);
x=y;
y=z;
}
printf("%d");
}//fibonacci func ends
There are a lot of problems in your code:
# I would like to suggest you to use getchar() instead of getche() for a character input.
# Now you are using a charcter for numeric options which you could have avoided by defining your choice variable as integer:
int choice;
scanf (%d, &choice);
# also if you were using char choice;
you are having cases as:
case 1: inversion();
break;
while your choice variable was char
so you should have case like:
case '1': inversion();
anyways if you will define choice as int then your code should work fine.
or you can change your cases also with case values in single quotes.