pset 1
hello
Simple C program to print hello
#include <stdio.h>#include <cs50.h>
int main(void){ string name = get_string("What's your name?\n"); printf("hello, %s\n", name);}mario
Pyramid building
If the user doesn’t, in fact, input a positive integer between 1 and 8, inclusive, when prompted, the program should re-prompt the user until they cooperate:
$ ./marioHeight: -1Height: 0Height: 42Height: 50Height: 4 # # ## ## ### ####### #####include <stdio.h>#include <cs50.h>
void pyramid(int height);
int main(void){ int height;
do { height = get_int("Height: "); } while (height < 1 || height > 8);
pyramid(height);}
void pyramid(int height){ int space = height - 1; int hash = 1; for (int i = 0; i < height; i++) { for (int j = 0; j < space; j++) { printf(" "); } for (int j = 0; j < hash; j++) { printf("#"); } printf(" "); for (int j = 0; j < hash; j++) { printf("#"); } space--; hash++; printf("\n"); }}credit
Validation of AMEX, MASTERCARD, and VISA credit/debit cards using Luhn’s Algorithm
#include <stdio.h>#include <cs50.h>
#define dig12 1000000000000#define dig13 10000000000000#define dig14 100000000000000#define dig15 1000000000000000
int dig = 0;
string serviceDetect(long long num);
bool fraudCheck(long long number);
int main(void){ long long num = get_long("Number: "); if (fraudCheck(num)) { printf("%s\n", serviceDetect(num)); } else { printf("INVALID\n"); }}
bool fraudCheck(long long number){ int mulsum = 0, regsum = 0; int buffer = 0; while (number > 0) { if (buffer == 0) { regsum += (number % 10); number /= 10; buffer = 1; } else { int k = 2 * (number % 10); if (k >= 10) { mulsum += (k % 10); k /= 10; mulsum += k; } else { mulsum += k; } number /= 10; buffer = 0; } dig++; }
if ((mulsum + regsum) % 10) { return false; printf("false\n"); }
return true;}
string serviceDetect(long long num){ if (dig == 13 && (num / dig12) == 4) { return "VISA"; } else if (dig == 16) { if ((num / dig15) == 4) { return "VISA"; } else if (((num / dig14) >= 51) && ((num / dig14) <= 55)) { return "MASTERCARD"; } else { return "INVALID"; } } else if (dig == 15) { if (((num / dig13) == 34) || ((num / dig13) == 37)) { return "AMEX"; } else { return "INVALID"; } } else { return "INVALID"; }}