Skip to content

Commit fc24870

Browse files
committed
fresh
1 parent 0cd3b47 commit fc24870

File tree

93 files changed

+4989
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+4989
-0
lines changed

s2/c/00.hello_world.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("Hello world\n");
5+
return 0;
6+
}
7+
8+
/* Algorithm
9+
Step 1: Start
10+
Step 2: Display "Hello world" to the console.
11+
Step 3: Stop
12+
*/

s2/c/01.sum_2_num.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int num1, num2;
5+
6+
printf("Enter the 1st number: ");
7+
scanf("%d", &num1);
8+
printf("Enter the 2nd number: ");
9+
scanf("%d", &num2);
10+
11+
printf("%d + %d = %d\n", num1, num2, num1 + num2);
12+
13+
return 0;
14+
}
15+
16+
17+
/*Algorithm
18+
Step 1: Start
19+
Step 2: Declare variables num1, num2 and sum.
20+
Step 3: Read values num1 and num2.
21+
Step 4: Add num1 and num2 and assign the result to sum.
22+
sum ← num1+num2
23+
Step 5: Display sum
24+
Step 6: Stop
25+
*/

s2/c/02.area_circle.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#define PI 3.14
3+
4+
int main() {
5+
float r, area;
6+
7+
printf("Enter radius of the circle: ");
8+
scanf("%f", &r);
9+
area = PI * r * r;
10+
printf("The area of the circle is %f\n", area);
11+
12+
return 0;
13+
}
14+
15+
/*Algorithm
16+
Step 1: Start
17+
Step 2: Define constant PI = 3.14.
18+
Step 3: Declare variables r and area.
19+
Step 4: Read value of r.
20+
Step 5: Calculate area (PI * r * r) and assign the value to area.
21+
area ← PI * r * r
22+
Step 6: Display the area
23+
Step 7: Stop
24+
*/

s2/c/03.expression.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int a, b, c, d, e, f, g, result;
5+
6+
printf("Enter the values of a, b, c, d, e, f and g :");
7+
scanf("%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g);
8+
9+
result = ((a-b/c*d+e)*(f+g));
10+
11+
printf("Result of ((a-b/c+d+e)*(f+g)) = %d\n", result);
12+
return 0;
13+
}
14+
15+
/*Algorithm
16+
Step 1: Start
17+
Step 2: Declare variables a, b, c, d, e, f, g and result.
18+
Step 3: Read values of a, b, c, d, e, f and g.
19+
Step 4: Calculate expression ((a-b/c*d+e)*(f+g)) and assign the value to result.
20+
result ← ((a-b/c*d+e)*(f+g))
21+
Step 5: Display the result
22+
Step 6: Stop
23+
*/

s2/c/04.largest_of_3.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int a, b, c, largest;
5+
6+
printf("Enter 3 numbers: ");
7+
scanf("%d%d%d", &a, &b, &c);
8+
9+
if (a >= b && a >= c) {
10+
largest = a;
11+
} else if (b >= a && b >= c) {
12+
largest = b;
13+
} else {
14+
largest = c;
15+
}
16+
17+
printf("The largest number is %d\n", largest);
18+
19+
return 0;
20+
}
21+
22+
/*Algorithm
23+
Step 1: Start
24+
Step 2: Declare variables a, b, c and largest.
25+
Step 3: Read values of a, b and c.
26+
Step 4: if a >= b && a >= c
27+
largest ← a;
28+
else if b >= a && b >= c
29+
largest ← b;
30+
else
31+
largest ← c;
32+
Step 5: Display the largest
33+
Step 6: Stop
34+
*/

s2/c/05.prime_check.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
int main()
5+
{
6+
int num, i, isPrime = 1;
7+
8+
printf("Enter a number: ");
9+
scanf("%d", &num);
10+
11+
if (num <= 1)
12+
isPrime = 0;
13+
else
14+
for (i = 2; i <= sqrt(num); i++)
15+
{
16+
if (num % i == 0)
17+
{
18+
isPrime = 0;
19+
break;
20+
}
21+
}
22+
23+
if (isPrime)
24+
printf("%d is a prime number\n", num);
25+
else
26+
printf("%d is not a prime number\n", num);
27+
28+
return 0;
29+
}
30+
31+
/*Algorithm
32+
Step 1: Start
33+
Step 2: Declare variables num, i and isPrime.
34+
Step 3: Initialize variables
35+
isPrime ← 1
36+
i ← 2
37+
Step 4: Read num from the user.
38+
Step 5: Repeat the steps until i<=sqrt(num)
39+
5.1 If remainder of num÷i equals 0
40+
isPrime ← 0
41+
Go to step 6
42+
5.2 i ← i+1
43+
Step 6: If isPrime = 0
44+
Display num is not prime
45+
else
46+
Display num is prime
47+
Step 7: Stop
48+
*/

s2/c/06.armstrong_check.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
int main() {
5+
int num, tempNum, remainder, result = 0, d = 0;
6+
7+
printf("Enter an integer: ");
8+
scanf("%d", &num);
9+
10+
tempNum = num;
11+
12+
while (tempNum != 0) {
13+
tempNum /= 10;
14+
d++;
15+
}
16+
17+
tempNum = num;
18+
19+
while (tempNum != 0) {
20+
remainder = tempNum % 10;
21+
result += pow(remainder, d);
22+
tempNum /= 10;
23+
}
24+
25+
if (result == num) printf("%d is an Armstrong number.\n", num);
26+
else printf("%d is not an Armstrong number.\n", num);
27+
28+
return 0;
29+
}
30+
31+
32+
33+
/*Algorithm
34+
Step 1: Start
35+
Step 2: Declare variables num, tempNum, remainder, d and result.
36+
Step 3: Initialize variables
37+
result ← 0
38+
d ← 0
39+
Step 4: Read num from the user.
40+
Step 5: tempNum num
41+
Step 6: Repeat the steps until tempNum != 0
42+
6.1 tempNum ← tempNum/10
43+
6.2 d ← d+1
44+
Step 7: tempNum ← num
45+
Step 8: Repeat the steps until tempNum != 0
46+
8.1 remainder ← tempNum%10
47+
8.2 result ← result + pow(remainder, d);
48+
8.3 tempNum ← tempNum/10
49+
Step 9: If result == num
50+
Display num is an Armstrong number.
51+
else
52+
Display num is not an Armstrong number.
53+
Step 10: Stop
54+
*/

s2/c/07.arr_sum_avg.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int n, i;
5+
float sum = 0, average;
6+
7+
printf("Enter the size of the array: ");
8+
scanf("%d", &n);
9+
10+
int arr[n];
11+
12+
printf("Enter array elements:\n");
13+
for (i = 0; i < n; i++) {
14+
printf("%d : ", i + 1);
15+
scanf("%d", &arr[i]);
16+
sum += arr[i];
17+
}
18+
19+
average = sum / n;
20+
21+
printf("Sum of the numbers: %f\n", sum);
22+
printf("Average of the numbers: %f\n", average);
23+
24+
return 0;
25+
}
26+
27+
/*Algorithm
28+
Step 1: Start
29+
Step 2: Declare variables n, i, sum and average.
30+
Step 3: Initialize variables
31+
sum ← 0
32+
Step 4: Read n from the user.
33+
Step 5: Declare array arr with size n
34+
Step 6: Read elements of arr from the user.
35+
Step 7: Repeat the steps until i < n
36+
7.1 sum ← sum + arr[i]
37+
7.2 i ← i+1
38+
Step 8: average ← sum / n
39+
Step 9: Display sum and average.
40+
Step 10: Stop
41+
*/

s2/c/08.arr_linear_search.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int n, key, found, index;
5+
6+
printf("Enter the size of array: ");
7+
scanf("%d", &n);
8+
9+
int arr[n];
10+
11+
printf("Enter array elements:\n");
12+
for (int i = 0; i < n; i++) {
13+
printf("%d : ", i + 1);
14+
scanf("%d", &arr[i]);
15+
}
16+
17+
printf("Enter the element to search: ");
18+
scanf("%d", &key);
19+
20+
for (int i = 0; i < n; i++) {
21+
if (arr[i] == key) {
22+
found = 1;
23+
index = i;
24+
break;
25+
}
26+
}
27+
28+
if (found) printf("Element %d found at index %d\n", key, index);
29+
else printf("Element %d not found in the array\n", key);
30+
31+
return 0;
32+
}
33+
34+
/*Algorithm
35+
Step 1: Start
36+
Step 2: Declare variables n, i = 0, key, found and index.
37+
Step 3: Read n from the user.
38+
Step 4: Declare array arr with size n.
39+
Step 5: Read elements of arr from the user.
40+
Step 6: Read key from the user.
41+
Step 7: Repeat the steps until i < n
42+
7.1 If arr[i] == key
43+
found ← 1
44+
index ← i
45+
Go to step 8
46+
7.2 i ← i+1
47+
Step 8: If found
48+
Display Element found
49+
else
50+
Display Element not found
51+
Step 9: Stop
52+
*/

s2/c/09.arr_bubble_sort.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int n, i, j;
5+
6+
printf("Enter the size of array: ");
7+
scanf("%d", &n);
8+
9+
int arr[n];
10+
11+
printf("Enter array elements:\n");
12+
for (int i = 0; i < n; i++) {
13+
printf("%d : ", i + 1);
14+
scanf("%d", &arr[i]);
15+
}
16+
17+
for (i = 0; i < n - 1; i++) {
18+
for (j = 0; j < n - i - 1; j++) {
19+
if (arr[j] > arr[j + 1]) {
20+
int temp = arr[j];
21+
arr[j] = arr[j + 1];
22+
arr[j + 1] = temp;
23+
}
24+
}
25+
}
26+
27+
printf("Sorted array in ascending order: ");
28+
for (int i = 0; i < n; i++) {
29+
printf("%d, ", arr[i]);
30+
}
31+
printf("\n");
32+
33+
return 0;
34+
}
35+
36+
/*Algorithm
37+
Step 1: Start
38+
Step 2: Declare variable n, temp, i and j.
39+
Step 3: Initialize variables
40+
i, j ← 0
41+
Step 4: Read n from the user.
42+
Step 5: Declare array arr with size n
43+
Step 6: Read elements of arr from the user.
44+
Step 7: Repeat the steps until i < n-1
45+
7.1 Repeat the steps until j < n-i-1
46+
7.1.1 If arr[j] > arr[j + 1]
47+
temp ← arr[j]
48+
arr[j] ← arr[j + 1]
49+
arr[j + 1] ← temp
50+
7.1.2 j ← j+1
51+
7.2 i ← i+1
52+
Step 8: Display sorted arr.
53+
Step 9: Stop
54+
*/

0 commit comments

Comments
 (0)