Skip to content

Commit 8fb5599

Browse files
committed
Add Day 1 and Day 2 programs
1 parent e18f191 commit 8fb5599

14 files changed

+289
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.DS_Store
2+
*.out

Day 1/add.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Program to add two numbers
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int add(int first, int second) {
8+
return first + second;
9+
}
10+
11+
int main () {
12+
13+
int firstNumber;
14+
int secondNumber;
15+
16+
printf("Please enter the first number: ");
17+
scanf("%d", &firstNumber);
18+
19+
printf("Please enter the second number: ");
20+
scanf("%d", &secondNumber);
21+
22+
int result = add(firstNumber, secondNumber);
23+
printf("Sum of two numbers is: %d\n", result);
24+
return 0;
25+
}

Day 1/hello.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This is our first program.
3+
* It will print hello world to the screen.
4+
*/
5+
6+
#include <stdio.h>
7+
#include <math.h>
8+
9+
int main () {
10+
11+
int firstVariable = - pow(2, 31);
12+
// firstVariable = 2;
13+
14+
printf("Value of the variable is: %d\n", firstVariable);
15+
16+
// integer can store 4 bytes = 32 bits
17+
// it can store 2^32 values!!!
18+
19+
// but they also can store negative numbers
20+
// -(2^31) to (2^31 - 1)
21+
22+
// 1 byte = 8 bits
23+
// every bit = either 0 or 1
24+
// how many possible bit combinations we have
25+
// for 32 bits?
26+
27+
// 2 bits
28+
// -(2^1) to (2^1 - 1)
29+
// -1 to +1, -2, -1, 0, 1
30+
31+
// 2 bits => 00, 01, 10, 11 (4 possible combinations)
32+
// 3 bits => 000, 001, 010, 011, 100, 101, 110, 111 (8 in total)
33+
// n bits => 2^n combinations
34+
return 0;
35+
}

Day 1/variable.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Understanding variable in C
3+
*/
4+
5+
#include <stdio.h>
6+
#include <math.h>
7+
8+
int main () {
9+
10+
int variable_int = 2;
11+
char variable_char = 'a';
12+
13+
printf("Value of the int variable is: %d\n", variable_int);
14+
printf("Value of char variable is: %c\n", variable_char);
15+
return 0;
16+
}

Day 2/arithmeticOperators.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Understand the arithmetic operators in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
9+
int x, y;
10+
printf("Please enter number1: ");
11+
scanf("%d", &x);
12+
printf("Please enter number2: ");
13+
scanf("%d", &y);
14+
15+
// add two numbers
16+
int addResult = x + y;
17+
18+
// subtract two numbers
19+
int subtractResult = x - y;
20+
21+
// multiply the numbers
22+
int multiplyResult = x * y;
23+
24+
// divide the numbers
25+
int divideResult = x / y;
26+
27+
printf("Add: %d\n, Subtract: %d\n, Multiply: %d\n, Divide: %d\n", addResult, subtractResult, multiplyResult, divideResult);
28+
29+
int moduloResult = x % y;
30+
printf("Remainder of x divided by y: %d\n", moduloResult);
31+
32+
return 0;
33+
34+
}

Day 2/assignmentOperator.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Understand the assignment operators in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
9+
int x, y;
10+
printf("Please enter number1: ");
11+
scanf("%d", &x);
12+
printf("Please enter number2: ");
13+
scanf("%d", &y);
14+
15+
// add two numbers
16+
int result = x + y;
17+
// the above statement computes the result of (x + y)
18+
// and assigns that result to the variable named 'addResult'
19+
20+
// a += b is equivalent to a = a + b
21+
// a -= b is equivalent to a = a - b
22+
// a *= b is equivalent to a = a * b
23+
// a /= b is equivalent to a = a / b
24+
25+
int z;
26+
printf("Please enter third number: ");
27+
scanf("%d", &z);
28+
29+
// result = result + z;
30+
result += z;
31+
32+
printf("Final sum of three numbers: %d\n", result);
33+
34+
// unary operator: works on a single value
35+
36+
// we want to add one to the result
37+
// result = result + 1;
38+
result ++;
39+
// result = result - 1;
40+
result --;
41+
printf("Result: %d\n", result);
42+
return 0;
43+
44+
}

Day 2/endQuestion1.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Understand the Logical operators in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
9+
int x, y = 5, z = 5;
10+
11+
x = y == z;
12+
13+
printf("%d\n", x);
14+
15+
return 0;
16+
}

Day 2/float.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
float c = 5.0;
5+
printf ("Temperature in Fahrenheit is %f", (9/5.0)*c + 32);
6+
// if the operands are integers
7+
// division will perform integer division
8+
// Mathematically, 9/5 = 1.8, it will truncate the value after decimal point
9+
// In C: 9/5 = 1
10+
return 0;
11+
}

Day 2/inputOutput.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Program to understand data types in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
int x = 3;
10+
11+
printf("int: %d\n", x);
12+
13+
char ch = 'Z';
14+
printf("char: %c\n", ch);
15+
16+
double d = 32.515512515151515213131312321412412412421412412451245151241231234214521351535324123424234452352523;
17+
printf("double: %f\n", d);
18+
19+
float f = 32.515512515151515213131312321412412412421412412451245151241231234214521351535324123424234452352523;
20+
printf("float: %f\n", f);
21+
22+
printf("Printing the size of the variables:\n\n");
23+
printf("size of int: %lu\n", sizeof(x));
24+
printf("size of char: %lu\n", sizeof(ch));
25+
printf("size of float: %lu\n", sizeof(d));
26+
printf("size of double: %lu\n", sizeof(f));
27+
return 0;
28+
}

Day 2/logicalOperators.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Understand the Logical operators in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
9+
int x = 4;
10+
11+
int y = 3;
12+
13+
// (expression 1) == (expression 2)
14+
// this means we are checking whether both give the same result
15+
int z = (x == y);
16+
printf("X equals Y: %d\n", z);
17+
18+
return 0;
19+
}

Day 2/long.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
5+
long long longInteger = 354551315513135;
6+
7+
printf("Value of longInteger is: %lld\n", longInteger);
8+
9+
return 0;
10+
}

Day 2/printf.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
5+
int var1 = 2, var2 = 3, var3 = 4;
6+
7+
printf("All variables: %d, %d, %d\n", var1, var2, var3);
8+
9+
return 0;
10+
}

Day 2/scanf.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
5+
int number;
6+
7+
printf("Please enter the number: ");
8+
9+
scanf("%d", &number);
10+
11+
printf("The number input from the user is: %d", number);
12+
13+
return 0;
14+
}

Day 2/unaryOperator.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Understand the Unary operators in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
9+
int x = 4;
10+
11+
// post increment
12+
int y = x++;
13+
14+
printf("\nPOST INCREMENT: \n");
15+
printf("Value of y: %d\n", y);
16+
printf("Value of x: %d\n", x);
17+
18+
// pre increment
19+
int z = ++x;
20+
21+
printf("\nPRE INCREMENT: \n");
22+
printf("Value of z: %d\n", z);
23+
printf("Value of x: %d\n", x);
24+
return 0;
25+
}

0 commit comments

Comments
 (0)