# Thread: Arithmetic Operations and formatting help!

1. ## Arithmetic Operations and formatting help!

I am supposed to write a code for my very first assignment where when the user enters four integers the program would perform several operations on it.

The problem I am having is that the product and ratio are incorrect.

Also, I cannot figure out how to get the remainder to appear.

Formatting issues I have is I do not know how to make a colon appear after the words. For example,

The colon doesn't show up when it runs.

I also cannot get the strings to skip a line like,

Please enter four integers: 1 2 3 4

Sum is: 10

Code:
```/*-------------
Include Section
--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

/*------------------Main Functions------------------
Purpose: Allow user to input four different integers to have the programme give multiple outputs of different arithmatic operations executed on these integers.
Returns: Sum, product, average, smallest, second-smallest, ration between smallest and second-smallest.
--------------------------------------------------*/

int
main(void)
{
int num1;
int num2;
int num3;
int num4;
int sum;
int product;
int avg;
float avgrem;
int small;
int small2;
int ratio;
float ratiorem;

scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

sum = num1 + num2 + num3 + num4;
product = num1 * num2 * num3 * num4;
avg = product / 4.0;
avgrem = product % 4.0;
small = num1; //To assign smallest number to int small
if (small > num2) small = num2;
if (small > num3) small = num3;
if (small > num4) small = num4;

small2 = INT_MAX; /*To assign second smallest number to small2. Assume small2 has the max value of type int*/
if (small == num1) {
if (small2 > num2) small2 = num2;
if (small2 > num3) small2 = num3;
if (small2 > num4) small2 = num4;
}

if (small == num2) {
if (small2 > num1) small2 = num1;
if (small2 > num3) small2 = num3;
if (small2 > num4) small2 = num4;
}

if (small == num3) {
if (small2 > num1) small2 = num1;
if (small2 > num2) small2 = num2;
if (small2 > num4) small2 = num4;
}

if (small == num4) {
if (small2 > num1) small2 = num1;
if (small2 > num2) small2 = num2;
if (small2 > num3) small2 = num3;
}

ratio = small2 / small;
ratiorem = small2 % small;

printf("Sum is %d\n", sum);
printf(" \n");
printf("Product is %d\n", product);
printf("Average is %d%f\n", avg, avgrem);
printf("Second Smallest is %d\n", small2);
printf("Smallest is %d\n", small);
printf("Ratio between Second Smallest and Smallest is %d%f\n", ratio, rationrem);
printf("**** Program Terminated ****");

return (0);
}

```

2. Ok... not bad for a first assignment...

avg is defined as an int... int's do not have remainders, only whole numbers.
acgrem is a float... the % operator only works on ints.

The colon is not showing up because there's no colon in your printf() format strings.

3. Originally Posted by CommonTater
Ok... not bad for a first assignment...

avg is defined as an int... int's do not have remainders, only whole numbers.
acgrem is a float... the % operator only works on ints.

The colon is not showing up because there's no colon in your printf() format strings.

So if I define avg as a float does that fix it, and if its defined as a float do I still need the remainder?

I put colons there and they do not show up regardless.

Would the fact that I'm compiling in Code::Blocks mean anything?

New Code:

Code:
```/*-------------
Include Section
--------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

/*------------------Main Functions------------------
Purpose: Allow user to input four different integers to have the programme give multiple outputs of different arithmatic operations executed on these integers.
Returns: Sum, product, average, smallest, second-smallest, ration between smallest and second-smallest.
--------------------------------------------------*/

int
main(void)
{
int num1;
int num2;
int num3;
int num4;
int sum;
int product;
int avg;
float avgrem;
int small;
int small2;
int ratio;
float ratiorem;

scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

sum = num1 + num2 + num3 + num4;
product = num1 * num2 * num3 * num4;
avg = product / 4.0;
avgrem = product % 4.0;
small = num1; //To assign smallest number to int small
if (small > num2) small = num2;
if (small > num3) small = num3;
if (small > num4) small = num4;

small2 = INT_MAX; /*To assign second smallest number to small2. Assume small2 has the max value of type int*/
if (small == num1) {
if (small2 > num2) small2 = num2;
if (small2 > num3) small2 = num3;
if (small2 > num4) small2 = num4;
}

if (small == num2) {
if (small2 > num1) small2 = num1;
if (small2 > num3) small2 = num3;
if (small2 > num4) small2 = num4;
}

if (small == num3) {
if (small2 > num1) small2 = num1;
if (small2 > num2) small2 = num2;
if (small2 > num4) small2 = num4;
}

if (small == num4) {
if (small2 > num1) small2 = num1;
if (small2 > num2) small2 = num2;
if (small2 > num3) small2 = num3;
}

ratio = small2 / small;
ratiorem = small2 % small;

printf("Sum is %d\n", sum);
printf(" \n");
printf("Product is %d\n", product);
printf("Average is %d%f\n", avg, avgrem);
printf("Second Smallest is %d\n", small2);
printf("Smallest is %d\n", small);
printf("Ratio between Second Smallest and Smallest is %d%f\n", ratio, rationrem);
printf("**** Program Terminated ****");

return (0);
}```

4. Take a look at this and apply it to the rest of your program:
Code:
```#include <stdio.h>

int main(void){

int num1,num2,num3,num4,sum, product; //note same type can be define on same line
float average; //average as float so you have decimal value

printf("Enter 4 numbers: ");
scanf("%d %d %d %d",&num1,&num2,&num3,&num4);

sum = num1+num2+num3+num4;
product = num1*num2*num3*num4;
/*note the divisor, the 'f' stands for float*/
average = sum / 4.0f;

/*Note in the printf statements there are ':' at the end*/
printf("Sum: %d\n",sum);
printf("Average: %f\n",average);
printf("Product: %d\n",product);

return(0);
}```