summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorsmlckz <smlckz@college>2021-12-24 15:21:53 +0530
committersmlckz <smlckz@college>2021-12-24 15:21:53 +0530
commit6dec141d4529a490384eb42314c04428fc58ba20 (patch)
treea0bc436963bfa404c5208f12ad637f11fc70b20d
parentb73983c3717642ca10e7cfe93d97609adc377da9 (diff)
downloadcollege-6dec141d4529a490384eb42314c04428fc58ba20.tar.gz
backup HEAD master
-rw-r--r--assignments/22-dec-to-roman.c34
-rw-r--r--assignments/23-basic-calculator.c149
-rw-r--r--assignments/24-value-vs-ref.c17
-rw-r--r--assignments/25-krishnamurti.c53
-rw-r--r--assignments/27-fac-rec.c52
-rw-r--r--assignments/28-fib-rec.c58
-rw-r--r--assignments/29-gcd-rec.c49
7 files changed, 399 insertions, 13 deletions
diff --git a/assignments/22-dec-to-roman.c b/assignments/22-dec-to-roman.c
index 05a9553..e4dbef1 100644
--- a/assignments/22-dec-to-roman.c
+++ b/assignments/22-dec-to-roman.c
@@ -51,3 +51,37 @@ int main(void)
 	return 0;
 }
 
+/*
+Output:
+Set 1:
+To convert a decimal number to roman number
+
+Enter a number: 0
+Invalid input: it must be between 1 and 3999, inclusive.
+
+Set 2:
+To convert a decimal number to roman number
+
+Enter a number: 123
+CXXIII
+
+Set 3:
+To convert a decimal number to roman number
+
+Enter a number: 987
+CMLXXXVII
+
+Set 4:
+To convert a decimal number to roman number
+
+Enter a number: 1234
+MCCXXXIV
+
+Set 5:
+To convert a decimal number to roman number
+
+Enter a number: 2946
+MMCMXLVI
+
+*/
+
diff --git a/assignments/23-basic-calculator.c b/assignments/23-basic-calculator.c
index 203fdf8..3a5aaf0 100644
--- a/assignments/23-basic-calculator.c
+++ b/assignments/23-basic-calculator.c
@@ -1,23 +1,146 @@
 #include <stdio.h>
 
-int menu_option(char *choice)
+void read_values(int *a, int *b)
 {
-	printf(
-	"Basic Calculator\n"
-	"================\n"
-	"Menu:\n"
-	"	+  Add\n"
-	"	-  Subtract\n"
-	"	*  Multiply\n"
-	"	/  Divide\n"
-	"	q  Quit\n\n"
-	);
+	printf("Enter two values: ");
+	scanf("%d%d", a, b);
 }
 
 int main(void)
 {
-	char *choice;
-	menu_option(&choice);
+	int a, b, c, choice;
+	for (;;) {
+		printf("\nEnter:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n\n");
+		printf("Enter choice: ");
+		scanf("%d", &choice);
+		switch (choice) {
+		case 1:
+			read_values(&a, &b);
+			printf("The sum of %d and %d is %d\n", a, b, a + b);
+			break;
+		case 2:
+			read_values(&a, &b);
+			printf("The difference of %d and %d is %d\n", a, b, a - b);
+			break;
+		case 3:
+			read_values(&a, &b);
+			printf("The product of %d and %d is %d\n", a, b, a * b);
+			break;
+		case 4:
+			read_values(&a, &b);
+			if (b == 0) {
+				printf("Can not divide by zero.\n");
+				return 0;
+			}
+			printf("The quotient of %d and %d is %d\n", a, b, a / b);
+			break;
+		case 5:
+			printf("\nBye\n\n");
+			return 0;
+		default:
+			printf("Invalid choice\nPlease choose a valid option given in the menu.\n");
+		}
+	}
 	return 0;
 }
 
+/*
+Output:
+=== Set 1 ===
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 1
+Enter two values: 123 456
+The sum of 123 and 456 is 579
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 2   
+Enter two values: 789 3456
+The difference of 789 and 3456 is -2667
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 3
+Enter two values: 78 45
+The product of 78 and 45 is 3510
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 4   
+Enter two values: 12 5
+The quotient of 12 and 5 is 2
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 5
+
+Bye
+
+=== Set 2 ===
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 4
+Enter two values: 1 0
+Can not divide by zero.
+
+=== Set 3 ===
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 0
+Invalid choice
+Please choose a valid option given in the menu.
+
+Enter:
+1. Addition
+2. Subtraction
+3. Multiplication
+4. Division
+5. Exit
+
+Enter choice: 5
+
+Bye
+
+
+
+*/
+
diff --git a/assignments/24-value-vs-ref.c b/assignments/24-value-vs-ref.c
index 8640792..094db82 100644
--- a/assignments/24-value-vs-ref.c
+++ b/assignments/24-value-vs-ref.c
@@ -16,6 +16,23 @@ void swap_by_ref(int *a, int *b)
 
 int main(void)
 {
+	int x, y;
+	printf("Enter two numbers: ");
+	scanf("%d%d", &x, &y);
+	printf("The values are: %d %d\n", x, y);
+	swap_by_value(x, y);
+	printf("The values are: %d %d\n", x, y);
+	swap_by_ref(&x, &y);
+	printf("The values are: %d %d\n", x, y);
 	return 0;
 }
 
+/*
+Output:
+Enter two numbers: 2 3
+The values are: 2 3
+The values are: 2 3
+The values are: 3 2
+
+*/
+
diff --git a/assignments/25-krishnamurti.c b/assignments/25-krishnamurti.c
new file mode 100644
index 0000000..fd3a1ca
--- /dev/null
+++ b/assignments/25-krishnamurti.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+int fact(int n)
+{
+	int i, f = 1;
+	for (i = n; i > 1; i--) f *= i;
+	return f;
+}
+
+int is_krishnamurti(int n)
+{
+	int m = n, sum = 0, r;
+	while (m > 0) {
+		r = m % 10;
+		sum += fact(r);
+		m /= 10;
+	}
+	return n == sum;
+}
+
+int main(void)
+{
+	int n;
+	printf("To check whether a given number is a Krishnamurti number or not\n\n");
+	printf("Enter a number: ");
+	scanf("%d", &n);
+	printf("%d is%s a Krishnamurti number.\n", n, is_krishnamurti(n) ? "" : " not");
+	return 0;
+}
+
+
+/*
+Output:
+Set 1:
+To check whether a given number is a Krishnamurti number or not
+
+Enter a number: 2
+2 is a Krishnamurti number.
+
+Set 2:
+To check whether a given number is a Krishnamurti number or not
+
+Enter a number: 145
+145 is a Krishnamurti number.
+
+Set 3:
+To check whether a given number is a Krishnamurti number or not
+
+Enter a number: 123
+123 is not a Krishnamurti number.
+
+*/
+
diff --git a/assignments/27-fac-rec.c b/assignments/27-fac-rec.c
new file mode 100644
index 0000000..13f00d3
--- /dev/null
+++ b/assignments/27-fac-rec.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+
+int fact(int n)
+{
+	if (n == 0)
+		return 1;
+	else
+		return n * fact(n - 1);
+}
+
+int main(void)
+{
+	int n;
+	printf("To calculate the factorial of a given number\n\n");
+	printf("Enter a number: ");
+	scanf("%d", &n);
+	if (n < 0) {
+		printf("Invalid input, must be greater than -1\n");
+		return 0;
+	}
+	printf("%d! = %d\n", n, fact(n));
+	return 0;
+}
+
+/*
+Output:
+Set 1:
+To calculate the factorial of a given number
+
+Enter a number: -1
+Invalid input, must be greater than zero
+
+Set 2:
+To calculate the factorial of a given number
+
+Enter a number: 0
+0! = 1
+
+Set 3:
+To calculate the factorial of a given number
+
+Enter a number: 5
+5! = 120
+
+Set 4:
+To calculate the factorial of a given number
+
+Enter a number: 10
+10! = 3628800
+
+*/
+
diff --git a/assignments/28-fib-rec.c b/assignments/28-fib-rec.c
new file mode 100644
index 0000000..6e483a2
--- /dev/null
+++ b/assignments/28-fib-rec.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+
+int fib(int n)
+{
+	if (n == 1)
+		return 0;
+	else if (n == 2)
+		return 1;
+	else
+		return fib(n - 1) + fib(n - 2);
+}
+
+int main(void)
+{
+	int i,n;
+	printf("To calculate the Fibonacci Series upto a given number\n\n");
+	printf("Enter a number: ");
+	scanf("%d", &n);
+	if (n <= 0) {
+		printf("Invalid input, must be greater than 0\n");
+		return 0;
+	}
+	printf("The first %d elements of the Fibonacci series: ", n);
+	for (i = 1; i <= n; i++) {
+		printf("%d ", fib(i));
+	}
+	printf("\n");
+	return 0;
+}
+
+/*
+Output:
+Set 1:
+To calculate the Fibonacci Series upto a given number
+
+Enter a number: 0 
+Invalid input, must be greater than 0
+
+Set 2:
+To calculate the Fibonacci Series upto a given number
+
+Enter a number: 5
+The first 5 elements of the Fibonacci series: 0 1 1 2 3 
+
+Set 3:
+To calculate the Fibonacci Series upto a given number
+
+Enter a number: 10
+The first 10 elements of the Fibonacci series: 0 1 1 2 3 5 8 13 21 34 
+
+Set 4:
+To calculate the Fibonacci Series upto a given number
+
+Enter a number: 25
+The first 25 elements of the Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 
+
+*/
+
diff --git a/assignments/29-gcd-rec.c b/assignments/29-gcd-rec.c
new file mode 100644
index 0000000..dcf2c90
--- /dev/null
+++ b/assignments/29-gcd-rec.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+
+int gcd(int a, int b)
+{
+	if (b == 0)
+		return a;
+	else
+		return gcd(b, a % b);
+}
+
+int main(void)
+{
+	int a, b;
+	printf("To calculate the Greatest Common Divisor of given two numbers\n\n");
+	printf("Enter two numbers: ");
+	scanf("%d%d", &a, &b);
+	printf("The GCD of %d and %d is %d\n", a, b, gcd(a, b));
+	return 0;
+}
+
+/*
+Output:
+Set 1:
+To calculate the Greatest Common Divisor of given two numbers
+
+Enter two numbers: 2 3
+The GCD of 2 and 3 is 1
+
+Set 2:
+To calculate the Greatest Common Divisor of given two numbers
+
+Enter two numbers: 15 18
+The GCD of 15 and 18 is 3
+
+Set 3:
+To calculate the Greatest Common Divisor of given two numbers
+
+Enter two numbers: 16 12
+The GCD of 16 and 12 is 4
+
+Set 4:
+To calculate the Greatest Common Divisor of given two numbers
+
+Enter two numbers: 65 91
+The GCD of 65 and 91 is 13
+
+
+*/
+