Easy Tutorial
❮ C Examples Printf Helloworld C Pointer To Pointer ❯

C Exercise Example 89

C Language Classic 100 Examples

Title: A company uses a public telephone to transmit data, which is a four-digit integer. The data is encrypted during transmission. The encryption rules are as follows: each digit is added by 5, then replaced by the remainder of the sum divided by 10, and finally, the first and fourth digits are swapped, and the second and third digits are swapped.

Program Analysis: None.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include <stdio.h>

int main()
{
    int a, i, aa[4], t;
    printf("Please enter a four-digit number: ");
    scanf("%d", &a);
    aa[0] = a % 10;
    aa[1] = a % 100 / 10;
    aa[2] = a % 1000 / 100;
    aa[3] = a / 1000;
    for (i = 0; i <= 3; i++)
    {
        aa[i] += 5;
        aa[i] %= 10;
    }
    for (i = 0; i <= 3 / 2; i++)
    {
        t = aa[i];
        aa[i] = aa[3 - i];
        aa[3 - i] = t;
    }
    printf("Encrypted number: ");
    for (i = 3; i >= 0; i--)
        printf("%d", aa[i]);
    printf("\n");
}

The above example output results are:

Please enter a four-digit number: 1234
Encrypted number: 9876

C Language Classic 100 Examples

❮ C Examples Printf Helloworld C Pointer To Pointer ❯