my teacher told us to "make a program that will print the first 10 consecutive numbers starting from 1 using the while and do" but i don't know how to do, i hope you can help me guys, thanks

my teacher told us to "make a program that will print the first 10 consecutive numbers starting from 1 using the while and do" but i don't know how to do, i hope you can help me guys, thanks

simple as that make first a int.
given
a design program

printf...


ohhh i got to go..

sorry ..next tym i will..

int  main()
{
          intialize an int variable with the inital value either zero or one
           while( variable < maxvalue)
            {
                  //maxvalue is 10 in your case
                print the variable value;
                increment the variable value;
            }
return 0;
}

thats all, finish.

Logic:
What we want is 10 consecutive number....
:)int main()

Let us start with 4 and print next 10 number
what we want here is an integer that can store first number.
:)int n;

We also require and integer that checks that the loop is executed 10 times. we start this integer from zero.
;)int i= 0;

here we could have used 'n' also but we will go for more simpler one...


Now we come to what we want:
=> do while is a statement that is executed atleast once even though the condition may be false..

:)
printf("Please enter a number: ");
scanf("%d",&n)
do
{
printf("%d",n);
n++; //n = n+1;
i++; //i = i+1;
}while(i<10);
getch();
return 0;
}

thats all simple and esay...

Edited 7 Years Ago by puranchandra248: n/a

printing till 10 starting from some n.( 0< n <=10 )

using while :

int main()
{
int num;
printf("enter number (0 <  num <= 10)\n");
scanf("%d",&num);
while(num>0 && num <=10)
       printf("%d ",num++);
if(!num)
   printf("enter num greater than zero\n");
return 0;
}

OUTPUT:

if num =5
5 6 7 8 9 10
if num=1
2 3 4 5 6 7 8 9 10


using do while:

void print_one_to_ten( void) {
 int num;   
printf("enter number (0 <  num <= 10)\n");
scanf("%d",&num);
if(num)
{
     do
      {
          printf("%d ",num);
          num++;
       }while(num<=10);
}
else
{
    printf("enter number greatre than zero\n");
}
}

OUTPUT:

if num =5
5 6 7 8 9 10
if num=1
2 3 4 5 6 7 8 9 10
if num =0
enter number greatre than zero.

pogram is self explanatory.


oops....! mistakenly used c code......., no problem just replace printf with cout and scanf with cin
and add headers iostream
and ofcourse namsespace std.

This article has been dead for over six months. Start a new discussion instead.