Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Answers of 27 Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

C Programming Solutions

November 20, 2023

Anonymous
1. write a program that takes a character as input and converts it into its corresponding
ASCII value
2. Write a program that takes a distance in kilometer as input and converts it into meter
and centimeter, then display the results?
3. Your program should take the student’s marks as input and determine their
corresponding grade according to the following grading scale:
a)If the student’s marks are greater than or equal to 90, assign a grade ’A’.
b) If the student’s marks are between 80 and 89 (inclusive), assign a grade ’B’.
c) If the student’s marks are between 70 and 79 (inclusive), assign a grade ’C’.
d) If the student’s marks are between 60 and 69 (inclusive), assign a grade ’D’.
e) If the student’s marks are less than 60, assign a grade ’F’.
4. Find the sum of individual digits of a positive integer and find the reverse of the given
number.
5. Generate the first n terms of Fibonacci sequence.
6. Write a C program to Searching an element in an array?
7. Print the multiplication table of a given number n up to a given value, where n is entered
by the user?
8. Write a C Program to print below triangle star patterns?
9. Write a C Program to perform multiplication of two Matrices?
10. Write a C Program to Transpose of a matrix?
11. Write a C Program of (nCr) and (nPr) of the given numbers?
12. Write a C Program to Replace a character of string either from beginning or ending or
at a specified location?
13. Write a C Program to swap to numbers using call by reference?
14. Write a C program to Generate all the prime numbers between 1 and n, where n is a
value supplied by the user?
15. Write a program Find the roots of a quadratic equation?
16. Takes two integer operands and one operator form the user, performs the operation
and then prints the result.
17. Swap two numbers without using a temporary variable?
18. Write a C Program for Decimal number to binary conversion?

19. Write a C Program to Interchange the largest and smallest numbers in the array?
20. Check whether the given number is Armstrong number or not?
21. Write a C program to perform addition of Two Matrices?
22. Write a C Program Function to find both the largest and smallest number of an array of
integers.
23. Write a C Program to perform linear search using functions?
24. Write a C Program 1+x/1!+x2 \2!+x3 \3!+x4 \4!+………..Xn \n! ?
25. Write a C Program to perform linear search using functions?
26. Write a C program to print the Fibonacci series using recursion?
27. Write a C program to print the sum of Natural Numbers using recursion?
number it and give title and wait after 5 questions

ChatGPT

1. ASCII Converter
c

Copy code
#include <stdio.h>
int main() {
char input;
printf("Enter a character: ");
scanf("%c", &input);
printf("ASCII value: %d\n", input);
return 0;
}

2. Distance Converter
c

Copy code

#include <stdio.h>
int main() {
double distanceKm, distanceMeter, distanceCentimeter;
printf("Enter distance in kilometers: ");
scanf("%lf", &distanceKm);

distanceMeter = distanceKm * 1000;


distanceCentimeter = distanceKm * 100000;
printf("Distance in meters: %.2lf\n", distanceMeter);
printf("Distance in centimeters: %.2lf\n", distanceCentimeter);
return 0;
}

3. Grade Determiner
c

Copy code

#include <stdio.h>
int main() {
int marks;
printf("Enter student’s marks: ");
scanf("%d", &marks);
if (marks >= 90)
printf("Grade: A\n");
else if (marks >= 80)
printf("Grade: B\n");
else if (marks >= 70)
printf("Grade: C\n");
else if (marks >= 60)
printf("Grade: D\n");
else
printf("Grade: F\n");
return 0;
}

4. Sum and Reverse of Digits


c

Copy code
#include <stdio.h>
int main() {
int num, sum = 0, digit, originalNum;
printf("Enter a positive integer: ");
scanf("%d", &num);
originalNum = num;
while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
// Reversing the number
printf("Reverse of %d: ", originalNum);
while (originalNum > 0) {
digit = originalNum % 10;
printf("%d", digit);
originalNum /= 10;
}
return 0;
}
I’ll wait for your confirmation before proceeding with more questions.

Anonymous
Continue

ChatGPT

5. Fibonacci Sequence
c

Copy code

#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}

6. Search in Array
c

Copy code

#include <stdio.h>
int searchElement(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target)
return i;
}
return -1; // Element not found
}

int main() {
int size, target;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &target);
int result = searchElement(arr, size, target);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

7. Multiplication Table
c

Copy code

#include <stdio.h>
int main() {
int n, limit;
printf("Enter the number for multiplication table: ");
scanf("%d", &n);

scanf("%d", &n);
printf("Enter the limit: ");
scanf("%d", &limit);
printf("Multiplication table for %d:\n", n);
for (int i = 1; i <= limit; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}

8. Triangle Star Patterns


c

Copy code

#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
// Pattern 1
printf("Pattern 1:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
// Pattern 2
printf("Pattern 2:\n");
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
printf("* ");

printf("* ");
printf("\n");
}
return 0;
}
I’ll wait for your confirmation before proceeding further.

Anonymous
Continue

ChatGPT

9. Matrix Multiplication
c

Copy code

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10

void multiplyMatrices(int first[MAX_ROWS][MAX_COLS], int second[MAX_ROWS


for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
result[i][j] = 0;
}
}
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < colSecond; ++j) {
for (int k = 0; k < colFirst; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

int main() {
int rowFirst, colFirst, rowSecond, colSecond;
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &rowFirst, &colFirst);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &rowSecond, &colSecond);
if (colFirst != rowSecond) {

printf("Error! Column of the first matrix not equal to row of th


return 0;
}

int first[MAX_ROWS][MAX_COLS], second[MAX_ROWS][MAX_COLS], result[MA


printf("\nEnter elements of matrix 1:\n");
for (int i = 0; i < rowFirst; ++i)
for (int j = 0; j < colFirst; ++j)
scanf("%d", &first[i][j]);
printf("\nEnter elements of matrix 2:\n");
for (int i = 0; i < rowSecond; ++i)
for (int j = 0; j < colSecond; ++j)
scanf("%d", &second[i][j]);

multiplyMatrices(first, second, result, rowFirst, colFirst, rowSecon


printf("\nResultant matrix:\n");
for (int i = 0; i < rowFirst; ++i)
for (int j = 0; j < colSecond; ++j) {
printf("%d

", result[i][j]);

if (j == colSecond - 1)
printf("\n");
}

return 0;
}

10. Matrix Transposition


c

Copy code

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10

void transposeMatrix(int matrix[MAX_ROWS][MAX_COLS], int transposed[MAX_


for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
transposed[j][i] = matrix[i][j];
}
int main() {
int rows, cols;
printf("Enter rows and columns for the matrix: ");
scanf("%d %d", &rows, &cols);
int matrix[MAX_ROWS][MAX_COLS], transposed[MAX_ROWS][MAX_COLS];
printf("\nEnter elements of the matrix:\n");
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
scanf("%d", &matrix[i][j]);
transposeMatrix(matrix, transposed, rows, cols);
printf("\nOriginal matrix:\n");
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j) {

for (int j = 0; j < cols; ++j) {


printf("%d

", matrix[i][j]);

if (j == cols - 1)
printf("\n");
}
printf("\nTransposed matrix:\n");
for (int i = 0; i < cols; ++i)
for (int j = 0; j < rows; ++j) {
printf("%d

", transposed[i][j]);

if (j == rows - 1)
printf("\n");
}
return 0;
}
I’ll wait for your confirmation before proceeding further.

Anonymous
Continue

ChatGPT

11. Combination and Permutation


c

Copy code

#include <stdio.h>
// Function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
// Function to calculate nCr (combination)

// Function to calculate nCr (combination)


int nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
// Function to calculate nPr (permutation)
int nPr(int n, int r) {
return factorial(n) / factorial(n - r);
}
int main() {
int n, r;
printf("Enter values for n and r: ");
scanf("%d %d", &n, &r);
if (n < r) {
printf("Error! n should be greater than or equal to r.\n");
return 0;
}
printf("nCr = %d\n", nCr(n, r));
printf("nPr = %d\n", nPr(n, r));
return 0;
}

12. Character Replacement in String


c

Copy code

#include <stdio.h>
void replaceChar(char str[], char oldChar, char newChar) {
for (int i = 0; str[i] != ’\0’; ++i) {
if (str[i] == oldChar)
str[i] = newChar;

str[i] = newChar;
}
}
int main() {
char str[100], oldChar, newChar;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter the character to replace: ");
scanf(" %c", &oldChar);
printf("Enter the new character: ");
scanf(" %c", &newChar);
replaceChar(str, oldChar, newChar);
printf("String after replacement: %s\n", str);
return 0;
}

13. Swap Two Numbers using Call by Reference


c

Copy code

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
I’ll wait for your confirmation before proceeding further.

Anonymous
Continue

ChatGPT

14. Prime Numbers Generator


c

Copy code

#include <stdio.h>
int isPrime(int num) {
if (num <= 1)
return 0; // Not prime
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0)
return 0; // Not prime
}
return 1; // Prime
}

int main() {
int n;
printf("Enter a number (n): ");
scanf("%d", &n);
printf("Prime numbers between 1 and %d:\n", n);
for (int i = 2; i <= n; ++i) {
if (isPrime(i))
printf("%d ", i);
}
return 0;
}

15. Quadratic Equation Solver


c

Copy code

#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2;
printf("Enter coefficients (a, b, c) of the quadratic equation: "
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);

} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root = %.2lf\n", root1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
return 0;
}

16. Arithmetic Operation


c

Copy code

#include <stdio.h>
int main() {
int num1, num2;
char operator;
printf("Enter two integer operands: ");
scanf("%d %d", &num1, &num2);
printf("Enter the operator (+, -, *, /): ");
scanf(" %c", &operator);
switch (operator) {
case ’+’:
printf("Result: %d\n", num1 + num2);
break;
case ’-’:
printf("Result: %d\n", num1 - num2);

printf("Result: %d\n", num1 - num2);


break;
case ’*’:
printf("Result: %d\n", num1 * num2);
break;
case ’/’:
if (num2 != 0)
printf("Result: %.2lf\n", (double)num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default:
printf("Invalid operator.\n");
}
return 0;
}
I’ll wait for your confirmation before proceeding further.
Anonymous
Continue

ChatGPT

17. Swap Two Numbers without Temporary Variable


c

Copy code

#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1 - num2;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

18. Decimal to Binary Conversion


c

Copy code

#include <stdio.h>
void decToBinary(int n) {
if (n == 0)
return;
decToBinary(n / 2);
printf("%d", n % 2);
}
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary equivalent: ");
decToBinary(decimal);
return 0;
}

19. Interchange Largest and Smallest Numbers in Array


c

Copy code

#include <stdio.h>
void interchangeMinMax(int arr[], int size) {
int maxIndex = 0, minIndex = 0;
for (int i = 1; i < size; ++i) {
if (arr[i] > arr[maxIndex])
maxIndex = i;
if (arr[i] < arr[minIndex])
minIndex = i;
}
int temp = arr[maxIndex];
arr[maxIndex] = arr[minIndex];
arr[minIndex] = temp;
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; ++i)
scanf("%d", &arr[i]);
interchangeMinMax(arr, size);
printf("Array after interchange: ");
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);

printf("%d ", arr[i]);


return 0;
}

20. Armstrong Number Checker


c

Copy code

#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int originalNum = num, n = 0, result = 0;
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
int digit = originalNum % 10;
result += pow(digit, n);
originalNum /= 10;
}
return result == num;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (isArmstrong(number))
printf("%d is an Armstrong number.\n", number);
else
printf("%d is not an Armstrong number.\n", number);
return 0;
}
I’ll wait for your confirmation before proceeding further.

Anonymous
Continue

ChatGPT

21. Matrix Addition


c

Copy code

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10

void addMatrices(int first[MAX_ROWS][MAX_COLS], int second[MAX_ROWS][MAX


for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
result[i][j] = first[i][j] + second[i][j];
}
int main() {
int rows, cols;
printf("Enter rows and columns for the matrices: ");
scanf("%d %d", &rows, &cols);

int first[MAX_ROWS][MAX_COLS], second[MAX_ROWS][MAX_COLS], result[MA

printf("\nEnter elements of the first matrix:\n");


for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
scanf("%d", &first[i][j]);
printf("\nEnter elements of the second matrix:\n");
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
scanf("%d", &second[i][j]);
addMatrices(first, second, result, rows, cols);
printf("\nResultant matrix after addition:\n");
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j) {
printf("%d

", result[i][j]);

if (j == cols - 1)
printf("\n");
}
return 0;
}

22. Largest and Smallest Numbers in Array


c
Copy code

#include <stdio.h>

void findLargestSmallest(int arr[], int size, int *largest, int *smalles


*largest = *smallest = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > *largest)
*largest = arr[i];
if (arr[i] < *smallest)

if (arr[i] < *smallest)


*smallest = arr[i];
}
}
int main() {
int size, largest, smallest;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; ++i)
scanf("%d", &arr[i]);
findLargestSmallest(arr, size, &largest, &smallest);
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);
return 0;
}

23. Linear Search in Array using Functions


c

Copy code

#include <stdio.h>
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; ++i) {
if (arr[i] == target)
return i;
}
return -1; // Element not found
}

}
int main() {
int size, target;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; ++i)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &target);
int result = linearSearch(arr, size, target);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

24. Series Sum Calculation


c

Copy code

#include <stdio.h>
double seriesSum(int x, int n) {
double sum = 1.0;
int term = 1, fact = 1;

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


term *= x;
fact *= i;
sum += (double)term / fact;
}
return sum;
}
int main() {
int x, n;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the number of terms (n): ");
scanf("%d", &n);
double result = seriesSum(x, n);
printf("Result of the series: %.4lf\n", result);
return 0;
}
I’ll wait for your confirmation before proceeding further.

Anonymous
Continue

ChatGPT

25. Linear Search in Array using Recursion


c

#include <stdio.h>

Copy code

int linearSearchRecursive(int arr[], int start, int end, int target)


if (start > end)
return -1; // Element not found
if (arr[start] == target)
return start;
return linearSearchRecursive(arr, start + 1, end, target);
}
int main() {
int size, target;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; ++i)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &target);
int result = linearSearchRecursive(arr, 0, size - 1, target);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

26. Fibonacci Series using Recursion


c

Copy code

#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series:\n");
for (int i = 0; i < n; ++i)
printf("%d ", fibonacci(i));
return 0;
}

27. Sum of Natural Numbers using Recursion


c

Copy code

#include <stdio.h>
int sumOfNaturalNumbers(int n) {
if (n == 0)
return 0;
return n + sumOfNaturalNumbers(n - 1);
}

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

printf("Sum of first %d natural numbers: %d\n", n, sumOfNaturalNumbe


return 0;
}
These cover the additional questions you requested. If you have any specific topics or
types of programs you’d like more examples on, feel free to let me know!

Report content|Terms of use|Privacy policy

You might also like