English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can any one Write a program to display odd numbers from 1 to 10? Please need help.
U can write after this:
#include
#include
main()
{
/*your answers thank you......*/

2007-04-26 05:24:00 · 10 answers · asked by mac_pc 2 in Computers & Internet Programming & Design

10 answers

main()
{
int i;
for(i=0;i<10;i++)
{
if(i%2==1)
printf("\n %d ",i);
}
}

2007-04-26 06:12:45 · answer #1 · answered by Bhagavathi D 2 · 0 1

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); }

2016-05-19 03:33:12 · answer #2 · answered by ? 3 · 0 0

I'm not going to write your program for you but I will give you a hint. Odd numbers are numbers that are not divisible by 2


so if

mod 2 != 0 then you have an odd number

just use a for loop to loop from 1 to 10 and your done.

2007-04-26 05:29:43 · answer #3 · answered by sinkablehail1978 5 · 1 0

Use a loop and check the remainder of each number when divided by 2. So something like if(iloop%2 != 0) then print iloop else continue.

2007-04-26 05:34:44 · answer #4 · answered by Chris 4 · 0 0

do a for from 1 to 10

for($i=0; $i<11; $++)
{
do an i modulus 2
if modulus of 2 != 0
print i
}

you can figure out the C language of it

2007-04-26 05:29:55 · answer #5 · answered by Duds331 5 · 1 0

for (int a = 1; a<10; a+=2)
cout << a;

pretty simple...

2007-04-27 02:17:55 · answer #6 · answered by justme 7 · 0 0

int i = 1;
while( i < 10){
printf("\n%d", i);
i += 2;
}
}
I have no idea why people are wasting time using modulus, it's easier to use i += 2.

2007-04-26 07:59:41 · answer #7 · answered by Anonymous · 0 0

printf++ does it. Hint: set your integer=1 and then printf++ while int < 10.

2007-04-26 05:29:41 · answer #8 · answered by Jim B 2 · 0 1

#include
const max = 500;

void main()
{
clrscr();
for (int i =0 ; i< max; i++)
if(i%2 != 0)
cout<
}

2007-04-26 05:52:15 · answer #9 · answered by locottus 1 · 0 0

printf ("odd nos");

please be more specific

use sentence below and please try to read your c++ book

if ((number>=1)&&(number%2>0))

2007-04-26 05:30:04 · answer #10 · answered by Anonymous · 0 0

fedest.com, questions and answers