You are on page 1of 18

===================================================================================

=============
### Question 1
a. Program to read and display int, float, char, and string:
```c
#include <stdio.h>

int main() {
int integer;
float floating;
char character;
char string[100];

printf("Enter an integer: ");


scanf("%d", &integer);

printf("Enter a float: ");


scanf("%f", &floating);

printf("Enter a character: ");


scanf(" %c", &character); // Note the space before %c to consume the newline
character

printf("Enter a string: ");


scanf("%s", string);

printf("Integer: %d\n", integer);


printf("Float: %f\n", floating);
printf("Character: %c\n", character);
printf("String: %s\n", string);

return 0;
}
=============================================================
b. Program to check if a number is a palindrome:
```c
#include <stdio.h>

int isPalindrome(int num) {


int original = num, reversed = 0, remainder;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

return original == reversed;


}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isPalindrome(num))
printf("%d is a palindrome.\n", num);
else
printf("%d is not a palindrome.\n", num);

return 0;
}
```
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++
### Question 2
a. Program to reverse an array:
```c
#include <stdio.h>

void reverseArray(int arr[], int size) {


int start = 0, end = size - 1;
while (start < end) {
// Swap elements at start and end indices
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

start++;
end--;
}
}

int main() {
int size;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

reverseArray(arr, size);

printf("Reversed array: ");


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);

return 0;
}
```
============================================================
b. Program to check if a number is an Armstrong number:
```c
#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {


int original = num, sum = 0, remainder, n = 0;

while (original != 0) {
original /= 10;
n++;
}
original = num;

while (original != 0) {
remainder = original % 10;
sum += pow(remainder, n);
original /= 10;
}

return num == sum;


}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

return 0;
}
-------------------------------------------------------------------
Certainly! Let's continue with the solutions for the remaining questions:

### Question 3
a. Program to check if a number is prime:
```c
#include <stdio.h>

int isPrime(int num) {


if (num < 2)
return 0; // Not a prime number

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0)
return 0; // Not a prime number
}

return 1; // Prime number


}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

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

return 0;
}
```
b. Program to search an element in an array:
```c
#include <stdio.h>

int searchElement(int arr[], int size, int element) {


for (int i = 0; i < size; i++) {
if (arr[i] == element)
return i; // Element found at index i
}

return -1; // Element not found


}

int main() {
int size, element;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

printf("Enter the element to search: ");


scanf("%d", &element);

int index = searchElement(arr, size, element);

if (index != -1)
printf("%d found at index %d.\n", element, index);
else
printf("%d not found in the array.\n", element);

return 0;
}
===================================================================================
======

### Question 4
a. Program to find the sum of the digits of a number:
```c
#include <stdio.h>

int sumOfDigits(int num) {


int sum = 0;

while (num != 0) {
sum += num % 10;
num /= 10;
}

return sum;
}

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

printf("Sum of digits: %d\n", sumOfDigits(num));

return 0;
}
```

===================================================================================
=====
#include <stdio.h>

void findMinMax(int arr[], int size, int *min, int *max) {


*min = *max = arr[0];

for (int i = 1; i < size; i++) {


if (arr[i] < *min)
*min = arr[i];
if (arr[i] > *max)
*max = arr[i];
}
}

int main() {
int size, min, max;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

findMinMax(arr, size, &min, &max);

printf("Minimum element: %d\n", min);


printf("Maximum element: %d\n", max);

return 0;
}
=============================================================================
### Question 5
a. Program to check if the given year is a leap year or not:
```c
#include <stdio.h>

int isLeapYear(int year) {


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return 1; // Leap year
else
return 0; // Not a leap year
}

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (isLeapYear(year))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}
```

b. Program to replace an element in an array at a given index:


```c
#include <stdio.h>

void replaceElement(int arr[], int size, int index, int newValue) {


if (index >= 0 && index < size)
arr[index] = newValue;
else
printf("Index out of bounds.\n");
}

int main() {
int size, index, newValue;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

printf("Enter the index to replace: ");


scanf("%d", &index);

printf("Enter the new value: ");


scanf("%d", &newValue);

replaceElement(arr, size, index, newValue);

printf("Updated array: ");


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);

return 0;
}
```

### Question 6
a. Program to simulate a calculator using switch case:
```c
#include <stdio.h>

int main() {
char operator;
double num1, num2;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator); // Note the space before %c to consume the newline
character

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0)
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
else
printf("Cannot divide by zero.\n");
break;
default:
printf("Invalid operator.\n");
}

return 0;
}
```

b. Program to identify duplicate elements in an array:


```c
#include <stdio.h>

void findDuplicates(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j])
printf("Duplicate element: %d\n", arr[i]);
}
}
}

int main() {
int size;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

findDuplicates(arr, size);
return 0;
}
```
==========================================================================
### Question 7
a. Program that takes marks of 5 subjects, finds the total (integer), and average
(float):
```c
#include <stdio.h>

int main() {
int marks[5];
int total = 0;

printf("Enter marks for 5 subjects:\n");

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


printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
total += marks[i];
}

float average = (float)total / 5;

printf("Total marks: %d\n", total);


printf("Average marks: %.2f\n", average);

return 0;
}
```

b. Program to sort given elements in an array using Bubble sort:


```c
#include <stdio.h>

void bubbleSort(int arr[], int size) {


for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int size;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

bubbleSort(arr, size);

printf("Sorted array: ");


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);

return 0;
}
```
===================================================================================
=
### Question 8
a. Program to find the maximum of three numbers using the conditional operator:
```c
#include <stdio.h>

int main() {
int num1, num2, num3;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

int max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2
: num3);

printf("Maximum number: %d\n", max);

return 0;
}
```================================================================================
==============================

b. Program to add two matrices:


```c=
#include <stdio.h>

void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows,


int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

int main() {
int rows, cols;

printf("Enter the number of rows and columns for matrices: ");


scanf("%d %d", &rows, &cols);

int mat1[10][10], mat2[10][10], result[10][10];

printf("Enter elements for matrix 1:\n");


for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &mat1[i][j]);
printf("Enter elements for matrix 2:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &mat2[i][j]);

addMatrices(mat1, mat2, result, rows, cols);

printf("Resultant matrix after addition:\n");


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

return 0;
}
```================================================================================
================
### Question 9
a. Program to find the square root of a given number:
```c
#include <stdio.h>
#include <math.h>

int main() {
double num;

printf("Enter a number: ");


scanf("%lf", &num);

if (num >= 0)
printf("Square root: %.2lf\n", sqrt(num));
else
printf("Invalid input. Cannot find square root of a negative number.\n");

return 0;
}
```
===================================================================================
===========================
b. Program to perform multiplication of two matrices:
```c
#include <stdio.h>

void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int


rows1, int cols1, int rows2, int cols2) {
if (cols1 != rows2) {
printf("Invalid matrices for multiplication.\n");
return;
}

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


for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

int main() {
int rows1, cols1, rows2, cols2;

printf("Enter the dimensions of matrix 1 (rows and columns): ");


scanf("%d %d", &rows1, &cols1);

printf("Enter the dimensions of matrix 2 (rows and columns): ");


scanf("%d %d", &rows2, &cols2);

int mat1[10][10], mat2[10][10], result[10][10];

printf("Enter elements for matrix 1:\n");


for (int i = 0; i < rows1; i++)
for (int j = 0; j < cols1; j++)
scanf("%d", &mat1[i][j]);

printf("Enter elements for matrix 2:\n");


for (int i = 0; i < rows2; i++)
for (int j = 0; j < cols2; j++)
scanf("%d", &mat2[i][j]);

multiplyMatrices(mat1, mat2, result, rows1, cols1, rows2, cols2);

printf("Resultant matrix after multiplication:\n");


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++)
printf("%d\t", result[i][j]);
printf("\n");
}

return 0;
}
```
===========================================================================
### Question 10
a. Program to find the area of circle, square, rectangle, and triangle:
```c
#include <stdio.h>
#include <math.h>

#define PI 3.14159

int main() {
int choice;
double area;

printf("Choose a shape:\n");
printf("1. Circle\n");
printf("2. Square\n");
printf("3. Rectangle\n");
printf("4. Triangle\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

switch (choice) {
case 1:
{
double radius;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = PI * pow(radius, 2);
printf("Area of the circle: %.2lf\n", area);
}
break;
case 2:
{
double side;
printf("Enter the side length of the square: ");
scanf("%lf", &side);
area = pow(side, 2);
printf("Area of the square: %.2lf\n", area);
}
break;
case 3:
{
double length, width;
printf("Enter the length and width of the rectangle: ");
scanf("%lf %lf", &length, &width);
area = length * width;
printf("Area of the rectangle: %.2lf\n", area);
}
break;
case 4:
{
double base, height;
printf("Enter the base and height of the triangle: ");
scanf("%lf %lf", &base, &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2lf\n", area);
}
break;
default:
printf("Invalid choice.\n");
}

return 0;
}
```
===================================================================================
=====
b. Program to find the transpose of a matrix:
```c
#include <stdio.h>

void transposeMatrix(int mat[10][10], int trans[10][10], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
trans[j][i] = mat[i][j];
}
}
}

int main() {
int rows, cols;
printf("Enter the dimensions of the matrix (rows and columns): ");
scanf("%d %d", &rows, &cols);

int mat[10][10], trans[10][10];

printf("Enter elements for the matrix:\n");


for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &mat[i][j]);

transposeMatrix(mat, trans, rows, cols);

printf("Transpose of the matrix:\n");


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

return 0;
}
```
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++=====
### Question 11
a. Program to evaluate the given expressions:
```c
#include <stdio.h>

int main() {
int a, b, c, d, i, j;

printf("Enter values for a, b, c, d, i, j:\n");


scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &i, &j);

double result1 = a / b * c - b + a * d / 3.0;


int result2 = (i++) + (++i);

printf("Result of expression (a/b*c-b+a*d/3): %.2lf\n", result1);


printf("Result of expression (j = (i++) + (++i)): %d\n", result2);

return 0;
}
```
===================================================================================
=========================
b. Program to convert a string from lowercase to uppercase without using built-in
functions:
```c
#include <stdio.h>

void convertToUpper(char str[]) {


for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
}
}

int main() {
char str[100];

printf("Enter a string in lowercase: ");


scanf("%s", str);

convertToUpper(str);

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

return 0;
}
```
===================================================================================
======================
### Question 12
a. Program to calculate simple interest and compound interest:
```c
#include <stdio.h>
#include <math.h>

double calculateSimpleInterest(double principal, double rate, double time) {


return (principal * rate * time) / 100.0;
}

double calculateCompoundInterest(double principal, double rate, double time) {


return principal * (pow((1 + rate / 100), time) - 1);
}

int main() {
double principal, rate, time;

printf("Enter principal amount, rate of interest, and time period (in years):\
n");
scanf("%lf %lf %lf", &principal, &rate, &time);

double simpleInterest = calculateSimpleInterest(principal, rate, time);


double compoundInterest = calculateCompoundInterest(principal, rate, time);

printf("Simple Interest: %.2lf\n", simpleInterest);


printf("Compound Interest: %.2lf\n", compoundInterest);

return 0;
}
===================================================================================
=======================
b. Program to convert a string from uppercase to lowercase without using built-in
functions:
```c
#include <stdio.h>

void convertToLower(char str[]) {


for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
}

int main() {
char str[100];
printf("Enter a string in uppercase: ");
scanf("%s", str);

convertToLower(str);

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

return 0;
}
```
===================================================================================
========
### Question 13
a. Program to find the distance traveled by an object:
```c
#include <stdio.h>

double calculateDistance(double initialVelocity, double acceleration, double time)


{
return initialVelocity * time + 0.5 * acceleration * time * time;
}

int main() {
double initialVelocity, acceleration, time;

printf("Enter initial velocity, acceleration, and time:\n");


scanf("%lf %lf %lf", &initialVelocity, &acceleration, &time);

double distance = calculateDistance(initialVelocity, acceleration, time);

printf("Distance traveled: %.2lf\n", distance);

return 0;
}
```
===================================================================================
============
b. Program to find string length and reverse a string without using built-in string
functions:
```c
#include <stdio.h>

int calculateStringLength(char str[]) {


int length = 0;

while (str[length] != '\0') {


length++;
}

return length;
}

void reverseString(char str[]) {


int length = calculateStringLength(str);

for (int i = 0; i < length / 2; i++) {


char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

int length = calculateStringLength(str);

printf("Length of the string: %d\n", length);

reverseString(str);

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

return 0;
}
```
==========================================================================
### Question 14
a. Program to convert Fahrenheit to Celsius:
```c
#include <stdio.h>

double convertFahrenheitToCelsius(double fahrenheit) {


return (fahrenheit - 32) * 5 / 9.0;
}

int main() {
double fahrenheit;

printf("Enter temperature in Fahrenheit: ");


scanf("%lf", &fahrenheit);

double celsius = convertFahrenheitToCelsius(fahrenheit);

printf("Temperature in Celsius: %.2lf\n", celsius);

return 0;
}
```
===================================================================================
b. Program to print up to N using recursion:
```c
#include <stdio.h>

void printNumbers(int n) {
if (n >= 1) {
printNumbers(n - 1);
printf("%d ", n);
}
}

int main() {
int N;
printf("Enter a positive integer N: ");
scanf("%d", &N);

printf("Numbers up to %d: ", N);


printNumbers(N);
printf("\n");

return 0;
}
```

===================================================================================
=======
### Question 15
a. Program to compare two strings using built-in functions and concatenate two
strings using built-in functions:
```c
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100], result[200];

printf("Enter the first string: ");


scanf("%s", str1);

printf("Enter the second string: ");


scanf("%s", str2);

// Compare two strings


if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");

// Concatenate two strings


strcpy(result, str1); // Copy the first string into the result
strcat(result, str2); // Concatenate the second string to the result

printf("Concatenated string: %s\n", result);

return 0;
}
```

b. Program to find the factorial of a given integer using recursion:


```c
#include <stdio.h>

int calculateFactorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * calculateFactorial(n - 1);
}

int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);

if (num < 0)
printf("Invalid input. Please enter a non-negative integer.\n");
else
printf("Factorial of %d: %d\n", num, calculateFactorial(num));

return 0;
}
```
========================================THE
END==============================================

You might also like