0% found this document useful (0 votes)
20 views3 pages

MCS201 Repeated Codes Solutions

The document provides a collection of programming codes and solutions in Python and C, covering various topics such as Fibonacci series, factorial calculation (both iterative and recursive), palindrome checking, file handling, slope calculation, map function, generator functions, call by reference, typedef and typecasting, and MySQL connection. Each code snippet is accompanied by a brief explanation of its functionality. This serves as a practical reference for programming concepts and implementations.

Uploaded by

Muskan Rastogi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

MCS201 Repeated Codes Solutions

The document provides a collection of programming codes and solutions in Python and C, covering various topics such as Fibonacci series, factorial calculation (both iterative and recursive), palindrome checking, file handling, slope calculation, map function, generator functions, call by reference, typedef and typecasting, and MySQL connection. Each code snippet is accompanied by a brief explanation of its functionality. This serves as a practical reference for programming concepts and implementations.

Uploaded by

Muskan Rastogi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Repeated Programming Codes with Solutions (MCS-201)

1. Fibonacci Series (Python)

# Python program to print Fibonacci series up to n terms


n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=' ')
a, b = b, a + b

2. Factorial (C - Iterative & Recursive)

// 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);
}

3. Palindrome Check (C)

#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;
}

4. File Handling in Python

# 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")

5. Slope of a Line (C)

#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;
}

6. Map Function (Python)

numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print("Squares:", squares)

7. Generator Function (Python)

# Generator
def generate():
for i in range(5):
yield i*i

g = generate()
for val in g:
print(val)

8. Call by Reference (C)

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

9. Typedef and Typecast (C)

typedef int myInt;


myInt a = 5;

int x = 10;
float f = (float)x / 3; // Typecast

10. MySQL Connect in Python

import mysql.connector

conn = mysql.connector.connect(
host="localhost",
user="root",
password="yourpass",
database="Employee_DB"
)
print("Connected", conn)

You might also like