Professional Documents
Culture Documents
MCS-011 Solved Assignment 2023-24 - Protected
MCS-011 Solved Assignment 2023-24 - Protected
Learning Science
in
o.c
e.
c
en
ci
gs
in
MCS-011
n
ar
e
Solved Assignment
//l
s:
https://learningscience.co.in
tp
ht
MCS-011 Solved Assignment 2023-2024 (July-January)
in
Course Code MCS-011
.
Course Title Problem Solving and Programming
co
Assignment Number BCA (II)/011/Assignment/2023-24
e.
Maximum Marks 100
nc
Weightage 25%
ie
Last Date of Submission
30 th April, 2024 (For January Session)
sc
Note: There are seven questions in this assignment, which carry 80 marks. Rest 20 marks are for viva-voce. Answer all the questions. You may use
g
illustrations and diagrams to enhance the explanations. Please go through the guidelines regarding assignments given in the Programme Guide for
in
the format of presentation. rn
Q 1. Write an algorithm, draw corresponding flow chart and write an interactive program which prompts the user with the following options on the
ea
opening menu:
1) To accept two integers and check whether they are equal or not
//l
in
(b) To find the absolute difference between two integers.
.
co
(c) To convert a decimal number to its equivalent binary number.
e.
nc
Q3. Write an interactive program using recursion for each of the following:
ie
(b) To reverse a string
sc
(c) To find the least-common-multiple of two numbers.
g
in
Q4. Write interactive C programs to perform the following on strings:
rn
(a) To find the length of a given string without using the string library functions.
(c)To count the total number of vowels and consonants in a string and display the counts separately.
//l
s:
Q5. Write an interactive C program to insert new elements in the unsorted array.
tp
Q6. Using Structures in C, write an interactive program to display the mark-sheet and grade card for 10 students for a MOOC course.
ht
in
(a) To find the number of lines in a text file.
.
co
(b) To delete specific line from a text file.
e.
nc
-: Solution :-
ie
Q 1. Write an algorithm, draw corresponding flow chart and write an interactive program which prompts the user with the following options on the
opening menu:
sc
1) To accept two integers and check whether they are equal or not
g
2) To check whether a given number is even or odd
in
3) To check whether a given number is a positive number or a negative number
rn
4) Quit
Solution:
Algorithm:
s:
4. Quit
in
Enter your choice:
.
co
Get user input choice
e.
Switch (choice) {
nc
Case 1:
ie
num1 = first integer
sc
num2 = second integer
g
If num1 == num2
in
Then print "The two numbers are equal"
Else
rn
Print "The two numbers are not equal"
ea
EndIf
//l
Case 2:
s:
If num % 2 == 0
in
EndIf
.
co
Case 3:
e.
Prompt user to enter a number
nc
num = the input number
If num > 0
ie
Then print "The number is positive"
sc
Else
g
Print "The number is negative"
in
EndIf
Case 4:
rn
Exit the program
ea
Default:
//l
}
tp
Flowchart:
Start
Switch Conditional
Expression
Yes
Print "num1 and
num2 are equal"
Case 1: If
If two integers True Read num1, num2 num1==num2
are equal
.in
No
Print "num1 and
num2 are not equal"
co
False
e.
Yes Print "num is Even"
nc
Case 2: If If
ie
a number is even True Read num num%2==0
or odd
Yes
Print "num is
Positive"
r
ea
Case 3: If a
number is positive True Read num If num > 0 Print "num is
Yes
or negative Negative"
//l
s:
No If num < 0
tp
False
ht
False
Print "Invalid
Default True
Option"
Interactive Program in C:
#include <stdio.h>
. in
co
int main() {
e.
nc
do {
ie
printf("\n *************** Menu *************** \n");
sc
printf("\n 1. Check for equality of two integers\n");
g
printf(" 2. Check for even or odd number\n");
in
printf(" 3. Check for positive or negative number\n");
printf(" 4. Quit\n");
rn
printf("\n Enter your choice: ");
ea
scanf("%d", &choice);
//l
switch(choice) {
tp
case 1:
ht
in
// Check if the integers are equal
.
co
if(num1 == num2) {
e.
} else {
nc
printf("\n The two integers are not equal\n");
ie
break;
g sc
case 2:
in
// Accept a number
if(num % 2 == 0) {
s:
} else {
ht
}
break;
in
case 3:
.
co
// Accept a number
e.
scanf("%d", &num);
nc
// Check if the number is positive or negative
ie
if(num > 0) {
sc
printf("\n The number is positive\n");
g
} else if(num < 0) {
in
printf("\n The number is negative\n");
} else {
rn
printf("\n The number is zero\n");
ea
break;
//l
s:
case 4:
tp
return 0;
default:
in
printf("\n Invalid choice. Please try again.\n");
.
co
}
} while(1);
e.
nc
return 0;
ie
Screenshot of Outputs:
g sc
in
rn
ea
//l
s:
tp
ht
ht
tp
s:
//l
ea
rn
in
gsc
ie
nc
e.
co
. in
Q2. Write the functions in C for the following:
in
(b) To find the absolute difference between two integers.
.
co
(c) To convert a decimal number to its equivalent binary number.
e.
Solution:
nc
(a) C Program to find the square of any number:
ie
#include <stdio.h>
sc
int square(int num) {
g
return num * num;
in
} rn
int main() {
int num;
ea
scanf("%d", &num);
return 0;
tp
}
ht
Screenshot of Output:
. in
co
e.
nc
(b) C Program to find the absolute difference between two integers:
#include <stdio.h>
ie
#include <stdlib.h> // for abs() function
sc
int absoluteDifference(int num1, int num2) {
g
return abs(num1 - num2);
}
in
rn
ea
int main() {
printf("\n The absolute difference between %d and %d is %d\n", num1, num2, absoluteDifference(num1, num2));
return 0;
ht
}
Screenshot of Output:
in.
co
e.
nc
ie
(c) C Program to convert a decimal number to its equivalent binary number:
sc
#include <stdio.h>
g
void decimalToBinary(int num) {
int binary[32], i = 0;
in
rn
while(num > 0) {
ea
binary[i] = num % 2;
num = num / 2;
//l
i++;
s:
printf("%d", binary[j]);
}
printf("\n");
in
}
.
co
int main() {
e.
int num;
nc
printf("\n Enter a decimal number: ");
scanf("%d", &num);
ie
decimalToBinary(num);
sc
return 0;
g
}
in
Screenshot of Output: rn
ea
//l
s:
tp
ht
(d) C Program to find the largest element in a given array of n elements:
#include <stdio.h>
in.
co
int largestElement(int arr[], int n) {
e.
for(int i = 1; i < n; i++) {
nc
if(arr[i] > max) {
max = arr[i];
ie
}
sc
}
g
return max;
in
} rn
int main() {
ea
int n;
scanf("%d", &n);
s:
int arr[n];
tp
scanf("%d", &arr[i]);
}
in
return 0;
.
co
}
Screenshot of Output:
e.
nc
ie
g sc
in
rn
Q3. Write an interactive program using recursion for each of the following:
ea
Solution:
tp
ht
(a) C Program to count the digits of a given number:
#include <stdio.h>
. in
co
int countDigits(int num) {
// base case
e.
if(num == 0) {
nc
return 0;
ie
// recursive case
sc
return 1 + countDigits(num / 10);
g
}
int main() {
in
rn
int num;
ea
scanf("%d", &num);
//l
return 0;
tp
}
ht
Screenshot of Output:
in.
co
e.
nc
(b) C Program to reverse a string:
ie
#include <stdio.h>
sc
#include <string.h>
g
in
void reverseString(char str[], int start, int end) {
rn
// base case
return;
//l
}
s:
// recursive case
str[start] = str[end];
ht
str[end] = temp;
reverseString(str, start+1, end-1);
in.
co
int main() {
char str[100];
e.
printf("\n Enter a string: ");
nc
scanf("%s", str);
ie
reverseString(str, 0, strlen(str)-1);
sc
printf("\n The reversed string is: %s\n", str);
g
return 0;
in
} rn
Screenshot of Output:
ea
//l
s:
tp
ht
(c) C Program to find the least-common-multiple of two numbers:
#include <stdio.h>
in
int gcd(int num1, int num2) {
.
co
if(num2 == 0) {
return num1;
e.
} else {
nc
return gcd(num2, num1 % num2);
ie
}
sc
int lcm(int num1, int num2) {
g
return (num1 * num2) / gcd(num1, num2);
in
} rn
int main() {
ea
printf("\n The least common multiple of %d and %d is %d\n", num1, num2, lcm(num1, num2));
tp
return 0;
ht
}
Screenshot of Output:
. in
co
e.
nc
ie
Q4. Write interactive C programs to perform the following on strings:
sc
(a) To find the length of a given string without using the string library functions.
g
in
(c) To count the total number of vowels and consonants in a string and display the counts separately.
Solution:
rn
(a) C Program to find the length of a given string without using the string library functions:
ea
#include <stdio.h>
//l
int main() {
s:
char str[100];
tp
int length = 0, i;
ht
scanf("%s", str);
in.
co
// Calculate the length of the string
e.
length++;
nc
}
ie
printf("\n Length of the string: %d\n", length);
g sc
return 0;
in
} rn
Screenshot of Output:
ea
//l
s:
tp
ht
(b) C Program to compare two strings without using string library functions:
#include <stdio.h>
in.
co
int main() {
e.
int i, flag = 0;
nc
// Get input from user for first string
ie
printf("\n Enter the first string: ");
sc
scanf("%s", str1);
g
in
// Get input from user for second string
if(str1[i] != str2[i]) {
tp
flag = 1;
ht
break;
}
}
in
// Print the result of comparison
.
co
if(flag == 0) {
e.
} else {
nc
printf("\n The two strings are not equal.\n");
ie
sc
return 0;
g
}
in
Screenshot of Output: rn
ea
//l
s:
tp
ht
(c) C Program to count the total number of vowels and consonants in a string and display the counts separately:
#include <stdio.h>
. in
co
int main() {
char str[100];
e.
int i, vowels = 0, consonants = 0;
nc
// Get input from user for a string
ie
printf("\n Enter a string: ");
sc
scanf("%s", str);
g
in
// Count the number of vowels and consonants in the string
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
vowels++;
//l
} else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
s:
consonants++;
tp
}
ht
}
// Print the counts of vowels and consonants in the string
in
printf("\n Number of consonants in the string: %d\n", consonants);
.
co
return 0;
e.
}
nc
Screenshot of Output:
ie
g sc
in
rn
ea
Q5. Write an interactive C program to insert new elements in the unsorted array.
//l
Solution:
s:
#include <stdio.h>
tp
ht
in
printf("\n Array elements: ");
.
co
for(i = 0; i < size; i++) {
e.
printf("%d ", arr[i]);
nc
}
ie
printf("\n");
sc
}
g
in
int main() {
scanf("%d", &size);
s:
tp
scanf("%d", &arr[i]);
in
}
.
co
// Print the original array
e.
printf("\n Original array:\n");
nc
print_array(arr, size);
ie
// Get input from user for the new element to be inserted
sc
printf("\n\n Enter the new element to be inserted: ");
g
scanf("%d", &element);
in
// Insert the new element at the end of the array
rn
arr[size] = element;
ea
size++;
//l
print_array(arr, size);
ht
return 0;
}
Screenshot of Output:
. in
co
e.
nc
ie
g sc
in
rn
Q6. Using Structures in C, write an interactive program to display the mark-sheet and grade card for 10 students for a MOOC course.
ea
Solution:
//l
#include <stdio.h>
s:
struct student {
tp
char name[50];
ht
int id;
float marks;
char grade;
in
};
.
co
int main() {
e.
struct student s[10];
nc
int i;
ie
for(i = 0; i < 10; i++) {
sc
printf("\n Enter the details for student %d:\n", i+1);
g
printf("\n Name: ");
in
scanf("%s", s[i].name);
scanf("%f", &s[i].marks);
//l
s:
s[i].grade = 'A';
ht
s[i].grade = 'B';
} else if(s[i].marks >= 70) {
s[i].grade = 'C';
in
} else if(s[i].marks >= 60) {
.
co
s[i].grade = 'D';
e.
s[i].grade = 'E';
nc
} else {
s[i].grade = 'F';
ie
}
sc
}
g
in
printf("\n\n Mark-sheet and Grade Card for MOOC Course\n\n");
printf("--------------------------------------------------\n");
rn
printf("| %-15s | %-10s | %-5s |\n", "Name", "ID", "Marks");
ea
printf("--------------------------------------------------\n");
//l
}
ht
printf("--------------------------------------------------\n\n");
printf("\n Grade Card\n\n");
in
printf("--------------------------------------------------\n");
.
co
printf("| %-15s | %-10s |\n", "Name", "Grade");
printf("--------------------------------------------------\n");
e.
nc
for(i = 0; i < 10; i++) {
ie
}
g sc
printf("--------------------------------------------------\n");
return 0;
in
rn
}
ea
//l
s:
tp
ht
Screenshots of Output:
ht
tp
s:
//l
ea
rn
in
gsc
ie
nc
e.
co
. in
ht
tp
s:
//l
ea
rn
in
gsc
ie
nc
e.
co
. in
. in
co
e.
nc
ie
g sc
in
rn
ea
//l
s:
tp
Q7. Using File Handling concept in C programming, write the C programs for the following:
ht
Solution:
in
(a) C Program to find the number of lines in a text file:
.
co
#include <stdio.h>
e.
int main() {
nc
FILE *fp;
char ch;
ie
int lines = 0;
g sc
fp = fopen("example.txt", "r");
in
if(fp == NULL) {
rn
printf("\n File does not exist.");
ea
return 0;
}
//l
s:
if(ch == '\n') {
ht
lines++;
}
}
in
printf("\n Number of lines in the file: %d", lines);
.
co
fclose(fp);
e.
return 0;
nc
}
Note: Replace "example.txt" with the name of the file you want to count the lines for.
ie
Screenshot of File content:
g sc
in
rn
ea
#include <stdio.h>
in.
co
int main() {
e.
char ch;
nc
int line_no = 3, curr_line = 0;
ie
fp = fopen("example.txt", "r");
sc
temp = fopen("temp.txt", "w");
g
in
if(fp == NULL) {
}
//l
if(ch == '\n') {
tp
curr_line++;
ht
if(curr_line != line_no) {
putc(ch, temp);
in
}
.
co
fclose(fp);
e.
fclose(temp);
nc
remove("example.txt");
ie
rename("temp.txt", "example.txt");
g sc
printf("\n Line number %d has been deleted.", line_no);
return 0;
in
rn
}
ea
Note: Replace "example.txt" with the name of the file you want to delete the line from. Change the value of 'line_no' to the line number you want to
delete.
//l
nc
ie
g sc
Screenshot of txt file after running the program (Line no 3 has been deleted):
in
rn
ea
//l
s:
tp
ht
(c) C Program to copy a file to another folder with a different file-name:
#include <stdio.h>
in.
co
int main() {
e.
char ch;
nc
char source_file[] = "example.txt";
ie
sc
fp1 = fopen(source_file, "r");
g
in
if(fp1 == NULL) {
}
//l
if(fp2 == NULL) {
ht
return 0;
}
in
while((ch = getc(fp1)) != EOF) {
.
co
putc(ch, fp2);
e.
nc
fclose(fp1);
fclose(fp2);
ie
sc
printf("\n File copied successfully.");
g
in
return 0;
}
rn
Screenshot of directory before running the program:
ea
//l
s:
tp
ht
in.
co
e.
nc
ie
g sc
in
Screenshot of Program’s Output: rn
ea
//l
s:
ie
sc
Learning Science
g
https://learningscience.co.in
in Thank You
rn
ea
//l
s:
tp
ht