Pages

Thursday, 8 November 2012

WAP to enter a natural number and Display all the possible combination of consecutive numbers

  • WAP to enter a natural number and Display all the possible combination of consecutive numbers which add up the give the sum equal to the original number.
INPUT: 15
OUTPUT: 1 ,2 ,3, ,4 ,5.
               4 , 5, 6.
#include <stdio.h>
void main()
{
    int i,j,k,no,sum;
    printf("Enter a number\n");
    scanf("%d",&no);
    for(i=1;i<no/2;i++)
    {
        for(j=1;j<no;j++)
        {
            sum=0;
            for(k=i;k<=j;k++)
            sum=sum+k;
            if(sum==no)
            {
                for(k=i;k<=j;k++)
                printf("%d ,",k);
                printf("\n");
            }
        }
    }
}

No comments:

Post a Comment