Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to get all combination of 6 digit numbers that start with 2 or 9 include 3 of 7 and 1 of 4 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int leftmost_digit(int n);
 
int main(void)
{
    int n, i, x1, x2, count1, count2, digit1, digit2;

    srand(time(NULL));
    
    for (i = 100000; i <= 999999; i++)
    {
        if (leftmost_digit(i) == 2 || leftmost_digit(i) == 9)
        {
            x1 = 3;
            x2 = 1;
            digit1 = 7;
            digit2 = 4;
            count1 = count2 = 0;
            n = i;
            while (n > 0)
            {
                if (n % 10 == digit1)
                    count1++;                
                if (n % 10 == digit2)
                    count2++;
                n = n / 10;
            }
            if (count1 == x1 && count2 == x2)
                printf("%d\n", i);
            
        }
    }
    
    return 0;
}
int leftmost_digit(int n)
{
    while (n > 10)
        n = n / 10;
 
    return n;
}

/*
run:

247177
247277
247377
247577
247677
247707
247717
247727
247737
247757
247767
247770
247771
247772
247773
247775
247776
247778
247779
247787
247797
247877
247977
248777
249777
254777
257477
257747
257774
264777
267477
267747
267774
270477
270747
270774
271477
271747
271774
272477
272747
272774
273477
273747
273774
274077
274177
274277
274377
274577
274677
274707
274717
274727
274737
274757
274767
274770
274771
274772
274773
274775
274776
274778
274779
274787
274797
274877
274977
275477
275747
275774
276477
276747
276774
277047
277074
277147
277174
277247
277274
277347
277374
277407
277417
277427
277437
277457
277467
277470
277471
277472
277473
277475
277476
277478
277479
277487
277497
277547
277574
277647
277674
277704
277714
277724
277734
277740
277741
277742
277743
277745
277746
277748
277749
277754
277764
277784
277794
277847
277874
277947
277974
278477
278747
278774
279477
279747
279774
284777
287477
287747
287774
294777
297477
297747
297774
904777
907477
907747
907774
914777
917477
917747
917774
924777
927477
927747
927774
934777
937477
937747
937774
940777
941777
942777
943777
945777
946777
947077
947177
947277
947377
947577
947677
947707
947717
947727
947737
947757
947767
947770
947771
947772
947773
947775
947776
947778
947779
947787
947797
947877
947977
948777
949777
954777
957477
957747
957774
964777
967477
967747
967774
970477
970747
970774
971477
971747
971774
972477
972747
972774
973477
973747
973774
974077
974177
974277
974377
974577
974677
974707
974717
974727
974737
974757
974767
974770
974771
974772
974773
974775
974776
974778
974779
974787
974797
974877
974977
975477
975747
975774
976477
976747
976774
977047
977074
977147
977174
977247
977274
977347
977374
977407
977417
977427
977437
977457
977467
977470
977471
977472
977473
977475
977476
977478
977479
977487
977497
977547
977574
977647
977674
977704
977714
977724
977734
977740
977741
977742
977743
977745
977746
977748
977749
977754
977764
977784
977794
977847
977874
977947
977974
978477
978747
978774
979477
979747
979774
984777
987477
987747
987774
994777
997477
997747
997774

*/


answered Oct 24, 2014 by avibootz

Related questions

1 answer 146 views
2 answers 264 views
2 answers 201 views
1 answer 178 views
2 answers 214 views
2 answers 200 views
...