C Answers of 27 Questions
C Answers of 27 Questions
C Answers of 27 Questions
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);
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;
}
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;
}
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
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) {
", result[i][j]);
if (j == colSecond - 1)
printf("\n");
}
return 0;
}
Copy code
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
", 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
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)
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;
}
Copy code
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
Anonymous
Continue
ChatGPT
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;
}
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;
}
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);
ChatGPT
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);
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;
}
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]);
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
Copy code
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
", result[i][j]);
if (j == cols - 1)
printf("\n");
}
return 0;
}
#include <stdio.h>
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;
}
Copy code
#include <stdio.h>
double seriesSum(int x, int n) {
double sum = 1.0;
int term = 1, fact = 1;
Anonymous
Continue
ChatGPT
#include <stdio.h>
Copy code
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;
}
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);