100 examples of C language topic exercises - topic + topic analysis + source code (71-80)

[Title 71]

Title: write a function. When the input n is an even number, call the function to find 1 / 2 + 1 / 4 +... + 1/n. when the input n is an odd number, call the function 1 / 1 + 1 / 3 +... + 1/n (using pointer function).
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"
double evenumber(int n);	//Declare the called function 
double oddnumber(int n);

int main()
{
	int n;
	double r;
	double (*pfunc)(int);
	printf("Please enter a number: ");
	scanf("%d",&n);
	if(n%2 == 0)		//Judging odd and even numbers 
	pfunc=evenumber;
	else
	pfunc=oddnumber;
	
	r=(*pfunc)(n);
	printf("%lf \n",r);
	
	system("pause");
	return 0;
}
double evenumber(int n)		//When n is even 
{
	double s=0,a=0;
	int i;
	for(i=2;i<=n;i+=2)
	{
		a=(double)1/i;
		s+=a;
	}
	return s;
}
double oddnumber(int n)		//When n is odd 
{
	double s=0,a=0;
	int i;
	for(i=1;i<=n;i+=2)
	{
		a=(double)1/i;
		s+=a;
	}
	return s;
}

[Title 72]

Title: string sorting.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "string.h"
void swap(char *str1,char *str2);

int main()
{
	char str1[20],str2[20],str3[20];
	printf("Please enter 3 strings,Each string ends with a carriage return: \n");
	gets(str1);
	gets(str2);
	gets(str3);
	if(strcmp(str1,str2) > 0)	swap(str1,str2);
	if(strcmp(str2,str3) > 0)	swap(str2,str3);
	if(strcmp(str1,str2) > 0) 	swap(str1,str2);
	printf("The sorted result is: \n");
	printf("%s\n%s\n%s\n",str1,str2,str3);
	return 0;
}
void swap(char *str1,char *str2)
{
	char tem[20];
	strcpy(tem,str1);
	strcpy(str1,str2);
	strcpy(str2,tem);
}

[Title 73]

Title: there is a pile of peaches on the beach, which are divided by five monkeys. The first monkey divided the pile of peaches into five parts, one more. The monkey threw the one more into the sea and took one. The second monkey divided the remaining peaches into five parts on average, and one more. It also threw one more into the sea and took one part. The third, fourth and fifth monkeys did so. How many peaches were there on the beach?
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int x,i=0,j=1;
	while(i<5)
	{
		x=4*j;
		for(i=0;i<5;i++)
		{
			if(x%4 != 0)	break;
			x=(x/4)*5+1;
		}
		j++;
	}
	printf("%d \n",x);
	return 0;
}

[Title 74]

Title: 809 *= 800*??+ 9*??+ 1 where?? Represents two digits, 8 *?? The result is double digits, 9 *?? The result is 3 digits. Please?? Represents two digits, and 809 *?? Results after.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int i;
	for(i=10;i<100;i++)
	{
		if(8*i<100 && 9*i>99 && 9*i<1000)
		{
			printf("??The two digits represented are:%d \n",i);break;
		}
		printf("809*%d==800*%d+9*%d+1 \n",i,i,i);
	}
	return 0;
}

[Title 75]

Title: conversion from octal to decimal
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int n=0,i=0;
	char s[20];
	printf("Please enter an octal number: \n");
	gets(s);
	while(s[i] != '\0')
	{
		n=n*8+s[i]-'0';
		i++;
	}
	printf("The octal number just entered is converted to decimal:\n %d \n",n);
	return 0;
}

[Title 76]

Title: find the odd number that can be composed of 0-7.
1. Topic analysis: use 1, 3, 5 and 7 as a bit, and 0 cannot be the highest bit.
2. The source code of the title is as follows:

#include "stdio.h"
int factorial(int a,int b);

int main()
{
	int sum=0,i;
	sum+=4;		//One digit cannot be processed as follows. There are 4 odd numbers in one digit
	for(i=2;i<8;i++)
	{
		sum+=4*(factorial(7,i-1)-factorial(6,i-2));
	} 
	printf("Can form%d Odd number\n",sum);
	return 0;
}
int factorial(int a,int b)
{
	int i,sum=1;
	if(b<=0)return 1;
	for(i=0;i<b;i++)
	{
		sum*=(a-i);
	}
	return sum;
}

[Title 77]

Title: an even number can always be expressed as the sum of two prime numbers.
1. Topic analysis:
Title 2. Source code:

#include "stdio.h"
#include "math.h"
int Isprimer(unsigned int n);		//Declare the called function 

int main()
{
	unsigned int n,i;
	do{
		printf("Please enter an even number: \n");
		scanf("%d",&n);
	}while(n%2!=0);
	for(i=1;i<n;i++)
	{
		if(Isprimer(i)&&Isprimer(n-i))	break;
	}
	printf("even numbers%d Can be decomposed into%d and%d Sum of two prime numbers \n",n,i,n-i);
	return 0;
}
int Isprimer(unsigned int n)
{
	int i;
	if(n<4)
	return 1;
	else if(n%2==0)
	return 0;
	else
	for(i=3;i<sqrt(n)+1;i++)
	{
		if(n%i==0)
		return 0;
	}
}

[Title 78]

Title: two string connector.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
char* strconnect(char *str1,char *str2);
 
int main()
{
    char str1[20],str2[20];
    char *str;
    puts("Please enter two strings,Separate with carriage return:");
    scanf("%s%s", str1,str2);
    str=strconnect(str1,str2);
    puts("The connected string is:");
    puts(str);
    return 0;
}
char* strconnect(char *str1,char *str2)
{
    char*str;
    str=(char*)malloc(strlen(str1)+strlen(str2)+1);
    str[0]='\0';
    strcat(str,str1);
    strcat(str,str2);
    return str;
}

[Title 79]

Topic: judge how many 9 can divide a prime number.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int p,i;
	long int sum=9;
	printf("Please enter a prime number: \n");
	scanf("%d",&p);
	for(i=1;;i++)
	{
		if(sum%p==0)break;
		else
		sum=sum*10+9;
	}
	printf("prime number%d Divisible%d Number of 9%ld \n",p,i,sum);
	return 0;
}

[Title 80]

Title: read the integer value of 7 numbers (1-50). For each value read, the program prints out the "number" of the value.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int n,i,j;
	printf("please enter a number: \n");
	i--;
	for(i=0;i<7;i++)
	{
		scanf("%d",&n);
		if(n>50)
		{
			printf("Please re-enter: \n");
			i--;
		}
		else
		{
			for(j=0;j<n;j++)
			printf("*");
		}
		printf("\n");
	}
	return 0;
}

Keywords: C C++ Algorithm

Added by djKale on Tue, 15 Feb 2022 16:25:40 +0200