I was supposed to make a program that would be a calculator using functions to provide results. I got it down and everything is working but I'm having a slight issue with the printing function out of everything which is humorous; for some reason that I can't seem to get my head around whenever the program prints a solution it also prints the first selection which is add(num1, num2) and I can't for the life of me figure out why. Any input would be greatly appreciated! Thank you! Here is my code:
//Function to Add
int add (int a, int b);
//Function to Substract
int sub (int a, int b);
//Function to Multiply
int mul (int a, int b);
//Function to Divide
int div (int a, int b);
//Function to get remainder
int rem (int a, int b);
//Function to print menu
int printmenu();
//Begin main
int main()
{
//Initialize Variables
int num1=0;
int num2=0;
unsigned int selection =0;
//Loop
while ((selection=printmenu()) != 6)
{
switch (selection)
{
//Addition Case
case 1:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d added to %d is %d",num1,num2,add(num1,num2));
break;
case 2:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d substracted to %d is %d",num1,num2,sub(num1,num2));
break;
case 3:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d multiplied to %d is %d",num1,num2,mul(num1,num2));
break;
case 4:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d divided to %d is %d",num1,num2,div(num1,num2));
break;
case 5:
printf("Enter two numbers: ");
scanf("%d %d", &num1 , &num2 );
printf("%d divided to %d has a remainder of %d",num1,num2,rem(num1,num2));
break;
case 6:
return 0;
default:
printf("That is not a valid choice.");
break;
}//End switch
}//End while
return 0;
}//End main
//Function to add
int add (int a, int b)
{
int total=0;
total = a + b;
return total;
}
//Function to substract
int sub (int a, int b)
{
int total=0;
total = a - b;
return total;
}
//Function to multiply
int mul (int a, int b)
{
int total=0;
total = a * b;
return total;
}
//Function to Divide
int div (int a, int b)
{
int total=0;
total = a / b;
return total;
}
//Function to get Remainder
int rem (int a, int b)
{
int total=0;
total = a % b;
return total;
}
//Function to Print Menu
int printmenu()
{
int selection=0;
//Print menu
printf("1.Add\n");
printf("2.Substract\n");
printf("3.Multiply\n");
printf("4.Divide\n");
printf("5.Remainder\n");
printf("6.Exit\n");
printf("Selection: ");
scanf("%d", &selection);
return selection;
}
When you print the result of the operation, you aren't including a newline at the end of the output. So when you print the menu, the first line of the menu gets printed on the same.
Add \n at the end of each result printing statement, ex.:
printf("%d added to %d is %d\n",num1,num2,add(num1,num2));
Related
I am writing a C program to insert element into an array with pointers. In my INSERT method I can't seem to add the element into the array. when I select option 4, it shows 0 number in the array.
the test case I use is as follows :
run the program
select option 1
Enter an array size
enter the element to add to the array.
select option 4 to display the array
// Online C compiler to run C program online
#include <stdio.h>
#define MAX 10
void initialize (int *size, int ar[]);
void insert (int max, int *size, int ar[], int num);
void iremove (int *size, int ar[], int num);
void display (int size, int ar[]);
int main ()
{
int option = 0;
int num, ar[MAX], size = 0;
printf ("Please select an option: \n");
printf ("(1) Initialize the array \n");
printf ("(2) Insert an integer \n");
printf ("(3) Remove an integer \n");
printf ("(4) Display the numbers stored in the array \n");
printf ("(5) Quit \n");
do
{
printf ("Enter your choice: \n");
scanf ("%d", &option);
switch (option)
{
case 1:
initialize (&size, ar);
break;
case 2:
printf ("Enter an integer: \n");
scanf ("%d", &num);
insert (MAX, &size, ar, num);
break;
case 3:
break;
case 4:
display(size,ar);
break;
default:
break;
}
}
while (option < 5);
return 0;
}
void display (int size, int ar[])
{
int i;
printf ("The %d numbers in the array: \n", size);
for (i = 0; i < size; i++)
printf ("%d ", ar[i]);
printf ("\n");
}
void initialize (int *size, int ar[])
{
int total, i, num;
printf ("Enter the total number of integers (MAX=%d): \n", MAX);
scanf ("%d", &total);
(*size) = 0;
printf ("Enter the integers: \n");
for (i = 0; i < total; i++)
{
scanf ("%d", &num);
insert (MAX, size, ar, num);
}
}
void insert (int max, int *size, int ar[], int num)
{
if(*size>=MAX)
{
printf("Array full")
}
else
{
ar[*size] = num;
}
}
void iremove (int *size, int ar[], int num)
{
/* Write your code here */
}
You forgot to increment the size :
void insert (int max, int *size, int ar[], int num)
{
if(*size>=MAX)
{
printf("Array full")
}
else
{
ar[*size] = num;
(*size)++;
}
}
I would suggest avoiding side effects in the code when possible. Use return values.
Also, try to use the correct types as well. For sizes use size_t defined in many headers *for example stdio.h or stdlib.h)
Your functions should be called append as you do not insert but append the value to the end of the array.
size_t append(size_t max, size_t size, int *array, int num)
{
if(size < max)
{
array[size++] = num
}
else
{
printf("Array full");
}
return size;
}
and the call
size = insert(MAX, size, ar, num);
I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}
I'm starting to learn the c programming language and I have to write a program that subtracts two greatest numbers out of the three entered numbers. Can anyone help me with this?
Edit: Sorry, I still don't know how this site functions...
I know how to find the greatest number, but I'm not sure how to find the other one.
#include<stdio.h>
int main()
{
int a, b, c, d, max;
printf("Enter three numbers: ");
scanf("%d%d%d%d", &a, &b, &c);
max=a;
if (max<b) {max=b;}
if (max<c) {max=c;}
printf("the greatest number is %d\n", max);
return 0;
}
Don't expect to get answers to questions like yours. Try this. The assumption is that you are dealing with positive integers only. If you need to consider negative integers as well, you can do it yourself. You should note that this is the not the best solution, there can be much more elegant ones.
#include <stdio.h>
int main() {
int numbers[3];
printf("Enter number 1: \n");
scanf("%d", &numbers[0]);
printf("Enter number 2; \n");
scanf("%d", &numbers[1]);
printf("Enter number 3: \n");
scanf("%d", &numbers[2]);
printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);
int maximum_0 = 0;
int maximum_1 = 0;
int i;
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_0) {
maximum_0 = numbers[i];
}
}
for (i = 0; i < 3; i++) {
if (numbers[i] > maximum_1 && numbers[i] < maximum_0) {
maximum_1 = numbers[i];
}
}
printf("Result: %d\n", (maximum_0 - maximum_1));
return 0;
}
//Here it is since you are beginner Without loops
#include<stdio.h>
int main()
{
int a, b, c, d, max,min,result;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b)
{
if(a>c)
{
max=a;
if(c>b)
min=c;
else
min=b;
}
else
{
max=c;
a=min;
}
}
else
{
if(b>c)
{
max=b;
if(a>c)
min=a;
else
min=c;
}
else
{
max=c;
min=b;
}
}
result=max-min;
printf("the greatest number is %d\n", result);
return 0;
}
Use an array and then use qsort to sort your values. The code below will do this for you. The order of the array will be such that the first element will have your smallest number and the last element your largest.
#include<stdio.h>
void sort(const void* d1, const void* d2)
{
int a = *(int*)d1;
int b = *(int*)d2;
if (a > b)return 1;
if (b > a)return -1;
return 0;
}
int main()
{
int abc[3];
printf("Enter three numbers: ");
scanf("%d%d%d", &abc[0], &abc[1], &abc[2]);
qsort(abc, 3, sizeof(int), sort);
printf("%d\n", abc[2] - abc[1]);
while (1){}
return 0;
}
Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}
I am learning function in C. I want to sum multiple integers using arguments in function. I managed to write a code for adding two integers, but how if I want to add multiple integers and print the total of them? please guide me. Code which i wrote is;
#include<stdio.h>
#include<conio.h>
int sum(int a, int b, int c);
int main (void){
int x,y,z;
clrscr();
printf("Enter first integer to add.\n");
scanf("%d",&x);
printf("Enter second integer to add.\n");
scanf("%d",&y);
sum(x, y, z);
printf("Total = %d.\n",sum(x, y, z));
getch();
return 0;
}
int sum (int a, int b, int c){
c=a+b;
return c;
}
You can do something like this.
sum = 0;
while (ch == "y")
{
scanf("%d", &a);
sum+=a;
printf("Do you want to continue: ");
scanf("%c\n", &ch);
}
printf("%d", sum);
The idea is to have a variable sum whose initial value is 0.
Have a while loop that takes a integer a as input & add it to sum.
You can mantain a variable ch, which can be used to exit out of the loop. Only if the user enters "y", the user will be asked for integer again.
try this !
int main()
{
int var[100];
int count = 5;
printf("enter number %d number ", count);
for( int i = 0; i < count; i++ )
{
scanf( "%d", &var[i] );
}
printf("sum=%d", sum(var, count) );
return 0;
}
int sum( int var[], int count )
{
int sum = 0;
for( int i = 0; i < count; i++ )
{
sum += var[i];
}
return sum;
}
Currently you're overwriting the third argument to the function with the sum of the first two and return it. This should probably change a bit.
Just think about how you'd write a sum of three numbers in mathematics and you should see the solution.