Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i need help with this question

0 votes
asked Nov 14, 2018 by anonymous
write a program that will check how many numbers from 1 to N(inclusive) satisfy the condition that the sum of their digits be equal to 17

2 Answers

0 votes
answered Nov 16, 2018 by Nebojsa
#include <stdio.h>

int main()
{
int a,sum,i,j,n;
do{
printf("n must be >=89 \nn=") ;    //because 89 is the smallest number that satisfy the condition...
scanf("%d",&n);
}
while(n<89);

    for(i=89;i<n;i++)        
    {
        j=i;
    while(j)
        {
          sum+=j%10;
          j/=10;
        }
    if(sum==17)
       a++;
    sum=0;
    }
printf("There is %d numbers that satisfy the condition.",a);

    return 0;
}
0 votes
answered Nov 18, 2018 by Nelson (180 points)
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int sumDigits(int n)
{
    if (n < 10)
    {
        return n;
    }

    return (n % 10) + sumDigits(n / 10);
}

int main()
{
    int n;
    cout << "N = ";
    cin >> n;
    
    int count = 0;
    for (int index = 1; index <= n; index++)
    {
        if (sumDigits(index) == 17)
        {
            cout << index << endl;
            count++;
        }
    }
    cout << "Count: " << count;

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...