Skip to content

Instantly share code, notes, and snippets.

@shahril96
Last active October 4, 2015 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahril96/21de9029721d3c8572bd to your computer and use it in GitHub Desktop.
Save shahril96/21de9029721d3c8572bd to your computer and use it in GitHub Desktop.
Secret Code generator programming task (Universiti Malaysia Pahang)
#include <stdio.h>
int main()
{
char D[2], M[2], Y[4], name[100]; // char string
printf("Please enter your birthdate (DDMMYYYY): ");
scanf("%2s%2s%4s", D, M, Y);
printf("Please enter your name : ");
scanf("%100s", name); // 100 is used to specify maximum size of string
// first rule
char first_digit = D[1];
// second rule
// % (modulo) is used here to extract last digit from any integer (including single integer)
// example, 764, we module with 10, we will get last digit, which is 4
int second_digit = ((D[0] - '0') * (Y[2] - '0') * (Y[3] - '0')) % 10;
// third rule
int third_digit = 2; // default value
if(name[0] == 'A' || name[0] == 'U')
third_digit = 0;
else if(name[0] == 'I' || name[0] == 'E')
third_digit = 1;
printf("\nYour secret code : %c%d%d\n\n", first_digit, second_digit, third_digit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment