C language daily practice - day 87: limited 5 digits

C language daily practice
April 6, 2022

Topic description

How many five-digit numbers are there with a single digit of 6 that is divisible by 3?

problem analysis

[The most stupid method] The range of five digits is 10000~ 99999, find all the numbers within this range that satisfy the single digit of 6 and are divisible by 3.

Code


#include <stdio.h>

int main()
{
    
    
    int i = 0, cnt = 0;
    for(i = 10000; i <= 99999; i++)
    {
    
    
        if(i % 3 == 0 && i % 10 == 6)
        {
    
    
            printf("%-6d", i);
            cnt++;
            if(cnt % 5 == 0)
                printf("\n");
        }
    }
    printf("个位数为6且能被3整除的五位数共有%d个\n", cnt);
    return 0;
}

operation result

insert image description here

online reference

Original link: https://www.cnblogs.com/chaolong/archive/2013/05/18/3086240.html

insert image description here

/**
 * @file   018c.c
 * @author Chaolong Zhang <[email protected]>
 * @date   Sat May 18 21:41:48 2013
 *
 * @brief  个位数是6且能被3整除的五位数一共有多少个
 *
 *
 */
#include <stdio.h>

int main(int argc, char *argv[])
{
    
    
	int count=0;
	int n1,n2,n3,n4;
	for (int i = 1000; i <= 9999; ++i)
 	{
    
    
 		n1=i%10;
		n2=(i/10)%10;
		n3=(i/100)%10;
		n4=(i/1000);
		if (0== (n1+n2+n3+n4)%3 ) count++;
	}
	printf ("the total number is %d\n", count);
	return 0;
} 

extended question

Find how many integers between 100 and 1000 whose numbers add up to 5.

#include <stdio.h>

int main()
{
    
    
    int i = 0, cnt = 0;
    for(i = 100; i < 1000; i++) //1000不考虑,只考虑三位数
    {
    
    
        if(i / 100 % 10 + i / 10 % 10 + i % 10 == 5)
        {
    
    
            printf("%-4d", i);
            cnt++;
            if(cnt % 5 == 0)
                printf("\n");
        }
    }
    printf("100到1000之间有%d个其各位数字之和为5的整数。\n", cnt);
    return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43772810/article/details/123982568