CS50
Ask user for their age in years, print how many days & weeks they are.
#include <cs50.h> #include <stdio.h> int main(void){ int age = get_int("What's your age\n"); int days = age * 365; printf("You are atleast %i days old.\n", days); int weeks = days /7; printf("You are atleast %i weeks old.\n", weeks); int months = age *12; printf("You are atleast %i months old.\n", months); }
Take input from user and print it.
#include <cs50.h> #include <stdio.h> int main(void){ string fname = get_string("First name?\n"); printf("Hello, %s\n",fname); }
Take two input from user and print it together.
#include <cs50.h> #include <stdio.h> int main(void){ string fname = get_string("First name?\n"); string lname = get_string("Last name?\n"); printf("Hello, %s %s\n", fname,lname); }
Abstraction 3 - Create a algorithm that only takes positive value and prints it
#include <cs50.h> #include <stdio.h> int get_positive_int(void); int main(void){ int i = get_positive_int(); printf("%i\n", i); } int get_positive_int(void){ int n; do{ n = get_int("Positive Integer: "); } while(n<1); return n; }
Using Flaot, Ask for price and add tax to it and print it.
#include <cs50.h> #include <stdio.h> int main(void){ float price = get_flot("What's the pric?\n"); printf("Your total is %f.\n", price * 1.0625); }
Ask user to agree or disagree using upper & lowercase Y & N
#include <cs50.h> #include <stdio.h> int main(void){ char c = get_char("Do you agree\n"); if(c == 'Y' || c == 'y'){ printf("Agree.\n"); }else if(c == 'N' || c == 'n'){ printf("Not Agreed.\n"); } }
Using "For Loop" Print "Cough" 3 time
#include <cs50.h> #include <stdio.h> int main(void){ for (int i = 0; i <3; i++){ printf("cough\n"); } }
Aks user for a number. Check if it is odd or even
#include <cs50.h> #include <stdio.h> int main(void){ int n = get_int("n: "); if (n % 2 == 0){ printf("Even\n"); }else{ printf("Odd\n"); } }
Based on user input print "?" in a row
#include <cs50.h> #include <stdio.h> int main(void){ int n; do{ n = get_int("Width: "); } while (n < 1); for (int i = 0; i < n; i++){ printf("?"); }printf("\n"); }
Ask for two int number, print out which one is bigger.
#include <cs50.h> #include <stdio.h> int main(void){ int x = get_int("x: "); int y = get_int("y: "); if(x>y){ printf("X is bigger then Y"); }else if(y>x){ printf("Y is bigger than X"); }else{ printf("X is equal to Y"); } }
Abstraction 2 - Using "For Loop" Print "Cough" 3 times with a Custom Function that takes input from the user
#include <cs50.h> #include <stdio.h> void cough(int n); int main(void){ cough(3); } void cough(int n){ for (int i = 0; i < n; i++){ printf("cough\n"); } }
Abstraction 1 - Using "For Loop" Print "Cough" 3 times with Custom Function
#include <cs50.h> #include <stdio.h> void cough(void); int main(void){ for (int i = 0; i <3; i++){ cough(); } } void cough(void){ printf("cough\n"); }
Based on user input print "?" in a Grid
#include <cs50.h> #include <stdio.h> int main(void){ int n; do{ n = get_int("Sizze: "); }while (n < 1); for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ printf("#"); } printf("\n"); } }
Say "Hello World"
#include <stdio.h> int main(void){ printf("Hello, World"); }
Create a algorithm that case Overflow
#include <unistd.h> #include <stdio.h> int main(void){ for (int i = 1; ; i*= 2){ printf("%i\n", i); sleep(1); } }
What are the placeholders for the following data type - char, double, float, int, long, string
char - %c, double - %f, int - %i, long - %li, string - %s, float - %f
Using "For" Loop print "Hello World" 25 time.
int main(void){ for (int i =0; i<25; i++){ printf("Hello ma dude\n"); } }
Using "While" Loop print "Hello World" 20 time.
int main(void){ int i = 0; while (i < 20){ printf("Hello, World\n"); i++; } }