Traditional Culture Encyclopedia - Traditional stories - Determine whether a number n is divisible by both 3 and 5. The algorithm for the problem is represented in a traditional flowchart
Determine whether a number n is divisible by both 3 and 5. The algorithm for the problem is represented in a traditional flowchart
Numbers divisible by 5 with a final digit of 0 or 5.
==>Numbers divisible by both 3 and 5 with individual digits summing to a multiple of 3 and a final digit of 0 or 5.
Or more simply, divisible by 15.
Algorithm:
1. Determine if it's a multiple of 3
---- split the n
---. --- Add each number and divide by 3
---- remainder == 0?
- --- Yes, go to the next step; No, exit no
2. Determine whether the last digit is 0 or 5
--- --- last digit == 0 or == 5?
----Yes, exit yes; No, exit no
Or
1, to determine whether it is a multiple of 15
----n take the remainder of 15
---- remainder == 0?
----Yes, exit yes; No, exit no
Program:
#include
void
main()
{
int
i,m,k=10,s=0;
int
a[10];
printf("Input a positive integer:");
scanf("%d",&m);
a[0]=0;
for(i=1;i& lt;10;i++)
{a[i]=(m-s)%k;k*=10;s+=a[i];}
if(s%3==0)
{if(a[1]==0||a[1]==5)printf("\nm can be divisible by both 3 and 5\n");
else
printf("\nm cannot be divisible by both 3 and 5\n");
}
else
printf("\nm cannot be divisible by both 3 and 5\n");
}
- Previous article:The root cause of the outbreak of World War II
- Next article:The connotation of China's rule of law principle includes
- Related articles
- Phoenix specialties snacks list
- Advantages and disadvantages of various means of transportation?
- "Red and wet spring Luo dyed the royal robe", how did the ancient people dye the cloth to show their clothes? Comprehensive printing and dyeing technology
- Comprehensive Partner Cooperation Agreement
- Besides double skin milk, what other delicious old flavors are there in Shunde?
- Make sentences with left and right hands (about 30 or so)
- What are the repertoires of Guangxi
- What does flow modulation mean? What does it do?
- Group names with cultural connotations
- Which cities or countries have excellent cultures?