Please help with a newbie c program
Can someone help me with my 1st C program, ive done as much as i can... here's what the task is:
"Write a description that accepts as input one five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. The program should also print, on separate lines, the smallest and largest digit of the integer. Here is a sample input and output of the program:
Input a five-digit integer: 12345
The separated digits are:
1 2 3 4 5
The smallest digit is 1
The largest digit is 5
***** Program Terminated ***** "
Ok so thats what I gotta do... this is what I have so far, can you tell me where I'm going wrong? I think it has something to do with the stuff i have at the top. I dont know tho, im new to C...
#include stdio.h
#include stdlib.h
int
main( )
{
int num1; //first number taken from user
int num2; //second number taken from user
int num3; //third number taken from user
int num4; //fourth number taken from user
int num5; //fifth number taken from user
printf("Input a five-digit integer: ");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
printf("The separated digits are:\n");
printf("%d %d %d %d %d\n", num1, num2, num3, num4, num5);
if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5) {
printf("The smallest digit is %d\n", num1);
}
if (num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5) {
printf("The smallest digit is %d\n", num2);
}
if (num3 < num2 && num3 < num1 && num3 < num4 && num3 < num5) {
printf("The smallest digit is %d\n", num3);
}
if (num4 < num2 && num4 < num3 && num4 < num1 && num4 < num5) {
printf("The smallest digit is %d\n", num4);
}
if (num5 < num2 && num5 < num3 && num5 < num4 && num5 < num1) {
printf("The smallest digit is %d\n", num5);
}
if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5) {
printf("The largest digit is %d\n", num1);
}
if (num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5) {
printf("The largest digit is %d\n", num2);
}
if (num3 > num2 && num3 > num1 && num3 > num4 && num3 > num5) {
printf("The largest digit is %d\n", num3);
}
if (num4 > num2 && num4 > num3 && num4 > num1 && num4 > num5) {
printf("The largest digit is %d\n", num4);
}
if (num5 > num2 && num5 > num3 && num5 > num4 && num5 > num1) {
printf("The largest digit is %d\n", num5);
}
return 0;
}
Let me know what you guys think, i really appreciate ANY help
and "priyajeet", if youre reading this, can you explain what you suggested to me? Like i said im a beginner so what u did baffled me lol
"Write a description that accepts as input one five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. The program should also print, on separate lines, the smallest and largest digit of the integer. Here is a sample input and output of the program:
Input a five-digit integer: 12345
The separated digits are:
1 2 3 4 5
The smallest digit is 1
The largest digit is 5
***** Program Terminated ***** "
Ok so thats what I gotta do... this is what I have so far, can you tell me where I'm going wrong? I think it has something to do with the stuff i have at the top. I dont know tho, im new to C...
#include stdio.h
#include stdlib.h
int
main( )
{
int num1; //first number taken from user
int num2; //second number taken from user
int num3; //third number taken from user
int num4; //fourth number taken from user
int num5; //fifth number taken from user
printf("Input a five-digit integer: ");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
printf("The separated digits are:\n");
printf("%d %d %d %d %d\n", num1, num2, num3, num4, num5);
if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5) {
printf("The smallest digit is %d\n", num1);
}
if (num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5) {
printf("The smallest digit is %d\n", num2);
}
if (num3 < num2 && num3 < num1 && num3 < num4 && num3 < num5) {
printf("The smallest digit is %d\n", num3);
}
if (num4 < num2 && num4 < num3 && num4 < num1 && num4 < num5) {
printf("The smallest digit is %d\n", num4);
}
if (num5 < num2 && num5 < num3 && num5 < num4 && num5 < num1) {
printf("The smallest digit is %d\n", num5);
}
if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5) {
printf("The largest digit is %d\n", num1);
}
if (num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5) {
printf("The largest digit is %d\n", num2);
}
if (num3 > num2 && num3 > num1 && num3 > num4 && num3 > num5) {
printf("The largest digit is %d\n", num3);
}
if (num4 > num2 && num4 > num3 && num4 > num1 && num4 > num5) {
printf("The largest digit is %d\n", num4);
}
if (num5 > num2 && num5 > num3 && num5 > num4 && num5 > num1) {
printf("The largest digit is %d\n", num5);
}
return 0;
}
Let me know what you guys think, i really appreciate ANY help
and "priyajeet", if youre reading this, can you explain what you suggested to me? Like i said im a beginner so what u did baffled me lol
0
Comments
Like,
#include "stdio.h"
oh yeah, i have that, i just forgot to type it in the thread lol
anyone else have any suggestions for me to make this program work?
Can you use 'else if' in C?
And yeah you might need stiod.h. I know C alone doesnt have any way to print to a screen something has to be included like iostream in C++.
yeah i already have
#include stdio.h
#include stdlib.h
in it, i just forgot to write it up there, ill edit it in a sec.
and i think the bottom parts are right, but something is up with the 1st part, i dont know what. When i try running it, it does the "Input a five-digit integer: #####" thing right, but after that nothing happens...