im new to C and trying to write a program where it shows numOne + numTwo = total using just if, else if and else statements.
Any numbers that are between zero and ten should show as words (negative numbers between negative ten and zero also appear as words) All other numbers appear as decimal integers.
e.g. integers 10 and 5 should read: ten + five = 15
Repeating my code for numOne would give me almost 300 lines, surely there is a more efficient method of utilising these if and else statements? ty
the code i have so far is :
// edited code
#include <stdio.h>
int main(void) {
int numOne;
int numTwo;
printf("Please enter two integers:");
scanf("%d %d", &numOne, &numTwo);
if (numOne >= -10 && numOne < 0) {
printf("negative");
}
if (numOne == 0) {
printf("zero + ");
}
if (numOne == 1) {
printf("one + ");
}
if (numOne == 2) {
printf("two + ");
}
if (numOne == 3) {
printf("three + ");
}
if (numOne == 4) {
printf("four + ");
}
if (numOne == 5) {
printf("five + ");
}
if (numOne == 6) {
printf("six + ");
}
if (numOne == 7) {
printf("seven + ");
}
if (numOne == 8) {
printf("eight + ");
}
if (numOne == 9) {
printf("nine + ");
}
if (numOne == 10) {
printf("ten + ");
}
if (numTwo >= -10 && numTwo < 0){
printf("negative");
}
if (numTwo == 0) {
printf("zero");
}
if (numTwo == 1) {
printf("one");
}
if (numTwo == 2) {
printf("two");
}
if (numTwo == 3) {
printf("three");
}
if (numTwo == 4) {
printf("four");
}
if (numTwo == 5) {
printf("five");
}
if (numTwo == 6) {
printf("six");
}
if (numTwo == 7) {
printf("seven");
}
if (numTwo == 8) {
printf("eight");
}
if (numTwo == 9) {
printf("nine");
}
if (numTwo == 10) {
printf("ten");
}
int total = numOne + numTwo;
if (total >= -10 && total < 0) {
printf("negative");
}
if (total == 0) {
printf("zero");
}
if (total == 1) {
printf("one");
}
if (total == 2) {
printf("two");
}
if (total == 3) {
printf("three");
}
if (total == 4) {
printf("four");
}
if (total == 5) {
printf("five");
}
if (total == 6) {
printf("six");
}
if (total == 7) {
printf("seven");
}
if (total == 8) {
printf("eight");
}
if (total == 9) {
printf("nine");
}
if (total == 10) {
printf("ten");
}
else if (numOne > 10 || numOne < -10) {
printf("%d + ", numOne);
} else if (numTwo > 10 || numTwo < -10) {
printf("%d = ", numTwo);
} else if (total > 10 || total < -10) {
printf("%d", total);
}
return 0;
}
old:
int main(void) {
int numOne;
int numTwo;
int total = numOne + numTwo;
printf("Please enter two integers:");
scanf("%d %d", numOne, numTwo);
// repeat all this code for numTwo and total
if (numOne = -1) {
printf("negative one + ");
}
if (numOne = -2); {
printf("negative two + ");
}
if (numOne = -3) {
printf("negative three + ");
}
if (numOne = -4); {
printf("negative four + ");
}
if (numOne = -5) {
printf("negative five + ");
}
if (numOne = -6); {
printf("negative six + ");
}
if (numOne = -7) {
printf("negative seven + ");
}
if (numOne = -8); {
printf("negative eight + ");
}
if (numOne = -9) {
printf("negative nine + ");
}
if (numOne = -10); {
printf("negative ten + ");
}
if (numOne = 0) {
printf("zero + ");
}
if (numOne = 1) {
printf("one + ");
}
if (numOne = 2); {
printf("two + ");
}
if (numOne = 3) {
printf("three + ");
}
if (numOne = 4); {
printf("four + ");
}
if (numOne = 5) {
printf("five + ");
}
if (numOne = 6); {
printf("six + ");
}
if (numOne = 7) {
printf("seven + ");
}
if (numOne = 8); {
printf("eight + ");
}
if (numOne = 9) {
printf("nine + ");
}
if (numOne = 10); {
printf("ten + ");
}
else if (numOne > 10 || < -10) {
printf("%d + ", numOne);
} else if (numTwo > 10 || < -10) {
printf("%d = ", numTwo);
} else if (total > 10 || < -10) {
printf("%d", total);
}
return 0;
} ```
Aucun commentaire:
Enregistrer un commentaire