C language digital programming problems

Posted by davinci on Sat, 05 Feb 2022 05:52:42 +0100

catalogue

Three digit composition

Narcissistic number

Fibonacci sequence

Prime number

Isomorphic number

Greatest common divisor

Completion

Three digit composition

Title: write a program to output all three digits that meet the following requirements

Condition: it consists of four arrays: 1, 2, 3 and 4

A three digit number that is different from each other and does not have the same repeating number.

Idea: the four numbers are made into hundreds and ten bits respectively (three circular statements nest and traverse each number (if there is 0 in the given number, 0 cannot be made into hundreds)), and the numbers on the three bits cannot be the same (the selection statement limits the output to the case that the three numbers are different).

#include<stdio.h>
int main()
{
	int i, j, k;
	for (i = 1; i <= 4; i++)
		for (j = 1; j <= 4; j++)
			for (k = 1; k <= 4; k++)
				if (i != j && i != k && j != k)
					printf("%d%d%d ", i, j, k);//Directly output three numbers in parallel, and pay attention to the space after them to make each three digits separated
	return 0;
}

Narcissistic number

Title: find the number of all daffodils within 1000 and output it.

Narcissus number: a special three digit number in which the sum of the cubes of each digit is equal to the number itself. For example: 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3

Idea: this number takes the remainder of 10 to get a digit, divides by 10 to get a hundred and ten digits, then takes the remainder of 10 to get ten digits, and divides by 100 to get a hundred digits. Finally, judge whether the cube sum of each bit is equal to the original number.

#include<stdio.h>
int main()
{
	int i,a,b,c;
	for (i = 100; i <1000; i++)//The number of daffodils is a special three digit number, so judge directly from 100
	{
		a = i % 10;// Extract single digit
		b = i / 10 % 10;//Extract ten digits
		c = i / 100;//Extract hundreds
		if (a * a * a + b * b * b + c * c * c == i)//The sum of each digit cube is equal to the number of daffodils
			printf("%d It's daffodils\n", i);
	}
	return 0;
}

Fibonacci sequence

Title: output the first 20 items of Fibonacci series

Idea: the sum of the first two numbers is equal to the third number, and then exchange numbers

#include<stdio.h>
int main()
{
    int i = 1, j = 1, t;
    printf("%d %d ", i, j);
    for (int n = 0; n < 20; n++)
    {
        t = j;
        j = i + j;
        i = t;
        printf("%d ", j);
    }
    return 0;
}

Prime number

Title: find prime numbers within 1000 and output them

Idea: prime is a number that can only be divided by one and itself. According to this property, it traverses the range from 1 to the number. If it can be divided by integer, it means that the number is not prime.

#include<stdio.h>
int main()
{
    int x,i;
    scanf_s("%d",&x);
    for (i = 2; i < x; i++)//Go out 1 and the number itself
        if (x % i == 0)
            break;
    if (i == x)//If it is a prime number, the above loop statement will traverse each number and jump out of the loop when i=x
        printf("It's a prime");
    else
        printf("Not prime");
    return 0;
}

 

Isomorphic number

Title: find all isomorphic numbers within 1 ~ 10000

Idea: use the remainder operator to separate the last 1 to 4 bits of the squared number and compare it with the original number. If it is the same, it is an isomorphic number.

#include<stdio.h>
int main()
{
    int i,j;
    for (i = 1; i < 10000; i++)
    {
        j = i * i;
        if (j % 10 == i||j%100==i||j%1000==i||j%10000==i)
            printf("%d %d\n",i,j);
    }
    return 0;
}

Greatest common divisor

Title: input two numbers, find their maximum common divisor and minimum common multiple and output them.

Train of thought; According to the method in mathematics, two numbers take the remainder of the small number at the same time. When the remainder of two numbers is 0, it is the maximum common divisor. The least common multiple is the product of two numbers divided by the greatest common divisor.

#include<stdio.h>
int main()
{
    int m, n,t,i,gys;
    scanf_s("%d%d", &m, &n);
    if (m < n)//Judge the size of two numbers so that m is a large number
    {
        t = m;
        m = n;
        n = t;
    }
    for (i = n; i > 1; i--)//The traversal starts from n and is judged by the remainder of m and n respectively
        if (m % i == 0 && n % i == 0)
        {
            gys = i;
            break;//Find the largest and jump out of the loop
        }
    printf("The maximum common divisor is%d", gys);
    printf("The least common multiple is%d", m * n / gys);
    return 0;
}

Completion

Title: find all completions within 1 to 1000 and output them.

Idea: perfect number, all its true factors (i.e. those other than itself) Divisor )The sum of is exactly equal to itself. For example: 6 factors are 1, 2, 3 and 6. After removing itself, 1 + 2 + 3 = 6

#include<stdio.h>
int main()
{
    int i, j, s;
    for (i = 2; i <= 1000; i++)
    {
        s = 0;
        for (j = 1; j < i; j++)//If the judgment condition is less than i, the factor of the number itself is removed
        {
            if (i % j == 0)//Find all the factors of i and add them
                s += j;
        }
        if (s == i)//Judge whether the sum of factors is the same as the original number
            printf("%d It's perfect\n", i);
    }
    return 0;
}

Topics: C