C Practical Work
C Practical Work
ANSWER 1:
#include<stdio.h>
int main(){
float celsius,fahrenheit;
scanf("%f",&celsius);
fahrenheit=(celsius*9/5)+32;
return 0;
ANSWER 2:
#include <stdio.h>
int main() {
else
printf("%.2lf is the largest number.", n3);
return 0;
}
ANSWER 3:
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
}
}
printf("%d is a prime number.\n", num);
return 0;
}
ANSWER 3:
#include<stdio.h>
int main () {
int i,j,c,n;
c = 1;
printf("enter number of terms : ");
scanf("%d", &n);
}
printf("\n");
}
}
ANSWER 4:
#include <stdio.h>
int main() {
int marks[50];
int i;
float sum = 0;
sum += marks[i];
}
return 0;
}
OUTPUT:
ANSWER 5:
#include <stdio.h>
int main() {
if (arr[i] == key) {
found = 1;
break;
}
if (found) {
printf("\nThe number %d is found at index %d.\n", key, i);
} else {
printf("\nThe number %d is not found in the array.\n", key);
return 0;
}
ANSWER 6:
) #include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s is a palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
return 0;
}
ANSWER 7:
#include <stdio.h>
int main() {
int num1, num2;
int *ptr1, *ptr2;
ptr1 = &num1;
ptr2 = &num2;
return 0;
}
ANSWER 8:
#include <stdio.h>
#include <string.h>
#define NUM_EMPLOYEES 10
char name[50];
int age;
char address[100];
char department[50];
float salary;
};
int main() {
struct Employee employees[NUM_EMPLOYEES];
int i, searchId, found = 0;
inputEmployeeData(&employees[i]);
}
scanf("%d", &searchId);
displayEmployeeData(employees[i]);
found = 1;
break;
}
if (!found) {
printf("Employee with ID %d not found.\n", searchId);
return 0;
}
OUTPUT:
ANSWER 9:
#include <stdio.h>
int main() {
return 1;
}
printf("Enter 20 numbers:\n");
// Close files
fclose(evenFile);
fclose(oddFile);
return 0;
OUTPUT:
ANSWER 10:
#include <stdio.h>
int main() {
char operator;
double num1, num2;
switch (operator) {
case '+':
case '*':
printf("%.1lf * %.1lf = %.1lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 == 0) {
printf("Error: Division by zero!\n");
} else {
printf("%.1lf / %.1lf = %.1lf\n", num1, num2, num1 / num2);
}
break;
case 'a':
printf("Average of %.1lf and %.1lf is %.1lf\n", num1, num2, (num1 + num2) / 2);
break;
case 'p':
printf("%.1lf%% of %.1lf is %.1lf\n", num1, num2, (num1 / 100) * num2);
break;
default:
return 0;
}
ANSWER 11:
(i) #include <stdio.h>
#include <math.h>
int main() {
int n, i, num, originalNum, remainder, result = 0, count = 0;
scanf("%d", &n);
num = i;
result = 0;
while (num != 0) {
remainder = num % 10;
result += pow(remainder, count);
num /= 10;
}
if (result == originalNum) {
printf("%d ", originalNum);
}
}
printf("\n");
return 0;
}
(ii) #include <stdio.h>
int main() {
int n, i, j, isPrime;
}
}
if (isPrime) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
(iii) #include <stdio.h>
int main() {
int n, reverse = 0, remainder, original;
original = n;
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
if (original % 10 == 0) {
printf("Reversed number = %d", reverse);
while (original % 10 == 0) {
printf("0");
original /= 10;
}
} else {
printf("Reversed number = %d", reverse);
return 0;
}
ANSWER 13:
#include <stdio.h>
#include <math.h>
int main() {
int decimalNum, i, remainder, binaryNum[100], j;
scanf("%x", &decimalNum);
// Convert hexadecimal to binary
j = 0;
j++;
}
return 0;
}
ANSWER 14:
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
void fibonacci(int n) {
int a = 0, b = 1, c;
for (int i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
printf("\n");
int main() {
int num, n;
fibonacci(n);
return 0;
}
ANSWER 15:
(i) #include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
}
}
return 0;
OUTPUT:
int main() {
int m, n, p, q, i, j, k;
if (n != p) {
return 1;
scanf("%d", &A[i][j]);
scanf("%d", &B[i][j]);
C[i][j] = 0;
}
printf("\n");
} OUTPUT:
return 0;
}
(iii) #include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
transpose[j][i] = a[i][j];
}
// printing the transpose
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
}
return 0;
}
OUTPUT:
ANSWER 16:
#include <stdio.h>
#include <string.h>
};
int main() {
// Create an array of structure variables
struct Student students[3];
class1.num_students = 3;
// Input student details for the array inside the class structure
for (int i = 0; i < class1.num_students; i++) {
printf("Marks: ");
scanf("%f", &class1.students[i].marks);
}
return 0;
}
ANSWER 17:
#include <stdio.h>
#include <stdlib.h>
/* Function declaration */
int compareFile(FILE * fPtr1, FILE * fPtr2, int * line, int * col);
int main()
{
/* File pointer to hold reference of input file */
FILE * fPtr1;
FILE * fPtr2;
char path1[100];
char path2[100];
int diff;
scanf("%s", path1);
printf("Enter path of second file: ");
scanf("%s", path2);
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
if (diff == 0)
{
printf("\nBoth files are equal.");
}
else
{
printf("\nFiles are not equal.\n");
printf("Line: %d, col: %d\n", line, col);
}
fclose(fPtr2);
return 0;
}
/**
* Function to compare two files.
{
char ch1, ch2;
*line = 1;
*col = 0;
do
{
// Input character from both files
ch1 = fgetc(fPtr1);
ch2 = fgetc(fPtr2);
// Increment line
if (ch1 == '\n')
{
*line += 1;
*col = 0;
}
// If characters are not same then return -1
if (ch1 != ch2)
return -1;
*col += 1;
OUTPUT:
ANSWER 18:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
int numbers[] = {10, 20, 30, 40, 50}; // Sample data to write into the file
int read_num;
return 1;
}
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
OUTPUT:
ANSWER 19:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int choice, n;
do {
printf("\nMenu:\n");
printf("1. Calculate length of a string\n");
printf("2. Concatenate at the end of a given string\n");
printf("3. Copy one string to another\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a string: ");
scanf("%s", str1);
printf("Length of the string: %lu\n", strlen(str1));
break;
case 2:
break;
case 3:
printf("Enter the source string: ");
scanf("%s", str1);
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
break;
case 4:
break;
case 5:
printf("Enter the source string: ");
scanf("%s", str1);
str2[0] = str1[n - 1]; // Copy the nth character from str1 to str2
str2[1] = '\0'; // Null-terminate the destination string
printf("Copied character: %s\n", str2);
break;
case 6:
printf("Exiting the program.\n");
break;
default:
return 0;
}
ANSWER 20:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char time_str[10];
int hours, minutes, seconds;
printf("Invalid time.\n");
return 1;
}