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

2023-24

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%

31st October, 2023 (For July Session)

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

2) To check whether a given number is even or odd


s:

3) To check whether a given number is a positive number or a negative number


tp

4) Quit Enter your choice:

Note: Use SWITCH..CASE statement.


ht
Q2. Write the functions in C for the following:

(a) To find the square of any number.

in
(b) To find the absolute difference between two integers.

.
co
(c) To convert a decimal number to its equivalent binary number.

(d) To find the largest element in a given array of n elements.

e.
nc
Q3. Write an interactive program using recursion for each of the following:

(a) To count the digits of a given number.

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.

(b) To compare two strings without using string library functions.


ea

(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

Note: Assumptions can be made wherever necessary and mention them.


Q7. Using File Handling concept in C programming, write the C programs for the following:

in
(a) To find the number of lines in a text file.

.
co
(b) To delete specific line from a text file.

(c) To copy a file to another folder with a different file-name.

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

Enter your choice:


ea

Note: Use SWITCH..CASE statement.


//l

Solution:

Algorithm:
s:

Display main menu:


tp

1. Check if two integers are equal


ht

2. Check if a number is even or odd


3. Check if a number is positive or negative

4. Quit

in
Enter your choice:

.
co
Get user input choice

e.
Switch (choice) {

nc
Case 1:

Prompt user to enter two integers

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:

Prompt user to enter a number


tp

num = the input number


ht

If num % 2 == 0

Then print "The number is even"


Else

Print "The number is odd"

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

Print "Invalid option"


s:

}
tp

Repeat entire process again


ht

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

sc No Print "num is Odd"


ng
Stop
False
ni

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

No Print "num is Zero"

Print "Exit the


Case 4: Quit True
Program"

False

Print "Invalid
Default True
Option"
Interactive Program in C:

#include <stdio.h>

. in
co
int main() {

int choice, num1, num2, num;

e.
nc
do {

// Display the menu

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

// Execute the corresponding function based on the user's choice


s:

switch(choice) {
tp

case 1:
ht

// Accept two integers

printf("\n Enter two integers: ");


scanf("%d %d", &num1, &num2);

in
// Check if the integers are equal

.
co
if(num1 == num2) {

printf("\n The two integers are equal\n");

e.
} else {

nc
printf("\n The two integers are not equal\n");

ie
break;

g sc
case 2:

in
// Accept a number

printf("\n Enter a number: ");


rn
scanf("%d", &num);
ea

// Check if the number is even or odd


//l

if(num % 2 == 0) {
s:

printf("\n The number is even\n");


tp

} else {
ht

printf("\n The number is odd\n");

}
break;

in
case 3:

.
co
// Accept a number

printf("\nEnter 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

// Exit the program


ht

printf("\n Exiting the program...\n");

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:

(a) To find the square of any number.

in
(b) To find the absolute difference between two integers.

.
co
(c) To convert a decimal number to its equivalent binary number.

(d) To find the largest element in a given array of n elements.

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

printf("\n Enter a number: ");


//l

scanf("%d", &num);

printf("\n The square of %d is %d\n", num, square(num));


s:

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() {

int num1, num2;


//l

printf("\n Enter two numbers: ");


s:

scanf("%d %d", &num1, &num2);


tp

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("\n The binary equivalent is: ");


tp

for(int j = i - 1; j >= 0; j--) {


ht

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) {

int max = arr[0];

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;

printf("\n Enter the size of the array: ");


//l

scanf("%d", &n);
s:

int arr[n];
tp

printf("\n Enter %d elements in the array: ", n);


ht

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

scanf("%d", &arr[i]);
}

printf("\n The largest element in the array is %d\n", largestElement(arr, n));

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

(a) To count the digits of a given number.

(b) To reverse a string


//l

(c) To find the least-common-multiple of two numbers.


s:

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

printf("\n Enter a number: ");

scanf("%d", &num);
//l

printf("\n The number of digits in %d is %d\n", num, countDigits(num));


s:

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

if(start >= end) {


ea

return;
//l

}
s:

// recursive case

char temp = str[start];


tp

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);

// call the reverseString function

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

int num1, num2;

printf("\n Enter two numbers: ");


//l

scanf("%d %d", &num1, &num2);


s:

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.

(b) To compare two strings without using 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

// Get input from user for a string


printf("\n Enter a string: ");

scanf("%s", str);

in.
co
// Calculate the length of the string

for(i = 0; str[i] != '\0'; i++) {

e.
length++;

nc
}

// Print the length of the string

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() {

char str1[100], str2[100];

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

printf("\n Enter the second string: ");


rn
scanf("%s", str2);
ea

// Compare the two strings character by character


//l

for(i = 0; str1[i] != '\0' || str2[i] != '\0'; i++) {


s:

if(str1[i] != str2[i]) {
tp

flag = 1;
ht

break;

}
}

in
// Print the result of comparison

.
co
if(flag == 0) {

printf("\n The two strings are equal.\n");

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

for(i = 0; str[i] != '\0'; i++) {


rn
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
ea

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

printf("\n Number of vowels in the string: %d\n", vowels);

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

// Function to print the elements of an array

void print_array(int arr[], int size) {


int i;

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() {

int arr[100], size, i, element;


rn
ea

// Get input from user for the size of the array

printf("\n Enter the size of the array: ");


//l

scanf("%d", &size);
s:
tp

// Get input from user for the elements of the array


ht

printf("\n Enter the elements of the array: ");


for(i = 0; i < size; i++) {

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 the updated array


s:

printf("\n Updated array:\n");


tp

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

Note: Assumptions can be made wherever necessary and mention them.

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);

printf("\n ID: ");


rn
scanf("%d", &s[i].id);
ea

printf("\n Marks: ");

scanf("%f", &s[i].marks);
//l
s:

if(s[i].marks >= 90) {


tp

s[i].grade = 'A';
ht

} else if(s[i].marks >= 80) {

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';

} else if(s[i].marks >= 50) {

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

for(i = 0; i < 10; i++) {


s:

printf("| %-15s | %-10d | %-5.2f |\n", s[i].name, s[i].id, s[i].marks);


tp

}
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++) {

printf("| %-15s | %-10c |\n", s[i].name, s[i].grade);

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

(a) To find the number of lines in a text file.

(b) To delete specific line from a text file.


(c) To copy a file to another folder with a different file-name.

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:

while((ch = getc(fp)) != EOF) {


tp

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

Screenshot of Program’s Output:


//l
s:
tp
ht
(b) C Program to delete specific line from a text file:

#include <stdio.h>

in.
co
int main() {

FILE *fp, *temp;

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) {

printf("\n File does not exist.");


rn
return 0;
ea

}
//l

while((ch = getc(fp)) != EOF) {


s:

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

Screenshot of txt file before deletion:


s:
tp
ht
in.
co
e.
Screenshot of Program’s Output:

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() {

FILE *fp1, *fp2;

e.
char ch;

nc
char source_file[] = "example.txt";

char dest_file[] = "C:/Users/IGNOU/Desktop/new_example.txt";

ie
sc
fp1 = fopen(source_file, "r");

g
in
if(fp1 == NULL) {

printf("\n File does not exist.");


rn
return 0;
ea

}
//l

fp2 = fopen(dest_file, "w");


s:
tp

if(fp2 == NULL) {
ht

printf("\n Cannot create file at the specified location.");

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:

Screenshot of directory after running the program:


tp
ht
ht
tp
s:
//l
ea
rn
in
gsc
ie
nc
e.
co
. in
. in
co
e.
nc
For more solved assignments, WhatsApp me @ 7980608289

ie
sc
Learning Science

g
https://learningscience.co.in

in Thank You
rn
ea
//l
s:
tp
ht

You might also like