Sunday, 20 May 2012

Write a program to print the sum of all odd number between 1 to 10 using do while loop.


 //program to print the sum of all odd numbers between 1 to 10 using do while loop
void main()
{
int i=1,sum=0;
clrscr();
do
{
sum=sum+i;
i+=2;
}while(i<=10);
printf("the sum of all odd numbers between 1 to 10 is %d",sum);
getch();
}
Output
        the sum of all odd numbers between 1 to 10 is 25

2 comments:

  1. How would you list the odd numbers in a while loop?

    ReplyDelete
    Replies
    1. In the following way
      main()
      {
      int i=1;
      while(i<=10)
      {
      printf("%d\t",i);
      i+=2;
      }
      }

      Delete