0% found this document useful (0 votes)
3 views15 pages

C Programs - Updated - Docx1

The document outlines the details of a national level techno-cultural-fine arts-sports fest called UniFusion-2025, scheduled for March 28-29, 2025, which includes a coding competition. It provides programming tasks in C, Java, and Python, covering string manipulation, prime number checking, palindrome checking, matrix multiplication, sorting algorithms, basic calculator functionality, Fibonacci sequence generation, list comprehension, set operations, and prime number generation. Additionally, it includes rules regarding participation, such as the necessity of an ID card and restrictions on electronic devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

C Programs - Updated - Docx1

The document outlines the details of a national level techno-cultural-fine arts-sports fest called UniFusion-2025, scheduled for March 28-29, 2025, which includes a coding competition. It provides programming tasks in C, Java, and Python, covering string manipulation, prime number checking, palindrome checking, matrix multiplication, sorting algorithms, basic calculator functionality, Fibonacci sequence generation, list comprehension, set operations, and prime number generation. Additionally, it includes rules regarding participation, such as the necessity of an ID card and restrictions on electronic devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

A National Level

UniFusion-2025: A Techno-Cultural-Fine Arts-Sports Fest


28-29 March 2025
Coding Competition
Time Duration: 1 Hour

1. String Reverse: Write a C program that reverses a given string.

#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int start = 0;
int end = strlen(str) - 1;

// Swap characters from start and end until they meet


while (start < end) {
// Swap the characters
char temp = str[start];
str[start] = str[end];
str[end] = temp;

// Move towards the middle


start++;
end--;
}
}

int main() {
char str[100];

// Get input from the user


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Remove the newline character if it's there


str[strcspn(str, "\n")] = '\0';

// Reverse the string


reverseString(str);

// Display the reversed string


printf("Reversed string: %s\n", str);

return 0;
}

2. Prime Number Check: Write a C function int is Prime(int num) that returns 1 if num
is prime, and 0 otherwise
#include <stdio.h>

int isPrime(int num) {


// Handle cases where num is less than 2 (prime numbers are greater than 1)
if (num <= 1) {
return 0; // Not a prime number
}

// Check for factors from 2 to the square root of num


for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0; // num is divisible by i, so it's not prime
}
}

return 1; // num is prime


}

int main() {
int num;

// Input the number to check


printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is prime


if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
3. Palindrome Check: Write a C program that checks if a string is a palindrome.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isPalindrome(char str[]) {


int start = 0;
int end = strlen(str) - 1;

while (start < end) {


// Skip non-alphanumeric characters
if (!isalnum(str[start])) {
start++;
} else if (!isalnum(str[end])) {
end--;
} else {
// Compare characters, ignoring case
if (tolower(str[start]) != tolower(str[end])) {
return 0; // Not a palindrome
}
start++;
end--;
}
}

return 1; // Is a palindrome
}

int main() {
char str[100];

// Get input from the user


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character if present
str[strcspn(str, "\n")] = '\0';

// Check if the string is a palindrome


if (isPalindrome(str)) {
printf("'%s' is a palindrome.\n", str);
} else {
printf("'%s' is not a palindrome.\n", str);
}

return 0;
}
4. Matrix Multiplication: Write a C program that multiplies two matrices.
#include <stdio.h>

#define MAX 10

// Function to multiply two matrices


void multiplyMatrices(int first[][MAX], int second[][MAX], int result[][MAX], int
firstRows, int firstCols, int secondRows, int secondCols) {
// Check if multiplication is possible
if (firstCols != secondRows) {
printf("Matrix multiplication is not possible. The number of columns in the first
matrix must equal the number of rows in the second matrix.\n");
return;
}

// Perform matrix multiplication


for (int i = 0; i < firstRows; i++) {
for (int j = 0; j < secondCols; j++) {
result[i][j] = 0;
for (int k = 0; k < firstCols; k++) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

// Function to display a matrix


void displayMatrix(int matrix[][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];
int firstRows, firstCols, secondRows, secondCols;

// Input dimensions of the first matrix


printf("Enter number of rows and columns for the first matrix: ");
scanf("%d %d", &firstRows, &firstCols);

// Input elements of the first matrix


printf("Enter elements of the first matrix:\n");
for (int i = 0; i < firstRows; i++) {
for (int j = 0; j < firstCols; j++) {
scanf("%d", &first[i][j]);
}
}

// Input dimensions of the second matrix


printf("Enter number of rows and columns for the second matrix: ");
scanf("%d %d", &secondRows, &secondCols);

// Input elements of the second matrix


printf("Enter elements of the second matrix:\n");
for (int i = 0; i < secondRows; i++) {
for (int j = 0; j < secondCols; j++) {
scanf("%d", &second[i][j]);
}
}

// Multiply matrices
multiplyMatrices(first, second, result, firstRows, firstCols, secondRows,
secondCols);

// Display the result


printf("\nResultant Matrix after multiplication:\n");
displayMatrix(result, firstRows, secondCols);

return 0;
}
5. Array Sorting (Bubble/Insertion): Write a Java program that takes an array of
integers as input and sorts it in ascending order using Bubble Sort.
import java.util.Scanner;

public class BubbleSort {


// Method to perform Bubble Sort
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;

for (int i = 0; i < n - 1; i++) {


swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped in the inner loop, array is already sorted
if (!swapped) {
break;
}
}
}

// Method to print the array


public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Taking array input from user


System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();

int[] arr = new int[n];


System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

// Sorting the array


bubbleSort(arr);

// Printing the sorted array


System.out.println("Sorted array:");
printArray(arr);

scanner.close();
}
}
6. Simple Calculator: Write a Java program that takes two numbers and an operator (+,
-, *, /) as input and prints the result of the operation.
import java.util.Scanner;

public class SimpleCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Taking input for two numbers


System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");


double num2 = scanner.nextDouble();

// Taking input for the operator


System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

double result;

// Performing the operation based on the operator


switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero is not allowed.");
return;
}
break;
default:
System.out.println("Error: Invalid operator.");
return;
}

// Printing the result


System.out.println("Result: " + result);

scanner.close();
}
}
7. Fibonacci sequence (Iteration): Write a Java program that takes an integer 'n' as
input and prints the Fibonacci sequence up to 'n' terms.
import java.util.Scanner;

public class FibonacciSequence {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for number of terms
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();

// Validate input
if (n <= 0) {
System.out.println("Please enter a positive integer.");
} else {
System.out.println("Fibonacci sequence up to " + n + " terms:");
printFibonacci(n);
}

scanner.close();
}

// Method to print Fibonacci sequence


public static void printFibonacci(int n) {
int first = 0, second = 1;

for (int i = 0; i < n; i++) {


System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
System.out.println();
}
}
8. List Comprehension: Create a list of squares of even numbers from 1 to 20 using list
comprehension.
Python:
squares = [x**2 for x in range(1, 21) if x % 2 == 0]
print(squares)
9. Set Operations: Given two sets, find their intersection and union.
# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Find intersection
intersection = set1 & set2 # or use set1.intersection(set2)

# Find union
union = set1 | set2 # or use set1.union(set2)

# Print results
print("Intersection:", intersection)
print("Union:", union)

O/p:
Intersection: {4, 5}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
10.Prime Number Generation: Write a Python function that generates a list of prime
numbers up to a given limit.
def generate_primes(limit):
if limit < 2:
return []

primes = []
is_prime = [True] * (limit + 1)
is_prime[0], is_prime[1] = False, False # 0 and 1 are not prime numbers

for num in range(2, limit + 1):


if is_prime[num]:
primes.append(num)
for multiple in range(num * num, limit + 1, num):
is_prime[multiple] = False

return primes

# Example usage
limit = int(input("Enter the limit: "))
print("Prime numbers up to", limit, ":", generate_primes(limit))
Note:
 ID card is mandatory
 Electronics devices are not allowed
 If a student is found involved in malpractices, they may face disqualification from
the competition.

You might also like