Question:
One question about C language programming?
anonymous
1970-01-01 00:00:00 UTC
One question about C language programming?
Thirteen answers:
?
2016-04-09 06:34:41 UTC
No. Learn C, the C++. You can transition to C# easily.
Ciaron
2009-07-24 15:56:39 UTC
There are several ways the for loop is the most common with you just incrementing by two and printing the numbers that are odd as all odd numbers are two apart, still there are other ways to do loops in C.

The While loop

int i =1; /* this sets the number to 1 */

while ( i <= 49) /* If the number is less than or equal to 49 the loop will continue */

{ /* begining of loop */

printf("%d ",i); /* this prints the number the first is the number 1*/

i+=2; /* Now add 2 to the number */

/*the loop will go back to the start */

} /* end of loop */



the next one is the do while loop not so often used but still a valid loop



int i =1; /* the variable i is initialised to 1*/

do /* Start of loop */

{

printf("%d ",i);

i=i+2; /* This increments by two

or you can use

i+=2; they are the same

*/

} while ( i <= 49)

/* this checks the value if it is less than 49 it goes back to a loop */



Then there is the recursive - this is when you create a function that calls it's self a bit cleverer version



int count_odd(int x); /* predeclaration of function */



int main()

{

int x = 1;

int number;

number =count_odd(x);

printf(

return 0;

}

/* this is the function which returns a value */

int count_odd(int x)

{

/* this sets the limit to 49 so if the value is equal to 49 it doesn't call it's self*/

if (x == 49 )

return 0;

else

{

/* prints the value of x */

printf( "%d " , x);

/* the function now calls on it's self but adds two the value */

return count_odd(x + 2);

}
ssd
2009-07-24 07:23:18 UTC
include



void main()

{



for(int i= 1; i<=49; i++)

{

if((i%2)==1)

{

printf("%d",i);

}

}

}
Princess Psycho
2009-07-24 06:05:47 UTC
It's really quite a simple thing to do, just use a loop

for (int i=1; i>=49; i+=2) /* increment by two to get just odd numbers

{

printf("%d : " i); /* this will print only odd numbers

}
anonymous
2009-07-24 03:57:31 UTC
There's a much simpler way to do this, than some of the other answers have provided.



#include



int main()

{



  for( int i = 0; i < 49; i++)

  {

    printf( "%d\n", ++i);

  }



  return 0;



}



And conversely, to produce even numbers, you'd just change the increment operator from pre-fix to post-fix.

(++i) becomes (i++)
avikmullick
2009-07-24 03:28:00 UTC
include

int main(void)

{

int i;

printf("0");

for(i= 1; i<=49; i++)

if((i%2)!=0)

printf("%d",i);

return 0;

}
neeraj
2009-07-24 03:22:52 UTC
include

#include

#include

main( )

{ int i;

printf(" odd numbers are");

for(i=0;i<=49;i++)

{ if(i%2==1)

printf("%d,i);

}

getch( );

}
anonymous
2009-07-24 03:17:51 UTC
include



int main()

{

for(int i=1;i<=49;i++)

{

if(i % 2==1) //condition for Odd numbers

{

cout<<"Number "<
}

}

return 0;

}
buGGedDown
2009-07-24 03:16:05 UTC
here's the code:



#include



int main(void)

{



for(int i= 1; i<=49; i++){

if((i%2)==1){

printf("%d",i);



}

}

return 0;

}



--% is modulo division which returns the remainder of the division operation
RockStar
2009-07-24 05:25:45 UTC
include



int main(void)

{

int p;

for(p = 1; p <= 49; p += 2)

printf("%d ", p);

return 0;

}



cool your home work is completed
anonymous
2009-07-24 03:39:06 UTC
Funny... That's not a question, that's a homework assignment. An EZ1,2, and yet everyone so far messed it up:



1) Greg and Sharan apparently don't know the difference between odd and even.

2) Hassan doesn't know the difference between C and C++.

3) Jukebox Hero's solution isn't simpler than mine, and what's worse, it's non-standard C (you're not supposed to declare the loop variable inside the for-statement).

4) all the other people posted sub-optimal / platform-specific solutions.



I'm assuming that 'from 0 to 49', you mean up to and including 49. Then the solution is:



#include



int main(void)

{

int i;

for(i = 1; i <= 49; i += 2)

printf("%d ", i);

return 0;

}



## EDIT ## RockStar is semi-smart. He's smart because he has the right answer, but it's only semi-smart because it's for everyone to see that his answer is a copy of mine (credit for the fact that he changed name of the variable from i to p), and I posted my code 1.5 hours before he did.



Seeing that I gave the right answer already, at least an hour before anyone after me, everyone after me are what we call in Dutch 'spuit 11'.
sharan
2009-07-24 03:17:56 UTC
initiliaze a variable with 0

print the variable

increment the variable by 2



continue these step until variable reaches 50..tats it
Greg
2009-07-24 03:16:33 UTC
int i;

for(i=0;i<50;i+=2)

{

printf("%d ",i);

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...