How can i run a command only one time inside of for loop?
I couldn't find how to do it so i wrote code like that. But the problem on this code you can see when the program goes to else command it doesn't work.
I just wanted to see odd and even numbers with using for loop but I don't want to get result like;
Even numbers :12
Even numbers :28
Even numbers :46
I just want to see only one time print Even numbers and then print numbers like;
Even numbers:
12
28
46
etc.
I hope I could explain clearly.
My alternative but wrong code is:
#include <stdio.h>
int main()
{
int num [] = {12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20};
printf("even numbers:");
printf("\t\t\t\t Odd numbers:");
for (int i = 0; i < 11; i++) {
if (num[i] % 2 == 0) {
printf("\n%d", num[i]);
}
else {
printf("\t\t\n%d");
}
}
return 0;
}
You want two loops:
#include <stdio.h>
int main(void)
{
int num[] = {12,14,16,33,65,98,45,25,87,18,20};
size_t n = sizeof num / sizeof *num;
printf ("Even numbers:\t\t\t\tOdd numbers:\n");
for (size_t i = 0; i < n; i++) {
if (num[i] % 2 == 0) {
printf ("%d ", num[i]);
}
}
printf("\r\t\t\t\t\t");
for (size_t i = 0; i < n; i++) {
if (num[i] % 2 != 0) {
printf ("%d ", num[i]);
}
}
printf("\n");
return 0;
}
Output:
Even numbers: Odd numbers:
12 14 16 98 18 20 33 65 45 25 87
Notice the \r
to go to the beginning of the line
Here a different format:
#include <stdio.h>
int main(void) {
unsigned num []={12,14,16,33,65,98,45,25,87,18,20};
printf("even numbers"
"\todd numbers\n");
const char *prefix[] = {"", "\t\t"};
for(unsigned i = 0; i < sizeof(num) / sizeof(*num); i++) {
printf("%s%u\n", prefix[num[i] % 2], num[i]);
}
}
and the output is:
even numbers odd numbers
12
14
16
33
65
98
45
25
87
18
20
EDIT: OP has been edited to show desired output. I had add '\n' to two lines of this to effect the change.
"Factoring out" common processing into a function is always a good idea.
#include <stdio.h>
void show( int num[], int nItems, char *title, int rem ) {
printf( "%s\n", title );
for( int i = 0; i < nItems; i++ )
if( num[i]%2 == rem )
printf( "%d\n", num[ i ] );
printf( "\n" );
}
int main () {
int num [] = { 12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20 };
show( num, sizeof num/sizeof num[0], "even numbers: ", 0 );
show( num, sizeof num/sizeof num[0], "odd numbers: ", 1 );
return 0;
}
Output:
even numbers:
12
14
16
98
18
20
odd numbers:
33
65
45
25
87
Based on the assumption that the least changes to your shown code, with explanation, are most helpful, here is my explained solution:
#include <stdio.h>
int main()
{
int num [] = {12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20};
printf("even numbers:");
printf("\tOdd numbers:\n"); // newline after the output, one tab, no blanks
for (int i = 0; i < 11; i++) {
if (num[i] % 2 == 0) {
printf("%d\n", num[i]); // newline after output
}
else {
printf("\t\t%d\n", num[i]); // tabs, then output, then newline
}
}
return 0;
}
this gets you an output of:
even numbers: Odd numbers:
12
14
16
33
65
98
45
25
87
18
20
Your problem was only caused by missapplied whitespaces.
(Apart from accidentally dropping the parameter for the odd output....)
Doing newlines after output is a good practice, but that is a matter of taste.
Important is to not output tabulators followed by a newline; because the newline spoils the effect of the tabulators.
Here is the version with newlines before output (for really minimal changes), but I recommend against it.
#include <stdio.h>
int main()
{
int num [] = {12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20};
printf("even numbers:");
printf("\t\t\t\t Odd numbers:");
for (int i = 0; i < 11; i++) {
if (num[i] % 2 == 0) {
printf("\n%d", num[i]);
}
else {
printf("\n\t\t\t\t\t %d", num[i]); // newline, tabs, blank, output
}
}
return 0;
}
This gets you an output of:
even numbers: Odd numbers:
12
14
16
33
65
98
45
25
87
18
20
Wider, because I left the multiple tabulators and the unneeded blank in.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.