Answers
Answers
=============
### 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];
return 0;
}
=============================================================
b. Program to check if a number is a palindrome:
```c
#include <stdio.h>
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
int main() {
int 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>
start++;
end--;
}
}
int main() {
int size;
int arr[size];
reverseArray(arr, size);
return 0;
}
```
============================================================
b. Program to check if a number is an Armstrong number:
```c
#include <stdio.h>
#include <math.h>
while (original != 0) {
original /= 10;
n++;
}
original = num;
while (original != 0) {
remainder = original % 10;
sum += pow(remainder, n);
original /= 10;
}
int main() {
int 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 main() {
int 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 main() {
int size, element;
int arr[size];
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>
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
```
===================================================================================
=====
#include <stdio.h>
int main() {
int size, min, max;
int arr[size];
return 0;
}
=============================================================================
### Question 5
a. Program to check if the given year is a leap year or not:
```c
#include <stdio.h>
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;
}
```
int main() {
int size, index, newValue;
int arr[size];
return 0;
}
```
### Question 6
a. Program to simulate a calculator using switch case:
```c
#include <stdio.h>
int main() {
char operator;
double 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;
}
```
int main() {
int size;
int arr[size];
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;
return 0;
}
```
int main() {
int size;
int arr[size];
bubbleSort(arr, size);
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;
int max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2
: num3);
return 0;
}
```================================================================================
==============================
int main() {
int rows, cols;
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;
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>
int main() {
int rows1, cols1, rows2, cols2;
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>
int main() {
int rows, cols;
printf("Enter the dimensions of the matrix (rows and columns): ");
scanf("%d %d", &rows, &cols);
return 0;
}
```
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++=====
### Question 11
a. Program to evaluate the given expressions:
```c
#include <stdio.h>
int main() {
int a, b, c, d, i, j;
return 0;
}
```
===================================================================================
=========================
b. Program to convert a string from lowercase to uppercase without using built-in
functions:
```c
#include <stdio.h>
int main() {
char str[100];
convertToUpper(str);
return 0;
}
```
===================================================================================
======================
### Question 12
a. Program to calculate simple interest and compound interest:
```c
#include <stdio.h>
#include <math.h>
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);
return 0;
}
===================================================================================
=======================
b. Program to convert a string from uppercase to lowercase without using built-in
functions:
```c
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string in uppercase: ");
scanf("%s", str);
convertToLower(str);
return 0;
}
```
===================================================================================
========
### Question 13
a. Program to find the distance traveled by an object:
```c
#include <stdio.h>
int main() {
double initialVelocity, acceleration, time;
return 0;
}
```
===================================================================================
============
b. Program to find string length and reverse a string without using built-in string
functions:
```c
#include <stdio.h>
return length;
}
int main() {
char str[100];
reverseString(str);
return 0;
}
```
==========================================================================
### Question 14
a. Program to convert Fahrenheit to Celsius:
```c
#include <stdio.h>
int main() {
double fahrenheit;
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);
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];
return 0;
}
```
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==============================================