MCS-011 Problem Solving and Programming Solve Question paper JUNE-2024 Term-End Exam.
1. (a) Write an algorithm and draw corresponding flowchart to print first n even natural numbers and find their sum.
Answer: Algorithm and Flowchart to Print First n Even Natural Numbers and Their Sum
Algorithm:
1. Start
2.Initialize n
, i = 1
, and sum = 0
3. While i <= n
:
- Calculate
even_number = 2 * i
- Print
even_number
- Add
even_number
tosum
- Increment
i
by 1
4. Print sum
5. End
(b) Define recursion. Explain it with one example of C code segment.
Answer: Definition: Recursion is a programming technique where a function calls itself in order to solve a problem. Each recursive call reduces the problem size untile a base case in reached.
Example in C:
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
int main() {
int num =5;
printf ("Factorial of %d is %D\n", num, factorila (num));
return 0;
}
(c) Write a program to generate the following Pattern:
1
2 3
4 5 6
7 8 9 10
Answer: Pattern Generation Program using C Program
#include <stdio.h>
int main() {
int rows = 4, count = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", count);
count++;
}
printf("\n");
}
return 0;
}
(d) Find the Square and cube of any given number using Macro.
Answer: Square and cude using Macros using C Program.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
int main() {
int num = 5;
printf("Square of %d: %d\n", num, SQUARE(num));
printf("Cube of %d: %d\n", num, CUBE(num));
return 0;
}
(e) Define a structure . How do we declare and use the structure ? Explain with an example.
Answer: Definition: A structure in c is a user user-defined data type that allows grouping of variables of different types under a single name.
Exapmle in c Program
#include <stdio.h>
struct Student {
char name[50];
int roll_no;
float marks;
};
int main() {
struct Student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter roll number: ");
scanf("%d", &s1.roll_no);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("Name: %s, Roll No: %d, Marks: %.2f\n", s1.name, s1.roll_no, s1.marks);
return 0;
}
(f) Explain the folllowing looping constructs in C by giving code segment of each:
(i) While
(ii) For
Answer: (i) While loop Code in C Program:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
(ii) For Loop using C Program
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
2. (a) Explain a conditional operator in C. Write a program in c to find the greater number between two numbers using conditional operator.
Answer: Definition Conditional Operator: the conditional operator (?:) is a ternary operator that takes three opeands. It evaluated a condition and returns one of two values based on whether the condition is true or false.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
int greater = (a > b) ? a : b;
printf("Greater number is: %d\n", greater);
return 0;
}
(b) Write a C program to calculate income tax of an employee based on annual salary:
If salary < 5lakhs : No tax
5 lakhs-8lakhs : 10% tax
8 lakhs- 10 lakhs : 25% tax
salalry > 10 lakhs : 30% tax
Answer: Income tax Calculation Program:
#include <stdio.h>
int main() {
float salary, tax;
printf("Enter annual salary: ");
scanf("%f", &salary);
if (salary < 500000) {
tax = 0;
} else if (salary >= 500000 && salary < 800000) {
tax = (salary - 500000) * 0.10;
} else if (salary >= 800000 && salary < 1000000) {
tax = (300000 * 0.10) + (salary - 800000) * 0.25;
} else {
tax = (300000 * 0.10) + (200000 * 0.25) + (salary - 1000000) * 0.30;
}
printf("Income Tax: %.2f\n", tax);
return 0;
}
(c) Define a function in C. List and Explain various categories of Functions.
Answer: Definition and Categories of Functions in C
Definition: A function in C is a block of code that performs a specific task. It is a reusable piece of code that can be called multiple times.
Categories:
- Standard Library Functions: Functions defined in libraries (e.g.,
printf
,scanf
). - User-defined Functions: Functions created by the user to perform specific tasks.
- Recursive Functions: Functions that call themselves.
- Inline Functions: Functions defined with the
inline
keyword to suggest that the compiler expand the function code inline.
3. (a) Simulate the calculator to perform arithmetic operations add, subtracts, multiplication, division on two variables depending on the choice selected by the user.
Answer: Simple Calculator Program:
#include <stdio.h>
int main() {
float num1, num2;
char operator;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator);
switch (operator) {
case '+':
printf("Result: %.2f\n", num1 + num2);
break;
case '-':
printf("Result: %.2f\n", num1 - num2);
break;
case '*':
printf("Result: %.2f\n", num1 * num2);
break;
case '/':
if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Division by zero error!\n");
break;
default:
printf("Invalid operator!\n");
}
return 0;
}
(b) Write a program to find maximum marks among the marks of 10 students using arrays.
Answer: Maximum Marks Among 10 Students Using Arrays.
#include <stdio.h>
int main() {
int marks[10], max;
printf("Enter marks of 10 students:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &marks[i]);
}
max = marks[0];
for (int i = 1; i < 10; i++) {
if (marks[i] > max) {
max = marks[i];
}
}
printf("Maximum marks: %d\n", max);
return 0;
}
4. (a) Describe the initializations, declaration of ponter, accessing a variable using pointer with the help of an example.
Answer: Definition and Example: A pointer is a variable that stores the address of another variable.
Example code in C.:
#include <stdio.h>
int main() {
int var = 20;
int *ptr = &var; // Declaration of pointer
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", (void*)&var);
printf("Value of ptr: %p\n", (void*)ptr);
printf("Value pointed by ptr: %d\n", *ptr); // Dereferencing
return 0;
}
(b) Differentiate between swquential and random file access. Also write inbuilt functions in C related to file access.
Answer: Sequential vs Random File Access
Sequential File Access:
- Data is read or written in a linear order, from the beginning to the end of the file.
- It is simple and straightforward, making it suitable for tasks like logging.
- Example: Reading a text file line by line.
Random File Access:
- Data can be read or written at any location within the file without needing to process the data sequentially.
- It is more complex but allows for more efficient data retrieval, especially for large files.
- Example: Accessing specific records in a database file.
Inbuilt Functions for File Access in C:
fopen()
: Opens a file and returns a file pointer.fclose()
: Closes an opened file.fread()
: Reads data from a file into an array.fwrite()
: Writes data from an array to a file.fseek()
: Moves the file pointer to a specified location.ftell()
: Returns the current position of the file pointer.fprintf()
: Writes formatted output to a file.fscanf()
: Reads formatted input from a file.
(c) Write a C program to obtain GCD of two numbers.
Answer: C Program to Obtain GCD of Two Numbers.
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));
return 0;
}
5. Write short notes on the following:
(i) Storage classess in C
(ii) Perprocessor directives
(iii) Precedence of operators
(iv) Typecasting
Answer: (i) Storage Classes in C
Storage classes in C define the scope (visibility) and lifetime (duration) of variables. The main types are:
- Automatic: Variables are created and destroyed automatically (default is local). Declared using the
auto
keyword or by default. - External: Variables declared outside any function, accessible throughout the program. Declared using the
extern
keyword. - Static: Retains its value between function calls. Declared using the
static
keyword. - Register: Suggests the compiler to store the variable in a CPU register for faster access. Declared using the
register
keyword.
(ii) Preprocessor Directives
Preprocessor directives are commands in C that are processed before compilation. They begin with the #
symbol and are used for:
- File Inclusion:
#include <filename>
or#include "filename"
for including header files. - Macro Definitions:
#define MACRO_NAME value
to create macros for constants or functions. - Conditional Compilation:
#ifdef
,#ifndef
,#endif
to include/exclude parts of code based on certain conditions.
(iii) Precedence of Operators
Operator precedence determines the order in which different operations are performed in an expression. In C, the order from highest to lowest precedence is:
- Parentheses
()
- Unary operators
++
,--
,+
,-
- Multiplicative operators
*
,/
,%
- Additive operators
+
,-
- Shift operators
<<
,>>
- Relational operators
<
,<=
,>
,>=
- Equality operators
==
,!=
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Logical AND
&&
- Logical OR
||
- Conditional operator
?:
- Assignment operators
=
,+=
,-=
etc.
(iv) Typecasting
Typecasting is the conversion of one data type into another. In C, it can be done explicitly or implicitly:
Explicit Typecasting: Done by the programmer using casting operators (e.g., (float) num
to convert an integer to a float). This is useful when you want to avoid data loss or ensure correct operations.
Implicit Typecasting: Automatically done by the compiler when converting smaller data types to larger ones (e.g., int
to float
).
#include <stdio.h>
int main() {
int a = 5, b = 2;
float result;
// Implicit typecasting
result = a / b; // result will be 2.0 (but integer division, so will be 2.0 in float)
// Explicit typecasting
result = (float)a / b; // result will be 2.5
printf("Result: %.2f\n", result);
return 0;
}
The End
- IGNOU जून 2025 टर्म-एंड परीक्षा डेट शीट (पूर्ण विवरण)
- IGNOU June 2025 Term-End Exam Date Sheet – पूरी जानकारी हिंदी में
- MCS-011 Problem Solving and Programming language 2024-2025
- Title: How to Apply Online for B.Ed and BScN (PB) Entrance Test: A Complete Guide
- MCS-011 DEC-2023 Solve Question Paper FOR BCA/MCA
One comment