Practice C language often

        Practice it from time to time to prevent your programming from being rusty.

Table of contents

1. Calculate the sum of odd numbers in a number

2. Simulate signal transmission, send string A, receive string B, and judge whether the two signals are consistent. If they are inconsistent, output the wrong character


1. Calculate the sum of odd numbers in a number

Example:

Input: 123456

Output: 9

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sum = 0;
    int num = 0;
    printf("请输入一个数字:");
    scanf("%d", &num);
    while (num != 0)
    {
        int sub = num % 10; //得到个位的数字,顺序为个位向万位
        if (sub % 2 == 1)    //奇数
            sum += sub;   //和相加 
        num = num / 10;   //把该位置数字去掉
    }
    printf("奇数和是:%d", sum);

    return 0;
}

2. Simulate signal transmission, send string A, receive string B, and judge whether the two signals are consistent. If they are inconsistent, output the wrong character

Example:

Input: abcd abc

output: d

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char a[256] = "";
	char b[256] = "";
	
	scanf("%s %s",a,b);
	
	int ret = strcmp(a,b);

	if(ret == 0)
		printf("两个信号一致\n");
	else if(ret > 0)
	{
		//printf("a > b\n");
		printf("两个信号不一致\n");
		for (int i = 0; i < strlen(a); i++)
		{
			if (a[i] != b[i])
			{
				printf("错误的字符为:%c\n",a[i]);
			}
		}
	}
	else
		printf("数据错误\n");
	
	return 0; 
 }

Hope this article can help you.

If you have any errors, questions or infringements, please leave a message to contact the author. 

bang bang cha

Guess you like

Origin blog.csdn.net/qq_51399582/article/details/126234271