MCS201 Repeated Codes Solutions
MCS201 Repeated Codes Solutions
// Iterative
int factorial_iterative(int n) {
int result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
// Recursive
int factorial_recursive(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial_recursive(n - 1);
}
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
flag = 0;
break;
}
}
if (flag)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
# Copying content
with open("first.txt", "r") as f1, open("second.txt", "w") as f2:
f2.write(f1.read())
# Reading a file
with open("second.txt", "r") as f:
print(f.read())
# Writing to a file
with open("file.txt", "w") as f:
f.write("Hello World")
# Appending to a file
with open("file.txt", "a") as f:
f.write("\nNew Line")
#include <stdio.h>
int main() {
int x1, y1, x2, y2;
float slope;
printf("Enter coordinates (x1 y1 x2 y2): ");
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (x2 - x1 == 0)
printf("Slope is undefined\n");
else {
slope = (float)(y2 - y1) / (x2 - x1);
printf("Slope = %.2f\n", slope);
}
return 0;
}
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print("Squares:", squares)
# Generator
def generate():
for i in range(5):
yield i*i
g = generate()
for val in g:
print(val)
int x = 10;
float f = (float)x / 3; // Typecast
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="root",
password="yourpass",
database="Employee_DB"
)
print("Connected", conn)