Skip to content

Commit f69cf87

Browse files
committed
Add Day 6: More on loops
1 parent 184d360 commit f69cf87

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

Day 6/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## More on Loops
2+
3+
1. [Factorial of n using while loop](whileLoop.c)
4+
2. [Find sum of digits of a number](sumDigits.c)
5+
3. [do...while loop](doWhile.c)
6+
4. [Infinite loops](infiniteLoop.c)
7+
8+
## Nested loops
9+
1. [Multiplication table using nested loops](nestedLoops.c)

Day 6/doWhileLoop.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Program to add numbers entered by users
3+
* as long as -1 is not entered
4+
* using do while loop.
5+
*/
6+
7+
#include <stdio.h>
8+
9+
int main () {
10+
11+
int n = 0; // variable which is input by the user
12+
int sum = 0;
13+
do {
14+
sum += n;
15+
// do body
16+
scanf("%d", &n);
17+
} while (n != -1) ;
18+
19+
printf("%d\n", sum);
20+
21+
// Dry run:
22+
// sum += 0
23+
24+
// User enters n = 1
25+
// n != -1 => True
26+
// sum += 1
27+
28+
// User enters n = 2
29+
// n != -1 => True
30+
// sum += 2
31+
32+
// User enters n = -1
33+
// n != -1 => False
34+
// go outside the loop, we don't add -1 now!
35+
return 0;
36+
}

Day 6/infiniteLoop.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Program to write infinite loops
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
// infinite loop
10+
11+
/*
12+
for (; ;) {
13+
; // nothing to execute
14+
}
15+
*/
16+
17+
while (1) {
18+
;
19+
}
20+
21+
return 0;
22+
}

Day 6/nestedLoops.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Program to print multiplication table
3+
* of numbers from 1 to 10
4+
* using nested loops.
5+
*/
6+
7+
#include <stdio.h>
8+
9+
int main () {
10+
11+
/*
12+
1 2 3 4 5 6 7 8 9 10
13+
2 4 6 8 10 12 14 16 18 20
14+
3 6 9 12 15 18 21 24 27 30
15+
4 ...
16+
*/
17+
18+
// what are we looping on?
19+
// print the first multiples of all numbers in the first line
20+
// print the second multiples of all numbers in the second line
21+
// ...
22+
// print the 10th multiple of all numbers in last line
23+
24+
// outer loop: we loop on the i-th multiple (i=1 to 10)
25+
// inner loop: all numbers
26+
27+
return 0;
28+
}

Day 6/sumDigits.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Program to find sum of digits of a number
3+
* using while loop
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// input 1234
11+
// 1+2+3+4 = 10
12+
13+
// basically, your brain does the following:
14+
// sees all the digits in the number
15+
// and adds them up one by one
16+
17+
// basically, to write a program
18+
// we need to loop oer all the digits in the number
19+
// and add them to the result variable
20+
21+
// How do find out a specific digit in the number?
22+
// last digit => remainder when divided by 10
23+
// 1234 % 10 => 4, add to result, result = 4
24+
// 1234/10 = 123 (integer division)
25+
// 123 % 10 => 3, add to result, result = 4+3 = 7
26+
// 123/10 = 12
27+
// 12 % 10 => 2, add to result, result = 7+2 = 9
28+
// 12/10 = 1
29+
// 1 % 10 => 1, add to result, result = 9+1 = 10
30+
// 1/10 = 0 (stop at this point)
31+
32+
// Pseudo code:
33+
// number as input
34+
// result <- 0
35+
// while (number > 0)
36+
// find the last digit (number % 10)
37+
// add the last digit to the result (result += lastDigit)
38+
// divide number by 10 (number /= 10)
39+
// print(result)
40+
41+
int number;
42+
scanf("%d", &number);
43+
44+
int result = 0;
45+
while (number > 0) {
46+
// lastDigit is a local variable
47+
int lastDigit = number % 10;
48+
result += lastDigit;
49+
number /= 10;
50+
}
51+
52+
// line below won't work, the scope of variable
53+
// is limited to the part where it is declared
54+
55+
// lastDigit is declared inside the while loop
56+
// so it can't be accessed outside the while loop
57+
// printf("%d", lastDigit);
58+
59+
printf("The sum of digits in the number is: %d\n", result);
60+
return 0;
61+
}

Day 6/whileLoop.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Program to multiply numbers from 1 to n
3+
* using while loop
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// variable to store result
11+
int result = 1;
12+
13+
int n;
14+
scanf("%d", &n);
15+
16+
// Pseudo code:
17+
// result <- 1
18+
// number <- 1
19+
// while (number <= n)
20+
// multiply number to result
21+
// increment number by 1
22+
23+
int number = 1;
24+
while (number <= n) {
25+
result *= number;
26+
number ++;
27+
}
28+
29+
printf("The product of numbers from 1 to %d is: %d\n", n, result);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)