0% found this document useful (0 votes)
11 views23 pages

Ass All Done

cse assainment

Uploaded by

7074mahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views23 pages

Ass All Done

cse assainment

Uploaded by

7074mahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

NO1: Question: Write a C Program to Find Largest Number Among

Three Numbers.
Answar:
3 int main()
4 {
5 int num1, num2, num3;
6
7 printf("Enter three numbers: ");
8 scanf("%d %d %d", &num1, &num2, &num3);
9
10 // Using nested if statements to find the largest number
11 if (num1 >= num2 && num1 >= num3)
12 {
13 printf("%d is the largest number.", num1);
14 }
15 else if (num2 >= num1 && num2 >= num3)
16 {
17 printf("%d is the largest number.", num2);
18 }
19 else
20 {
21 printf("%d is the largest number.", num3);
22 }
23
24 return
25 0;
26 }
NO 2: Question: Write a C Program to Reverse a Number.
Answar:

3
4 int main()
5 {
6 int number, reversedNumber = 0, remainder;
7
8 printf("Enter a number: ");
9 scanf("%d", &number);
10
11 while (number != 0)
12 {
13 remainder = number % 10;
14
15 // Get the last digit
16 reversedNumber = reversedNumber * 10 + remainder; // Append the
17 last digit to the reversed number
18 number /= 10; // Remove the last digit from the original number 19
}
20
21 printf("Reversed number: %d\n", reversedNumber);
22
23 return 0;
24 }
NO 3: Question: Write a C Program to Reverse a Number at Even Indexes
up to N Terms.
Answer:
3 int main()
4 {
5 int n, number, reversedNumber = 0, remainder;
6
7 printf("Enter the number of terms: ");
8 scanf("%d", &n);
9
10 for (int i = 1; i <= n; i++)
11 {
12 printf("Enter number %d: ", i);
13 scanf("%d", &number);
14
15 // Reverse the number only if the index is even
16 if (i % 2 == 0)
17 {
18 reversedNumber = 0;
19 while (number != 0)
20 {
21 remainder = number % 10;
22 reversedNumber = reversedNumber * 10 + remainder;
23 number /= 10;
24 }
25 }
26
27 printf("Reversed number %d: %d\n", i, reversedNumber); 28
}
29
30 return 0;
31 }
NO 4: Question: Write a C Program to Find LCM of Two Numbers (input
from user).
Answer:

3
4 int main()
5 {
6 int num1, num2, max, lcm = 1;
7
8 printf("Enter two numbers: ");
9 scanf("%d %d", &num1, &num2);
10
11 // Find the maximum of the two numbers
12 max = num1 > num2 ? num1 : num2;
13
14 // Iterate until lcm is found
15 while (1)
16 {
17 if (max % num1 == 0 && max % num2 == 0)
18 {
19 lcm = max;
20 break;
21 }
22 max++;
23 }
24
25 printf("LCM of %d and %d is %d\n", num1, num2, lcm);
26
27 return 0;
28 }
NO 5: Question: Write a C Program to Find the Largest Element in an
Array.
Answer:

3
4 int main()
5 {
6 int n, i, largest;
7
8 printf("Enter the number of elements: ");
9 scanf("%d", &n);
10
11 int arr[n];
12
13 printf("Enter %d elements:\n", n);
14 for (i = 0; i < n; i++)
15 {
16 scanf("%d", &arr[i]);
17 }
18
19 // Initialize the largest element to the first element
20 largest = arr[0];
21
22 // Iterate through the array and compare each element with the largest
23 for (i = 1; i < n; i++)
24 {
25 if (arr[i] > largest)
26 {
27 largest = arr[i];
28 }
29 }
30
31 printf("The largest element in the array is: %d\n", largest);
32
33 return 0;
34 }
NO 6: Question: Write a C Program to Sort the Elements of an Array in
Ascending and Descending Order.
Answer:

3 void swap(int *a, int *b)


4 {
5 int temp = *a;
6 *a = *b;
7 *b = temp;
8 }
9
10 void bubbleSortAscending(int arr[], int n)
11 {
12 int i, j;
13 for (i = 0; i < n - 1; i++)
14 {
15 for (j = 0; j < n - i - 1; j++)
16 {
17 if (arr[j] > arr[j + 1])
18 {
19 swap(&arr[j], &arr[j + 1]);
20 }
21 }
22 }
23 }
24
25 void bubbleSortDescending(int arr[], int n)
26 {
27 int i, j;
28 for (i = 0; i < n - 1; i++)
29 {
30 for (j = 0; j < n - i - 1; j++)
31 {
32 if (arr[j] < arr[j + 1])
33 {
34 swap(&arr[j], &arr[j + 1]);
35
36
37 }
38 }
39 }
40 }
41
42 int main()
43 {
44 int n, i;
45
46 printf("Enter the number of elements: ");
47 scanf("%d", &n);
48
49 int arr[n];
50
51 printf("Enter %d elements:\n", n);
52 for (i = 0; i < n; i++)
53 {
54 scanf("%d", &arr[i]);
55
56
57 }
58
59 // Sort in ascending order
60 bubbleSortAscending(arr, n);
61 printf("Array sorted in ascending order: ");

62 for (i = 0; i < n; i++)


63 {
64 printf("%d ", arr[i]);
65 }
66 printf("\n");
69 bubbleSortDescending(arr, n);
70 printf("Array sorted in descending order: ");
71 for (i = 0; i < n; i++)
72 {
73 printf("%d ", arr[i]);
74 }
75
76 return 0;
77 }
NO 7: Question: Write a C Program to Find the Length of a String.
Answer:

3
4 int main()
5 {
6 char str[100];
7
8 printf("Enter a string: ");
9 scanf("%s", str);
10
11 // Using strlen() function to find the length
12 int length = strlen(str);
13
14 printf("Length of the string: %d\n", length);
15
16 return 0;
17 }
NO 8: Question: Write a program in C to show the simple structure of
a function.
Answer:

3 // Function declaration
4 int add(int a, int b);
5
6 int main()
7 {
8 int num1, num2, sum;
9
10 printf("Enter two numbers: ");
11 scanf("%d %d", &num1, &num2);
12
13 // Function call
14 sum = add(num1, num2);
15
16 printf("Sum of %d and %d is %d\n", num1, num2, sum);
17
18 return 0;
19 }
20
21 // Function definition
22 int add(int a, int b)
23 {
24 int result = a + b;
25 return result;
26 }
NO 9: Question: 10. Write a program in C to swap two numbers using a
function.
Answer:

3 void swap(int *a, int *b)


4 {
5 int temp = *a;
6 *a = *b;
7 *b = temp;
8 }
9
10 int main()
11 {
12 int num1, num2;
13
14 printf("Enter two numbers: ");
15 scanf("%d %d", &num1, &num2);
16
17 printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
18
19 // Function call to swap the numbers
20 swap(&num1, &num2);
21
22 printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
23
24 return 0;
25 }
NO 10: Question: 11. Write a program in C to check whether a number
is a prime number or not using the
function.
Answer:

3 int isPrime(int num)


4 {
5 int i;
6
7 // 0 and 1 are not prime numbers
8 if (num <= 1)
9 {
10 return 0;
11 }
12
13 // Check divisibility up to the square root of num
14 for (i = 2; i * i <= num; i++)
15 {
16 if (num % i == 0)
17 {
18 return 0; // Not a prime number
19 }
20 }
21
22 return 1; // Prime number
23 }
24
25 int main()
26 {
27 int num;
28
29 printf("Enter a number: ");
30 scanf("%d", &num);
31
32 if (isPrime(num))
33 {
34 printf("%d is a prime number.\n", num);
35 }
36 else
37 {
38 printf("%d is not a prime number.\n", num); 39
}
40
41 return 0;
42 }
NO 11: Question: Write a C program to find the position of a target
value within a sorted array using binary
search.
Answer:

3
4 int binarySearch(int arr[], int low, int high, int target) 5
{
6 if (high >= low)
7 {
8 int mid = low + (high - low) / 2;
9
10 // If found at mid
11 if (arr[mid] == target)
12 {
13 return mid;
14 }
15
16 // If target is greater than mid
17 if (arr[mid] < target)
18 {
19 return binarySearch(arr, mid + 1, high, target);
20 }
21
22 // If target is smaller than mid
23 return binarySearch(arr, low, mid - 1, target); 24
}
25
26 // Target not found
27 return -1;
28 }
29
30 int main()
31 {
32 int n, i, target;
33
34 printf("Enter the number of elements: ");
35 scanf("%d", &n);
36
37 int arr[n];
38
39 printf("Enter %d elements in sorted order:\n", n); 40
for (i = 0; i < n; i++)
41 {
42 scanf("%d", &arr[i]);
43 }
44
45 printf("Enter the target value: ");
46 scanf("%d", &target);
47
48 int result = binarySearch(arr, 0, n - 1, target);
49
50 if (result == -1)
51 {
52 printf("Target value not found.\n");
53 }
54 else
55 {
56 printf("Target value found at index %d.\n", result);
57 }
58
59 return 0;
60 }
NO 12: Question: Write a C Program to Create a Temporary File
Answer:

3 #include <stdlib.h>
4
5 int main()
6 {
7 FILE *tempFile;
8 char tempName[] = "tempfile.txt";
9
10 // Create a temporary file
11 tempFile = fopen(tempName, "w");
12 if (tempFile == NULL)
13 {
14 printf("Error creating temporary file.\n");
15 return 1;
16 }
17
18 // Write some data to the temporary file
19 fprintf(tempFile, "This is temporary data.\n");
20
21 // Close the temporary file
22 fclose(tempFile);
23
24 printf("Temporary file created successfully: %s\n", tempName);
25
26 return 0;
27 }
NO 13: Question: Write a C Program to Read/Write Structure to a File
Answer:

3 #include <stdlib.h>
4
5 struct Student
6 {
7 int rollNo;
8 char name[50];
9 float marks;
10 };
11
12 int main()
13 {
14 struct Student student;
15 FILE *file;
16
17 // Open the file in write mode
18 file = fopen("student.txt", "w");
19 if (file == NULL)
20 {
21 printf("Error opening file.\n");
22 return 1;
23 }
24
25 // Read student data from the user
26 printf("Enter student details:\n");
27 printf("Roll No: ");
28 scanf("%d", &student.rollNo);
29 printf("Name: ");
30 scanf("%s", student.name);
31 printf("Marks: ");
32 scanf("%f", &student.marks);
33
34 // Write the student data to the file
35 fwrite(&student, sizeof(student), 1, file);

36
37 // Close the file
38 fclose(file);
39
40 // Open the file in read mode
41 file = fopen("student.txt", "r");
42 if (file == NULL)
43 {
44 printf("Error opening file.\n");
45 return 1;
46 }
47
48 // Read the student data from the file
49 fread(&student, sizeof(student), 1, file);
50
51 // Print the student data
52 printf("\nStudent details:\n");
53 printf("Roll No: %d\n", student.rollNo);
54 printf("Name: %s\n", student.name);
55 printf("Marks: %.2f\n", student.marks);
56
57 // Close the file
58 fclose(file);
59
60 return 0;
61 }
NO 14: Question: Write a C Program to Rename a file
Answer:

1
2 #include <stdio.h>
3 #include <stdlib.h> 4
5 int main() {
6 char old_name[100], new_name[100]; 7
8 printf("Enter the old name of the file: ");
9 scanf("%s", old_name); 10
11 printf("Enter the new name for the file: ");
12 scanf("%s", new_name); 13
14 if (rename(old_name, new_name) == 0) {
15 printf("File renamed successfully.\n");
16 } else {
17 printf("Error renaming file.\n"); 18 }
19
20 return 0; 21 }
NO 15: Question: Write a C Program to Make a File Read-Only
Answer:

3 #include <stdlib.h>
4
5 int main()
6 {
7 FILE *file;
8 char filename[] = "example.txt";
9
10 // Open the file in read-only mode
11 file = fopen(filename, "r");
12 if (file == NULL)
13 {
14 printf("Error opening file.\n");
15 return 1;
16 }
17
18 // Make the file read-only
19 if (chmod(filename, S_IRUSR | S_IRGRP | S_IROTH) != 0)
20 {
21 printf("Error making file read-only.\n");
22 fclose(file);
23 return 1;
24 }
25
26 printf("File made read-only.\n");
27
28 // Close the file
29 fclose(file);
30
31 return 0;
32 }
NO 16: Question: Write a C program to Compare Two Files and Report
Mismatches
Answer:

3 #include <stdlib.h>
4
5 int main()
6 {
7 FILE *file1, *file2;
8 char ch1, ch2;
9 int lineNumber = 1, mismatchCount = 0;
10
11 // Open the files
12 file1 = fopen("file1.txt", "r");
13 if (file1 == NULL)
14 {
15 printf("Error opening file1.\n");
16 return 1;
17 }
18 file2 = fopen("file2.txt", "r");
19 if (file2 == NULL)
20 {
21 printf("Error opening file2.\n");
22 fclose(file1);
23 return 1;
24 }
25
26 // Compare the files character by character
27 while ((ch1 = fgetc(file1)) != EOF && (ch2 = fgetc(file2)) != EOF)
28 {
29 if (ch1 != ch2)
30 {
31 printf("Mismatch found on line %d.\n", lineNumber); 32
mismatchCount++;
33 }
34 if (ch1 == '\n')
35 {
36 lineNumber++;
37 }
38 }
39
40 // Check if the files have different lengths
41 if (ch1 != EOF || ch2 != EOF)
42 {
43 printf("Files have different lengths.\n");
44 mismatchCount++;
45 }
46
47 // Close the files
48 fclose(file1);
49 fclose(file2);
50
51 printf("Total mismatches found: %d\n", mismatchCount);
52
53 return 0;
54 }
NO 17: Question: Write a C Program to Copy One File into Another File
Answer:

3 #include <stdlib.h>
4
5 int main()
6 {
7 FILE *sourceFile, *targetFile;
8 char ch;
9
10 // Open the source file in read mode
11 sourceFile = fopen("source.txt", "r");
12 if (sourceFile == NULL)
13 {
14 printf("Error opening source file.\n");
15 return 1;
16 }
17
18 // Open the target file in write mode
19 targetFile = fopen("target.txt", "w");
20 if (targetFile == NULL)
21 {
22 printf("Error creating target file.\n");
23 fclose(sourceFile);
24 return 1;
25 }
26
27 // Copy characters from the source file to the target file
28 while ((ch = fgetc(sourceFile)) != EOF)
29 {
30 fputc(ch, targetFile);
31 }
32
33 // Close the files
34 fclose(sourceFile);
35 fclose(targetFile);
36
37 printf("File copied successfully.\n");
38
39 return 0;
40 }

You might also like