mcs011 Solved Guess Paper
mcs011 Solved Guess Paper
com 1
MCS-011 : PROBLEM SOLVING ANDPROGRAMMING
Guess Paper-I
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;
}
Provided by @To9yStark
2 Problem Solvining and Programming (MCS-011)
Q. Write a program read a file and count the number of lines in the file, assuming
that a line can contain at most 80 characters.
Ans. #include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *fp;
int cnt=0;
char str[80];
/* open a file in read mode */
if ((fp=fopen("lines.dat","r"))== NULL)
{ printf("File does not exist\n");
exit(0);
}
/* read the file till end of file is encountered */
while(!(feof(fp)))
{ fgets(str,80,fp); /*reads at most 80 characters in str */
cnt++; /* increment the counter after reading a line */
}
}/* print the number of lines */
printf(“The number of lines in the file is :%d\n”,cnt);
fclose(fp);
}
OUTPUT
Let us assume that the contents of the file “lines.dat” are as follows:
This is C programming.
I love C programming.
Provided by @To9yStark
Shrichakradhar.com 3
my_int = 35;
}
#else
void set_my_int()
{
my_int = 27;
}
#endif
main ()
{
set_my_int();
printf("%d\n", my_int);
}
OUTPUT
27
Provided by @To9yStark
4 Problem Solvining and Programming (MCS-011)
printf(“%-5.*s \n”, y, string); }
main()
{
int x, y;
static char string[ ] = “COBOL”;
printf(“\n”);
LOOP;
}
When the above program is executed the reference to macro (loop) is replaced by the
set of statements contained within the macro definition.
Q. Write a program to find out square and cube of any given number using
macros.
Ans.# include<stdio.h>
Provided by @To9yStark
Shrichakradhar.com 5
# define sqr(x) (x * x)
# define cub(x) (sqr(x) * x)
main()
{
int num;
printf(“Enter a number: ”);
scanf(“%d”, &num);
printf(“ \n Square of the number is %d”, sqr(num));
printf(“ \n Cube of the number is %d\n”, cub(num));
}
OUTPUT
Enter a number: 5
Square of the number is 25
Cube of the number is 125
Provided by @To9yStark
6 Problem Solvining and Programming (MCS-011)
MCS-011 : PROBLEM SOLVING ANDPROGRAMMING
Guess Paper-II
Provided by @To9yStark
Shrichakradhar.com 7
Element no.=2 Value of the element=43
Element no.=3 Value of the element=12
Element no.=4 Value of the element=8
Element no.=5 Value of the element=5
Enter the element to be searched: 8
The number exists in the list at position: 4
Q. Write a program to display the average marks of each student, given the marks
in 2 subjects for 3 students.
Ans. Write a program to display the average marks of each student, given the marks
in 2
subjects for 3 students.
/* Program to display the average marks of 3 students */
# include < stdio.h >
# defineSIZE 3
main()
{
int i = 0;
float stud_marks1[SIZE]; /* subject 1array declaration */
float stud_marks2[SIZE]; /*subject 2 array declaration */
float total_marks[SIZE];
float avg[SIZE];
printf(“\n Enter the marks in subject-1 out of 50 marks: \n”);
for( i = 0;i<SIZE;i++)
{
printf(“Student no. =%d”,i+1);
printf(“ Enter the marks= “);
scanf(“%f”,&stud_marks1[i]);
}
printf(“\n Enter the marks in subject-2 out of 50 marks \n”);
for(i=0;i<SIZE;i++)
{
printf(“Student no. =%d”,i+1);
printf(“ Please enter the marks= “);
scanf(“%f”,&stud_marks2[i]);
}
Provided by @To9yStark
8 Problem Solvining and Programming (MCS-011)
for(i=0;i<SIZE;i++)
{
total_marks[i]=stud_marks1[i]+ stud_marks2[i];
avg[i]=total_marks[i]/2;
printf(“Student no.=%d, Average= %f\n”,i+1, avg[i]);
}
}
OUTPUT
Enter the marks in subject-1out of 50 marks:
Student no. = 1 Enter the marks= 23
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 42
Enter the marks in subject-2 out of 50 marks:
Student no. = 1 Enter the marks= 31
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 40
Student no. = 1 Average= 27.000000
Student no. = 2 Average= 35.000000
Student no. = 3 Average= 41.000000
Provided by @To9yStark
Shrichakradhar.com 9
execution of the body of the loop depending upon the value of the test condition.
This is shown in the figure 5.5 given below:
Provided by @To9yStark
10 Problem Solvining and Programming (MCS-011)
Provided by @To9yStark
Shrichakradhar.com 11
main( )
{
float gross_salary, net_salary;
printf(“Enter gross salary of an employee\n”);
scanf(“%f ”,&gross_salary );
if (gross_salary <10000)
net_salary= gross_salary;
if (gross_salary >= 10000)
net_salary = gross_salary- 0.15*gross_salary;
printf(“\nNet salary is Rs.%.2f\n”, net_salary);
}
If … else statement: If…else statement is used when a different sequence of
instructions is to be executed depending on the logical value (True / False) of the
condition evaluated.
Its form used in conjunction with if and the syntax is as follows:
if (condition)
Statement _1;
else
Statement_ 2;
statement_3;
Or
if (condition)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
Statements _3_Block;
If the condition is true, then the sequence of statements (Statements_1_Block)
executes; otherwise the Statements_2_Block following the else part of if-else
statement will get executed. In both the cases, the control is then transferred to
Statements_3 to follow sequential execution of the program. This is shown in figure
5.2 given below:
Provided by @To9yStark
12 Problem Solvining and Programming (MCS-011)
#include <stdio.h>
main ( )
{
int x;
printf(“Enter a number:\n”);
scanf("%d",&x);
if (x % 2 == 0)
printf(“\nGiven number is even\n”);
else
Provided by @To9yStark
Shrichakradhar.com 13
printf(“\nGiven number is odd\n”);
}
Nested if…else statement:In nested if… else statement, an entire if…else construct is
written within either the body of the if statement or the body of an else statement.
The syntax is as follows:
if (condition_1)
{
if (condition_2)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
}
else
{
Statements_3_Block;
}
Statement_4_Block;
Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and
is followed by the execution of Statements_4_Block, otherwise if condition_1 is true,
then condition_2 is evaluated. Statements_1_Block is executed when condition_2 is
true otherwise Statements_2_Block is executed and then the control is transferred to
Statements_4_Block.
Provided by @To9yStark
14 Problem Solvining and Programming (MCS-011)
condition. If the condition is true then the statement in the body of if statement is
executed else if the condition is false, the statement is not executed. Whether the
body statement is executed or not, after the if structure completes, execution
proceeds with the next statement after the if structure. Conditions in the if structure
are formed with the relational operators which are summarized in the Table.
Relational operators usually appear in statements which are inquiring about the
truth of some particular relationship between variables. Normally, the relational
operators in C are the operators in the expressions that appear between the
parentheses. For example,
(i) if (thisNum < minimumSoFar) minimumSoFar = thisNum
(ii) if (job == Teacher) salary == minimumWage
(iii) if (numberOfLegs != 8) thisBug = insect
(iv) if (degreeOfPolynomial < 2) polynomial = linear
/*Program to find relationship between two numbers*/
#include <stdio.h>
main ( )
{
int a, b;
printf ( “Please enter two integers: ”);
scanf (“%d%d”, &a, &b);
if (a <= b)
printf (“ %d <= %d\n”,a,b);
else
printf (“%d > %d\n”,a,b);
}
Provided by @To9yStark
Shrichakradhar.com 15
MCS-011 : PROBLEM SOLVING ANDPROGRAMMING
Guess Paper-III
Q. Describe variables.
Ans. Variable is an identifier whose value changes from time to time during
execution. It is a named data storage location in your computer’s memory. By using
a variable’s name in your program, you are, in effect, referring to the data stored
there. A variable represents a single data item i.e. a numeric quantity or a character
constant or a string constant. Note that a value must be assigned to the variables at
some point of time in the program which is termed as assignment statement. The
variable can then be accessed later in the program. If the variable is accessed before it
is assigned a value, it may give garbage value. The data type of a variable doesn’t
change whereas the value assigned to can change. All variables have three essential
attributes:
• The name
• The value
• The memory, where the value is stored.
For example, in the following C program a, b, c, d are the variables but variable e is
not declared and is used before declaration. After compiling the source code and
look what gives?
main( )
{
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = ‘a’;
e=d;
……….
……….
}
After compiling the code, this will generate the message that variable e not defined.
Provided by @To9yStark
16 Problem Solvining and Programming (MCS-011)
3. Both upper and lower cases are allowed. Same text of different case is not
equivalent, for example: TEXT is not same as text.
4. Except the special character underscore ( _ ), no other special symbols can be used.
For example, some valid identifiers are shown below:
X
X123
_XI
temp
tax_rate
For example, some invalid identifiers are shown below:
123 First character to be alphabet.
“X.” Not allowed.
order-no Hyphen allowed.
error flag Blankspace allowed.
Keywords
Keywords are reserved words which have standard, predefined meaning in C. They
cannot be used as program-defined identifiers.
The lists of C keywords are as follows:
Provided by @To9yStark
Shrichakradhar.com 17
OUTPUT
The average is 8.
Q5. Write a program to divide a sum of two numbers by their difference
Ans. /* Program to divide a sum of two numbers by their difference*/
#include <stdio.h>
main( )
{
int a,b;
float c;
a=10;
b=10;
c = (a+b) / (a-b);
printf(“The value of the result is %f\n”,c);
}
Provided by @To9yStark
18 Problem Solvining and Programming (MCS-011)
Program
#include <stdio.h>
main()
{
int a,b,sum; /* variables declaration*/
printf(“\n Enter the values for a and b: \n”);
scanf(“%d, %d”, &a, &b);
sum=a+b;
printf("\nThe sum is %d",sum); /*output statement*/
}
OUTPUT
Enter the values of a and b:
23
The sum is 5
Provided by @To9yStark
Shrichakradhar.com 19
application programs to be run on a variety of computers. These languages
are machineindependent. Languages falling in this category are FORTRAN,
BASIC, PASCAL etc. They are easy to learn and programs may be written in
these languages with much less effort. However, the computer cannot
understand them and they need to be translated into machine language with
the help of other programs known as Compilers or Translators.
Q. Using structures, write a C program to calculate the Gross salary and Net
salary, if Basic pay, Grade pay, TA and DA and other allowances and deductions
are given as inputs.
Ans. #include
#include
#include
/* structure to store employee salary details */
struct employee {
int empId;
char name[32];
int basic, hra, da, ma;
int pf, insurance;
float gross, net;
};
printf(“\nDeductions: \n”);
printf(“Provident fund: %d\n”, e1.pf);
printf(“Insurance: %d\n”, e1.insurance);
printf(“\nNet Salary: %.2f Rupees\n\n”, e1.net);
return;
}
int main() {
Provided by @To9yStark
20 Problem Solvining and Programming (MCS-011)
int i, ch, num, flag, empID;
struct employee *e1;
/* get the number of employees from the user */
printf(“Enter the number of employees:”);
scanf(“%d”, &num);
/* dynamically allocate memory to store employee salary details */
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
/* get the employee salary details from the customer */
printf(“Enter your input for every employee:\n”);
for (i = 0; i < num; i++) {
printf(“Employee ID:”);
scanf(“%d”, &(e1[i].empId));
getchar();
printf(“Employee Name:”);
fgets(e1[i].name, 32, stdin);
e1[i].name[strlen(e1[i].name) – 1] = ‘\0’;
printf(“Basic Salary, HRA:”);
scanf(“%d%d”, &(e1[i].basic), &(e1[i].hra));
printf(“DA, Medical Allowance:”);
scanf(“%d%d”, &(e1[i].da), &(e1[i].ma));
printf(“PF and Insurance:”);
scanf(“%d%d”, &(e1[i].pf), &(e1[i].insurance));
printf(“\n”);
}
/* gross and net salary calculation */
for (i = 0; i < num; i++) {
e1[i].gross = e1[i].basic +
(e1[i].hra * e1[i].basic) / 100 +
(e1[i].da * e1[i].basic) / 100 +
(e1[i].ma * e1[i].basic) / 100;
e1[i].net = e1[i].gross – (e1[i].pf + e1[i].insurance);
}
/* printing payslip for the given employee ID */
while (1) {
printf(“Enter employee ID to get payslip:”);
scanf(“%d”, &empID);
flag = 0;
for (i = 0; i < num; i++) {
if (empID == e1[i].empId) {
printSalary(e1[i]);
Provided by @To9yStark
Shrichakradhar.com 21
flag = 1;
}
}
if (!flag) {
printf(“No Record Found!!\n”);
}
printf(“Do You Want To Continue(1/0):”);
scanf(“%d”, &ch);
if (!ch) {
break;
}
}
return 0;
}
/**
* C program to calculate gross salary of an employee
*/
#include <stdio.h>
int main()
{
float basic, gross, da, hra;
/* Input basic salary of employee */
printf("Enter basic salary of an employee: ");
scanf("%f", &basic);
/* Calculate D.A and H.R.A according to specified conditions */
if(basic <= 10000)
{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}
Provided by @To9yStark
22 Problem Solvining and Programming (MCS-011)
/* Calculate gross salary */
gross = basic + hra + da;
printf("GROSS SALARY OF EMPLOYEE = %.2f", gross);
return 0;
}
(b) Explain the following with the help of a suitable example for each:
(i) if statement
Ans. The syntax of the if statement in C programming is:
if (test expression)
{
// statements to be executed if the test expression is true
}
Example:
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
return 0;
}
(ii) nested if statement
Ans. A nested if in C is an if statement that is the target of another if statement.
Nested if statements means an if statement inside another if statement. Yes, both C
and C++ allows us to nested if statements within if statements, i.e, we can place an if
statement inside another if statement.
Syntax:
if (condition1)
Provided by @To9yStark
Shrichakradhar.com 23
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example:
// C program to illustrate nested-if statement
#include <stdio.h>
int main() {
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
(iii) switch statement
Ans. A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked for
each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
switch(expression) {
case constant-expression:
statement(s);
break; /* optional */
Provided by @To9yStark
24 Problem Solvining and Programming (MCS-011)
case constant-expression:
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
Example:
#include <stdio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;
}
(iv) for loop
Ans. The syntax of the for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
Provided by @To9yStark
Shrichakradhar.com 25
}
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
• However, if the test expression is evaluated to true, statements inside the body of
for loop are executed, and the update expression is updated.
• Again the test expression is evaluated.
• This process goes on until the test expression is false. When the test expression is
false, the loop terminates.
Example:
// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
Provided by @To9yStark