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,
Please enter four integers:
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
This is my code, please help me out!
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;
printf("Please enter four different integers:");
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);
} |