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.

A difficult problem for a begginner.

0 votes
asked Jan 14, 2018 by anonymous 1 flag
The exercise says find all the four digit numbers out of whom the first digit equals to the sum of other 3.

Can anyone explain what this program does please?

#include <stdio.h>
int main() {
  int i, n, sum, first_digit, digit;
  i = 1000;
    while (i <= 9999) {
    first_digit = i / 1000;
    n = i % 1000;
    sum = 0;
    while (n > 0) {
    digit = n % 10;
    sum += digit;
    n /= 10;
        }
    if (sum == first_digit) printf("%d\t", i);
    i++;
    }
  return 0;
}

2 Answers

0 votes
answered Feb 14, 2018 by Attila

//I have made own code for your project, it was easier to make notes on my work. All you can see in my code are in your as well, you can compare them. Good luck. :)

//For C language


#include <stdio.h>

int main()

{

    int number=1000;        //this is the smallest 4 digit number

    int thousands;          //declarate our variables [ X _ _ _ ]

    int hundreds;           //declarate our variables [ _ X _ _ ]

    int decimals;           //declarate our variables [ _ _ X _ ]

    int firstdigit;         //declarate our variables [ _ _ _ X ] (First digit is the lowest number)

    int sum3digit;          //declarate our variables (we should summarise the 3 digits except first digit)

    while(number<10000)     //start a loop to go throught numbers from the declared 1000 to 9999 (not number<=10000, so it is not examined)
    {

        thousands=number/1000;          //find out the last digit [ X _ _ _ ] (it is INTIGER so for example 8542 devided by 1000 is 8 and not 8,542)

        hundreds=(number%1000)/100;     //find out this digit [ _ X _ _ ] (using modulo, for example 8542 modulo 1000 is 542 because 8542/1000 and we get the rest 542; 542/100=5 like above)

        decimals=(number%100)/10;       //find out this digit [ _ _ X _ ] (using modulo, for example 8542 modulo 100 is 42 because 8542/100 and we get the rest 42; 42/10=4 like above)

        firstdigit=(number%10);         //find out firstdigit [ _ _ _ X ] (using modulo, for example 8542 modulo 10 is 2 because 8542/10 and we get the rest 2)

       
       
        sum3digit=thousands+hundreds+decimals;  //here we summarise the 3 digits except first digit


       
if(firstdigit==sum3digit)       //make a criterion, check if the firstdigit and other 3 digits summ are the same? IF YES step into { }, IF NO jump over { }

        {

            printf("%d\n",number);      //print the number to the terminal/output/screen when run the program ( "%d" means decimal/intiger "\n" means cursor go to next line )

        }

       
       
    number++;   //here we increment number to go throught 1000 to 9999 (one by one, so 1000 1001 1002 1003 etc 9997 9998 9999)

    }


return 0;   //have to use at the end of the source code, inside the [  main() {  }  ]

}

0 votes
answered Feb 14, 2018 by rs jangid
1001    1010    1100    2002    2011    2020    2101    2110    2200    3003    3012    3021    3030    3102    3111
3120    3201    3210    3300    4004    4013    4022    4031    4040    4103    4112    4121    4130    4202    4211
4220    4301    4310    4400    5005    5014    5023    5032    5041    5050    5104    5113    5122    5131    5140
5203    5212    5221    5230    5302    5311    5320    5401    5410    5500    6006    6015    6024    6033    6042
6051    6060    6105    6114    6123    6132    6141    6150    6204    6213    6222    6231    6240    6303    6312
6321    6330    6402    6411    6420    6501    6510    6600    7007    7016    7025    7034    7043    7052    7061
7070    7106    7115    7124    7133    7142    7151    7160    7205    7214    7223    7232    7241    7250    7304
7313    7322    7331    7340    7403    7412    7421    7430    7502    7511    7520    7601    7610    7700    8008
8017    8026    8035    8044    8053    8062    8071    8080    8107    8116    8125    8134    8143    8152    8161
8170    8206    8215    8224    8233    8242    8251    8260    8305    8314    8323    8332    8341    8350    8404
8413    8422    8431    8440    8503    8512    8521    8530    8602    8611    8620    8701    8710    8800    9009
9018    9027    9036    9045    9054    9063    9072    9081    9090    9108    9117    9126    9135    9144    9153
9162    9171    9180    9207    9216    9225    9234    9243    9252    9261    9270    9306    9315    9324    9333
9342    9351    9360    9405    9414    9423    9432    9441    9450    9504    9513    9522    9531    9540    9603
9612    9621    9630    9702    9711    9720    9801    9810    9900

this value is print
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.
...