for ONLY questions follow this link (link)
1. a)Write a program for display values reverse order from an array using a pointer.
ANSWER:
#include <stdio.h>
int main() {
// Define an array
int arr[] = {1, 2, 3, 4, 5};
// Calculate the length of the array
int length = sizeof(arr) / sizeof(arr[0]);
// Initialize a pointer to the last element of the array
int *ptr = arr + length - 1;
// Display values in reverse order using the pointer
for (int i = 0; i < length; ++i) {
printf("%d ", *ptr);
--ptr;
}
return 0;
}
1.b) Write a C program to construct a pyramid of numbers as follows:
*
* *
* * *
ANSWER:
#include <stdio.h>
int main() {
int rows;
// Input the number of rows for the pyramid
printf("Enter the number of rows for the pyramid: ");
scanf("%d", &rows);
// Construct the pyramid
for (int i = 1; i <= rows; ++i) {
// Print spaces before the stars
for (int j = 1; j <= rows - i; ++j) {
printf(" ");
}
// Print stars for each row
for (int k = 1; k <= i; ++k) {
printf("* ");
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
2.a) Write a C program to determine if the given string is a palindrome or not (Spelled same in
bothdirections with or without a meaning like madam, civic, noon, abcba, etc.)
ANSWER:
#include <stdio.h>
#include <string.h>
// Function to check if a string is a palindrome
int isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
// Compare characters from both ends
if (str[left] != str[right]) {
return 0; // Not a palindrome
}
left++;
right--;
}
return 1; // Palindrome
}
int main() {
char inputString[100];
// Input the string
printf("Enter a string: ");
gets(inputString);
// Check if the string is a palindrome
if (isPalindrome(inputString)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
2.b) Write a program through a pointer variable to sum of n elements from an array.
ANSWER:
#include <stdio.h>
// Function to calculate the sum of n elements using a pointer
int sumOfElements(int *arr, int n) {
int sum = 0;
// Use a pointer to iterate through the array and calculate the sum
for (int i = 0; i < n; ++i) {
sum += *(arr + i);
}
return sum;
}
int main() {
int n;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
// Calculate the sum using the sumOfElements function
int result = sumOfElements(arr, n);
// Display the sum
printf("Sum of the elements: %d\n", result);
return 0;
}
3. a)Write a program for finding the max and min from the three numbers.
ANSWER:
PROGRAM 1:FINDING MAX. AND MINI.
#include <stdio.h>
// Function to find the maximum of three numbers
int findMax(int a, int b, int c) {
int max = (a > b) ? a : b;
max = (max > c) ? max : c;
return max;
}
// Function to find the minimum of three numbers
int findMin(int a, int b, int c) {
int min = (a < b) ? a : b;
min = (min < c) ? min : c;
return min;
}
int main() {
int num1, num2, num3;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Find and display the maximum and minimum
printf("Maximum: %d\n", findMax(num1, num2, num3));
printf("Minimum: %d\n", findMin(num1, num2, num3));
return 0;
}
3.b) Write a program for reading elements using a pointer into an array and display the values using the
array.
ANSWER:
#include <stdio.h>
int main() {
int n;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
// Declare an array of size n
int array[n];
// Pointer to iterate through the array
int *ptr = array;
// Input elements into the array using pointers
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; ++i) {
scanf("%d", ptr + i);
}
// Display the elements using the array
printf("Elements in the array:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", array[i]);
}
return 0;
}
4.a) Write a program that finds if a given number is a prime number.
ANSWER:
#include <stdio.h>
// Function to check if a number is prime
int isPrime(int num) {
if (num <= 1) {
return 0; // Not a prime number
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int number;
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is prime and display the result
if (isPrime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
4,b) Transpose of a matrix with memory dynamically allocated for the new matrix as row and column
counts may not be the same
ANSWER:
#include <stdio.h>
#include <stdlib.h>
// Function to transpose a matrix
void transposeMatrix(int **matrix, int rows, int cols) {
// Dynamically allocate memory for the transposed matrix
int **transposedMatrix = (int **)malloc(cols * sizeof(int *));
for (int i = 0; i < cols; ++i) {
transposedMatrix[i] = (int *)malloc(rows * sizeof(int));
}
// Calculate the transpose
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
transposedMatrix[j][i] = matrix[i][j];
}
}
// Display the original matrix
printf("Original Matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Display the transposed matrix
printf("\nTransposed Matrix:\n");
for (int i = 0; i < cols; ++i) {
for (int j = 0; j < rows; ++j) {
printf("%d ", transposedMatrix[i][j]);
}
printf("\n");
}
// Free dynamically allocated memory for the transposed matrix
for (int i = 0; i < cols; ++i) {
free(transposedMatrix[i]);
}
free(transposedMatrix);
}
int main() {
int rows, cols;
// Input the number of rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Dynamically allocate memory for the matrix
int **matrix = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; ++i) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Input elements of the matrix
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
scanf("%d", &matrix[i][j]);
}
}
// Call the function to transpose the matrix
transposeMatrix(matrix, rows, cols);
// Free dynamically allocated memory for the matrix
for (int i = 0; i < rows; ++i) {
free(matrix[i]);
}
free(matrix);
return 0;
}
5.a) Write a C program to find the sum of individual digits of a positive integer and test given number
is palindrom.
ANSWER:
#include <stdio.h>
// Function to find the sum of individual digits of a number
int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10; // Add the last digit to the sum
num /= 10; // Remove the last digit
}
return sum;
}
// Function to check if a number is a palindrome
int isPalindrome(int num) {
int originalNum = num;
int reversedNum = 0;
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
return (originalNum == reversedNum);
}
int main() {
int number;
// Input a positive integer
printf("Enter a positive integer: ");
scanf("%d", &number);
// Check if the number is positive
if (number <= 0) {
printf("Please enter a positive integer.\n");
return 1; // Exit with an error code
}
// Find and display the sum of individual digits
printf("Sum of individual digits: %d\n", sumOfDigits(number));
// Check and display if the number is a palindrome
if (isPalindrome(number)) {
printf("%d is a palindrome.\n", number);
} else {
printf("%d is not a palindrome.\n", number);
}
return 0;
}
5.b) Write a simple program that converts one given data type to another using auto conversion and
casting. Take the values from standard input
ANSWER:
#include <stdio.h>
int main() {
// Variables to store input values
int integerNumber;
float floatNumber;
// Input an integer from the user
printf("Enter an integer: ");
scanf("%d", &integerNumber);
// Auto conversion to float
floatNumber = integerNumber;
printf("Auto conversion: %d (int) to %f (float)\n", integerNumber, floatNumber);
// Input a float from the user
printf("Enter a float: ");
scanf("%f", &floatNumber);
// Casting to int
integerNumber = (int)floatNumber;
printf("Casting: %f (float) to %d (int)\n", floatNumber, integerNumber);
return 0;
}
6. a)Write a program that prints a multiplication table for a given number and the number of rows inthe
table. For example, for a number 5 and rows = 3, the output should be:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
ANSWER:
#include <stdio.h>
int main() {
int number, rows;
// Input the number and the number of rows
printf("Enter a number: ");
scanf("%d", &number);
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Print the multiplication table
for (int i = 1; i <= rows; ++i) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
6.b)Write a C program to find the Multiplication of Two Matrices
ANSWER:
#include <stdio.h>
#define MAX_SIZE 10
// Function to multiply two matrices
void multiplyMatrices(int firstMatrix[MAX_SIZE][MAX_SIZE], int secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowFirst, int colFirst, int rowSecond, int colSecond) {
// Initializing elements of result matrix to 0
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
result[i][j] = 0;
}
}
// Multiplying firstMatrix and secondMatrix and storing in result
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
for (int k = 0; k < colFirst; ++k) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
// Function to display a matrix
void displayMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int rowFirst, colFirst, rowSecond, colSecond;
// Input dimensions of the first matrix
printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &rowFirst, &colFirst);
// Input elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colFirst; ++j) {
scanf("%d", &firstMatrix[i][j]);
}
}
// Input dimensions of the second matrix
printf("Enter the number of rows and columns of the second matrix: ");
scanf("%d %d", &rowSecond, &colSecond);
// Input elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rowSecond; ++i) {
for (int j = 0; j < colSecond; ++j) {
scanf("%d", &secondMatrix[i][j]);
}
}
// Check if matrices can be multiplied
if (colFirst != rowSecond) {
printf("Matrices cannot be multiplied. Number of columns in the first matrix must be equal to the number of rows in the second matrix.\n");
return 1; // Exit with an error code
}
// Call the function to multiply matrices
multiplyMatrices(firstMatrix, secondMatrix, result, rowFirst, colFirst, rowSecond, colSecond);
// Display the result matrix
printf("\nResultant Matrix:\n");
displayMatrix(result, rowFirst, colSecond);
return 0;
}
7. a)A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the top of the
building. Find the time taken by the ball to reach each floor. (Use the formula s = ut+(1/2)at^2where u
and a are the initial velocity in m/sec (= 0) and acceleration in m/sec^2 (= 9.8 m/s^2)).
ANSWER:
#include <stdio.h>
#include <math.h>
#define ACCELERATION 9.8
int main() {
int numberOfFloors = 10;
double floorHeight = 3.0;
printf("Time taken to reach each floor:\n");
for (int floor = 1; floor <= numberOfFloors; ++floor) {
double time = sqrt(2 * floorHeight * floor / ACCELERATION);
printf("Floor %d: %.2f seconds\n", floor, time);
}
return 0;
}
7.b) Write a C program to construct a pyramid of numbers as follows:
1
1 2
1 2 3
ANSWER:
#include <stdio.h>
int main() {
int rows;
// Input the number of rows for the pyramid
printf("Enter the number of rows for the pyramid: ");
scanf("%d", &rows);
// Construct the pyramid
for (int i = 1; i <= rows; ++i) {
// Print numbers for each row
for (int j = 1; j <= i; ++j) {
printf("%d ", j);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
8. a)Write a C program, which takes two integer operands and one operator from the user, performs the
operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement).
ANSWER:
#include <stdio.h>
int main() {
int operand1, operand2;
char operator;
// Input two integer operands and one operator
printf("Enter two integer operands: ");
scanf("%d %d", &operand1, &operand2);
printf("Enter an operator (+, -, *, /, %%): ");
scanf(" %c", &operator);
// Perform the operation based on the operator
switch (operator) {
case '+':
printf("Result: %d + %d = %d\n", operand1, operand2, operand1 + operand2);
break;
case '-':
printf("Result: %d - %d = %d\n", operand1, operand2, operand1 - operand2);
break;
case '*':
printf("Result: %d * %d = %d\n", operand1, operand2, operand1 * operand2);
break;
case '/':
// Check for division by zero
if (operand2 != 0) {
printf("Result: %d / %d = %.2f\n", operand1, operand2, (float)operand1 / operand2);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case '%':
// Check for modulus by zero
if (operand2 != 0) {
printf("Result: %d %% %d = %d\n", operand1, operand2, operand1 % operand2);
} else {
printf("Error: Modulus by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
break;
}
return 0;
}
8.b) Write a C program to find the Addition of Two Matrices.
ANSWER:
#include <stdio.h>
#define MAX_SIZE 10
// Function to add two matrices
void addMatrices(int firstMatrix[MAX_SIZE][MAX_SIZE], int secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
}
// Function to display a matrix
void displayMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int rows, cols;
// Input dimensions of the matrices
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
// Input elements of the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
scanf("%d", &firstMatrix[i][j]);
}
}
// Input elements of the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
scanf("%d", &secondMatrix[i][j]);
}
}
// Call the function to add matrices
addMatrices(firstMatrix, secondMatrix, result, rows, cols);
// Display the result matrix
printf("\nResultant Matrix (Sum of Two Matrices):\n");
displayMatrix(result, rows, cols);
return 0;
}
9. a)Write the program for the simple, compound interest.
ANSWER:
#include <stdio.h>
#include <math.h>
// Function to calculate simple interest
float calculateSimpleInterest(float principal, float rate, float time) {
return (principal * rate * time) / 100;
}
// Function to calculate compound interest
float calculateCompoundInterest(float principal, float rate, float time) {
return principal * (pow((1 + rate / 100), time) - 1);
}
int main() {
float principal, rate, time;
// Input principal amount, rate of interest, and time period
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time period (in years): ");
scanf("%f", &time);
// Calculate and display simple interest
float simpleInterest = calculateSimpleInterest(principal, rate, time);
printf("\nSimple Interest: %.2f\n", simpleInterest);
// Calculate and display compound interest
float compoundInterest = calculateCompoundInterest(principal, rate, time);
printf("Compound Interest: %.2f\n", compoundInterest);
return 0;
}
9.b) Write a c program to insert a sub-string into a given main string from a given position.
ANSWER:
#include <stdio.h>
#include <string.h>
// Function to insert a sub-string into a main string
void insertSubstring(char mainString[], char subString[], int position) {
int mainStringLength = strlen(mainString);
int subStringLength = strlen(subString);
// Shift characters to make space for the sub-string
for (int i = mainStringLength; i >= position; --i) {
mainString[i + subStringLength] = mainString[i];
}
// Insert the sub-string into the main string
for (int i = 0; i < subStringLength; ++i) {
mainString[position + i] = subString[i];
}
}
int main() {
char mainString[100], subString[50];
int position;
// Input the main string
printf("Enter the main string: ");
gets(mainString);
// Input the sub-string
printf("Enter the sub-string: ");
gets(subString);
// Input the position to insert the sub-string
printf("Enter the position to insert the sub-string: ");
scanf("%d", &position);
// Check if the position is valid
if (position < 0 || position > strlen(mainString)) {
printf("Invalid position. Please enter a valid position.\n");
return 1; // Exit with an error code
}
// Call the function to insert the sub-string
insertSubstring(mainString, subString, position);
// Display the modified main string
printf("Modified String: %s\n", mainString);
return 0;
}
10. a) Write a program that declares Class awarded for a given percentage of marks, where mark =
70% = Distinction. Read percentage from standard input.
ANSWER:
#include <stdio.h>
int main() {
float percentage;
// Input the percentage from the user
printf("Enter the percentage of marks: ");
scanf("%f", &percentage);
// Determine the class based on the percentage
if (percentage >= 70.0) {
printf("Class Awarded: Distinction\n");
} else if (percentage >= 60.0) {
printf("Class Awarded: First Class\n");
} else if (percentage >= 50.0) {
printf("Class Awarded: Second Class\n");
} else if (percentage >= 40.0) {
printf("Class Awarded: Pass Class\n");
} else {
printf("Class Awarded: Fail\n");
}
return 0;
}
10.b) Write a C program to construct a pyramid of numbers as follows:
1
2 2
3 3 3
4 4 4 4
ANSWER:
#include <stdio.h>
int main() {
int rows;
// Input the number of rows for the pyramid
printf("Enter the number of rows for the pyramid: ");
scanf("%d", &rows);
// Construct the pyramid
for (int i = 1; i <= rows; ++i) {
// Print spaces before the numbers
for (int j = 1; j <= rows - i; ++j) {
printf(" ");
}
// Print numbers for each row
for (int k = 1; k <= i; ++k) {
printf("%d ", i);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
11. a)A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and
1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program
to generate the first n terms of the sequence
ANSWER:
#include <stdio.h>
// Function to generate the first n terms of the Fibonacci sequence
void generateFibonacci(int n) {
int firstTerm = 0, secondTerm = 1, nextTerm;
printf("Fibonacci Sequence (first %d terms):\n", n);
for (int i = 0; i < n; ++i) {
printf("%d ", firstTerm);
nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
printf("\n");
}
int main() {
int n;
// Input the number of terms for the Fibonacci sequence
printf("Enter the number of terms for the Fibonacci sequence: ");
scanf("%d", &n);
// Call the function to generate the Fibonacci sequence
generateFibonacci(n);
return 0;
}
11.b) Write a C program to construct a pyramid of numbers as follows.
1
2 3
4 5 6
ANSWER:
#include <stdio.h>
int main() {
int rows, number = 1;
// Input the number of rows for the pyramid
printf("Enter the number of rows for the pyramid: ");
scanf("%d", &rows);
// Construct the pyramid
for (int i = 1; i <= rows; ++i) {
// Print spaces before the numbers
for (int j = 1; j <= rows - i; ++j) {
printf(" ");
}
// Print numbers for each row
for (int k = 1; k <= i; ++k) {
printf("%d ", number++);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
12.a) b) Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
ANSWER:
#include <stdio.h>
// Function to check if a number is prime
int isPrime(int num) {
if (num <= 1) {
return 0; // Not a prime number
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int n;
// Input the value of n from the user
printf("Enter the value of n: ");
scanf("%d", &n);
// Print prime numbers between 1 and n
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 2; i <= n; ++i) {
if (isPrime(i)) {
printf("%d\n", i);
}
}
return 0;
}
12.b) Write a c Program to find the Factorial of a given number.
ANSWER:
#include <stdio.h>
// Function to check if a number is prime
int isPrime(int num) {
if (num <= 1) {
return 0; // Not a prime number
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int n;
// Input the value of n from the user
printf("Enter the value of n: ");
scanf("%d", &n);
// Print prime numbers between 1 and n
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 2; i <= n; ++i) {
if (isPrime(i)) {
printf("%d\n", i);
}
}
return 0;
}
Comments
Post a Comment