C language-3 basic programming questions

C language-3 basic programming questions

1. Function: Input a positive integer, and output all its prime factors in ascending order (repetitions should also be listed) (for example, the prime factor of 180 is 2 2 3 3 5) (the prime factor is all the dividends that can be whole Prime number)

#include<stdio.h>
int main(){
    
    
	long num,i;
	scanf("%d",&num);
	for (i =2; i <=num;){
    
    
		if (num%i!=0){
    
    
			i++;
		}
		else{
    
    
			printf("%d ",i);
			num = num / i;
		}
	}
	printf("\n");
	return 0;
}

2. Write a program that accepts a positive floating point value and outputs the approximate integer value of the value. If the value after the decimal point is greater than or equal to 5, it is rounded up; if it is less than 5, it is rounded down.

#include<stdio.h>
int main(){
    
    
	float x;
	scanf("%f",&x);
	if ((int)(x+0.5)>x){
    
    
		printf("%d\n", (int)(x + 0.5));
	}
	else {
    
    
		printf("%d\n",(int)x);
	}
	return 0;
}

3. Enter an int type integer and follow the reading order from right to left, and return a new integer without repeated numbers.
Example:
input
9876673 and
output
37689

#include<stdio.h>
int main(){
    
    
	int number;
	scanf("%d",&number);
	int num[10],i=0;
	while (number){
    
    
		num[i] = number % 10;
		number /= 10;
		i++;
	}
	for (int j = 0; j <i-1;++j){
    
    
		for (int k = j+1; k < i;++k){
    
    
			if (num[j]==num[k]){
    
    
				num[k] =10;
			}
		}
	}
	for (int k = 0; k < i; ++k){
    
    
		if (num[k]!=10){
    
    
			printf("%d", num[k]);
		}
	}
	printf("\n");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/109473919