Easy Tutorial
❮ C Unions C Examples Printf Double ❯

C Exercise Example 83

C Language Classic 100 Examples

Title: Calculate the number of odd numbers that can be formed with digits 0-7.

Program Analysis:

This problem is essentially a permutation and combination problem. Let's denote the number as sun=a1a2a3a4a5a6a7a8, where a1-a8 represent the digits of the number. If the last digit of a number is odd, then the number is odd, regardless of the preceding digits. If the last digit is even, then the number is even.

a1-a8 can take any of the digits 0-7, with the first digit not being 0.

We start counting the number of odd numbers from one-digit numbers to eight-digit numbers:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015年 tutorialpro.org. All rights reserved.
//

#include<stdio.h>
int main(int agrc, char*agrv[])
{
    long sum = 4, s = 4;//sum's initial value is 4, indicating the number of odd numbers with only one digit is 4.
    int j;
    for (j = 2; j <= 8; j++)
    {    
        printf("%d-digit odd number count: %ld\n", j-1, s);
        if (j <= 2)
            s *= 7;
        else
            s *= 8;
        sum += s;    
    }
    printf("%d-digit odd number count: %ld\n", j-1, s);
    printf("Total count of odd numbers: %ld\n", sum);
    // system("pause");
    return 0;
}

The above example outputs the following results when run:

1-digit odd number count: 4
2-digit odd number count: 28
3-digit odd number count: 224
4-digit odd number count: 1792
5-digit odd number count: 14336
6-digit odd number count: 114688
7-digit odd number count: 917504
8-digit odd number count: 7340032
Total count of odd numbers: 8388608

C Language Classic 100 Examples

❮ C Unions C Examples Printf Double ❯