Sunteți pe pagina 1din 11

MCS-011 ASSIGNMENT

Question1: Write an algorithm, draw a flow chart and write its corresponding C
program to convert a decimal number to its equivalent hexadecimal number.

Answer. An algorithm is a finite set of steps defining the solution of a particular problem. An
algorithm is expressed in pseudo code – something resembling C language or Pascal, but with
some statements in English rather than within the programming language
1. A sequential solution of any program that written in human language, called algorithm.
2. Algorithm is first step of the solution process, after the analysis of problem, programmers
write the algorithm of that problem.
Algorithm:
Step 1: Inpur N;
Step 2: len=0 and y=n;
Step 3: while (y>0)
Hex[len]=y%16;
y=y/16;
len++;
Step 4: for(i=len-1;i>-1;i--)
If(Hex[i]<10)
Hex[i]=Hex[i]+48;
Else
Hex[i]=Hex[i]+55;
Step 5: Print Hex
Step 6: Stop
Flowchart :

Page 1 of 11 IGNOU Help


MCS-011 ASSIGNMENT

Question2: Write an algorithm and its corresponding C program to generate students’


Progress-Report for VIII standard (section of 20 students) of a CBSE school for all its
4 terms. Use Structures concept. Assumptions can be made wherever necessary.
Answer.
#include<stdio.h>
struct student
{
int ROLL;
char NAME[15];
int S1T1,S1T2,S1T3,S1T4,S2T1,S2T2,S2T3,S2T4,S3T1,S3T2,S3T3,S3T4
,S4T1,S4T2,S4T3,S4T4,S5T1,S5T2,S5T3,S5T4,S6T1,S6T2,S6T3,S6T4;
}STUD[12]={
{1,"GANESH" ,25,6,28,6,35,8,26,4,35,6,34,7,27,7,36,5,34,7,31,8,35,7,26,9},
{2,"MAHESH" ,35,6,26,7,31,6,36,8,25,5,29,6,28,9,37,9,27,7,34,6,31,5,31,8},
{3,"SURESH" ,21,9,27,7,26,7,28,7,31,6,29,6,28,9,37,9,27,7,34,6,31,5,31,8},
{4,"KALPESH" ,28,9,37,9,27,7,34,6,31,5,31,8,21,9,27,7,26,7,28,7,31,6,29,6},
{5,"RAHUL" ,27,7,36,5,34,7,31,8,35,7,26,9,21,9,27,7,26,7,28,7,31,6,29,6},
{6,"SUBBU" ,29,9,34,7,38,9,37,7,34,9,36,8,25,8,37,9,34,7,35,7,27,9,26,7},
{7,"RAKESH" ,25,8,37,9,34,7,35,7,27,9,26,7,29,9,34,7,38,9,37,7,34,9,36,8},
{8,"ATUL" ,25,6,38,7,35,8,25,7,27,9,26,6,27,7,36,5,34,7,31,8,35,7,26,9},
{9,"DHARMESH",35,6,37,7,34,8,36,8,37,7,34,9,28,9,37,9,27,7,34,6,31,5,31,8},
{10,"AJAY" ,35,7,37,6,34,5,35,7,37,6,36,5,21,9,27,7,26,7,28,7,31,6,29,6},
{11,"ABDUL" ,25,5,28,7,25,5,26,6,25,6,34,5,35,6,26,7,31,6,36,8,25,5,29,6},
{12,"RASHMI" ,35,7,38,5,25,6,36,5,35,4,26,5,25,6,28,6,35,8,26,4,35,6,34,7}
};
void main()
{
int ROL_NO;
void gen_result(int);
clrscr();
printf("ENTER roll no (1 to 12) : ");
scanf("%d",&ROL_NO);
if(ROL_NO>0 && ROL_NO<13)
gen_result(ROL_NO);
else
printf("\nYOU HAVE ENTERED WRONG ENROLMENT NO. !!");
getch();
}
void gen_result(int ROLL)
{
char STATUS;
int M01,M02,M03,M04,M05,M06;
M01=STUD[ROLL-1].S1T1+STUD[ROLL-1].S1T2+STUD[ROLL-1].S1T3+STUD[ROLL-1].S1T4;
M02=STUD[ROLL-1].S2T1+STUD[ROLL-1].S2T2+STUD[ROLL-1].S2T3+STUD[ROLL-1].S2T4;
M03=STUD[ROLL-1].S3T1+STUD[ROLL-1].S3T2+STUD[ROLL-1].S3T3+STUD[ROLL-1].S3T4;
M04=STUD[ROLL-1].S4T1+STUD[ROLL-1].S4T2+STUD[ROLL-1].S4T3+STUD[ROLL-1].S4T4;
M05=STUD[ROLL-1].S5T1+STUD[ROLL-1].S5T2+STUD[ROLL-1].S5T3+STUD[ROLL-1].S5T4;
M06=STUD[ROLL-1].S6T1+STUD[ROLL-1].S6T2+STUD[ROLL-1].S6T3+STUD[ROLL-1].S6T4;
printf("\n\t\t\tINDIRA GANDHI CBSE HIGH SCHOOL");
printf("\n\t\t\tPROGRESS CARD : (2018-2019)");
printf("\n\n\tROLL NO.\t: %d",ROLL);
printf("\n\tNAME\t\t: %s",STUD[ROLL-1].NAME);
printf("\n\tSTANDARD \t: VIII\t\tDIV\t: A");
printf("\n\t_______________________________________________________________");
printf("\n\tCOURSE\t\t1_TERM\t2_TERM\t3_TERM\t4_TERM\tTOTAL");
printf("\n\t CODE\t\t(40%)\t(10%)\t(40%)\t(10%)\t(100%)\tSTATUS");
printf("\n\t_______________________________________________________________");
if(M01<40) STATUS='F'; else STATUS='P';
printf("\n\tENGLISH\t\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S1T1,STUD[ROLL-1].S1T2,STUD[ROLL-
1].S1T3,STUD[ROLL-1].S1T4,M01,STATUS);
if(M02<40) STATUS='F'; else STATUS='P';
printf("\n\n\tHINDI\t\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S2T1,STUD[ROLL-1].S2T2,STUD[ROLL-
1].S2T3,STUD[ROLL-1].S2T4,M02,STATUS);
if(M03<40) STATUS='F'; else STATUS='P';

Page 2 of 11 IGNOU Help


MCS-011 ASSIGNMENT

printf("\n\n\tMATHS\t\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S3T1,STUD[ROLL-1].S3T2,STUD[ROLL-
1].S3T3,STUD[ROLL-1].S3T4,M03,STATUS);
if(M04<40) STATUS='F'; else STATUS='P';
printf("\n\n\tSOCIAL\t\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S4T1,STUD[ROLL-1].S4T2,STUD[ROLL-
1].S4T3,STUD[ROLL-1].S4T4,M04,STATUS);
if(M05<40) STATUS='F'; else STATUS='P';
printf("\n\n\tSCIENCE\t\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S5T1,STUD[ROLL-1].S5T2,STUD[ROLL-
1].S5T3,STUD[ROLL-1].S5T4,M05,STATUS);
if(M06<40) STATUS='F'; else STATUS='P';
printf("\n\n\tSANSKRIT\t%d\t%d\t%d\t%d\t%d\t%c",STUD[ROLL-1].S6T1,STUD[ROLL-1].S6T2,STUD[ROLL-
1].S6T3,STUD[ROLL-1].S6T4,M06,STATUS);
printf("\n\t_______________________________________________________________");
printf("\n\t\t\t P :- PASSED\t\t F :- FAILED");
}

Output of above program:

Question 3: Write a C program to generate the following pattern:


1
12
123
1234
12345

Page 3 of 11 IGNOU Help


MCS-011 ASSIGNMENT

Answer: For this question we have to use for loop:

#include<stdio.h>

void main()
{
char C[]="12345";
int I,J;
clrscr();
printf("Result :\n");
for(I=0;I<5;I++)
{
printf("\n");
for(J=0;J<=I;J++)
{
printf("%c ",C[J]);
}
printf("\n");
}
getch();
}

Output:

Question 4: Write a program to generate Fibonacci series using Recursion.

Answer: Recursive function is a function which calls itself. Calling a function within itself
makes it a endless loop. So we need to take care that there must be a termination condition in
every recursive function.
For writing a function and using it in the C program, we should declare the function in the MAIN
function. Declaration has to done in the area before the code starts, same area where we declare
data variables.

Program:

Page 4 of 11 IGNOU Help


MCS-011 ASSIGNMENT

#include<stdio.h>
void main()
{
void fibo_rec(int,int);
clrscr();
printf("\nFIBONACCI SERIES < 1000 ARE :- \n");
fibo_rec(0,1);
getch();
}
void fibo_rec(int X,int Y)
{
printf(" %d ",X);
if(Y<1000)
fibo_rec(Y,X+Y);
}

Output:

Question 5: Write a C program to perform the following operation on matrices D = A +


(B * C), where A, B and C are matrices of (3 X 3) size and D is the resultant matrix.
Answer:
#include<stdio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],D[3][3],I,J,K;
clrscr();
/* Entering Matrix A*/
printf("ENTER 3X3 MATRIX A VALUES\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
scanf("%d",&A[I][J]);
}
}
/* Entering Matrix B*/
printf("ENTER 3X3 MATRIX B VALUES\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
scanf("%d",&B[I][J]);

Page 5 of 11 IGNOU Help


MCS-011 ASSIGNMENT

}
}
/* Entering Matrix C*/
printf("ENTER 3X3 MATRIX C VALUES\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
Scanf("%d",&C[I][J]);
}
}
/* Performing Operation D=A+(B*C) */
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
D[I][J]=0;
for(K=0;K<3;K++)
{
D[I][J]=D[I][J]+A[I][K]*B[K][J];
}
D[I][J]=D[I][J]+C[I][K];
}
}
printf("RESULT 3X3 MATRIX D VALUES ARE :\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
printf("%d\t",D[I][J]);
}
printf("\n");
}
getch();
}

Question 6: Write an interactive C program to calculate the string length of a given


string, using pointers.
Answer:
#include<stdio.h>
#include<conio.h>

void main()
{
char str[20], *pt;
int i = 0;
clrscr();

printf("Pointer Example Program : Find or Calculate Length of String \n");


printf("Enter Any string [below 20 chars] : ");
gets(str);

pt = str;

while (*pt != '\0')


{

Page 6 of 11 IGNOU Help


MCS-011 ASSIGNMENT

i++;
pt++;
}

printf("Length of String : %d", i);

getch();
}

Output:

Enter Any string [below 20 chars] : FIND-STRING-LENGTH


Length of String : 18

Question 7: Write a C program to take a list of N numbers, separate even and odd
numbers and put them in two appropriate files (evenfile and oddfile). Use File
Handling concept.
Answer:
#include<stdio.h>
#include<math.h>
void main()
{
FILE *all,*even,*odd;
int number,i,records;
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER");
scanf("%d",& records);
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);

even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf("THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
printf(" %4d",number);
printf("THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf(" %4d",number);
fclose(even);
fclose(odd);
}

Page 7 of 11 IGNOU Help


MCS-011 ASSIGNMENT

Question 8: Write an interactive C program for each to illustrate the following


concepts:
(a)Enumerated data type
(b) Macros in C
(c) typedef
(d) Goto statement
(e) Break statement
Answer:
a. Enumerated data type :
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral
constants, the names make a program easy to read and maintain.Ex:
enum State {Working = 1, Failed = 0};

The keyword ‘enum’ is used to declare new enumeration types in C and C++.
Program :
// An example program to demonstrate working
// of enum in C
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}

Output:

b. Macros in C :
A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-
processor, the block of code is communicated to the compiler before entering into the actual coding
(main () function). A macro is defined with the preprocessor directive, #define.
The advantage of using macro is the execution speed of the program fragment. When the actual code
snippet is to be used, it can be substituted by the name of the macro. The same block of statements,
on the other hand, need to be repeatedly hard coded as and when required.
The disadvantage of the macro is the size of the program. The reason is, the pre-processor will
replace all the macros in the program by its real definition prior to the compilation process of the
program.
Program :
# include<stdio.h>
# define LOOP for(x=0; x<5; x++) \
{ y=x+1; \
printf(“%-5.*s\n”, y, string); } \
for(x=4; x>=0; x--) \
{ y=x+1; \
printf(“%-5.*s \n”, y, string); }
void main()
{
int x, y;
static char string[ ] = “COBOL”;
printf(“\n”); LOOP;
}

Page 8 of 11 IGNOU Help


MCS-011 ASSIGNMENT

When the above program is executed the reference to macro (loop) is replaced by the set of
statements contained within the macro definition.

OUTPUT

C
CO
COB
COBO
COBOL
COBOL
COBO
COB
CO
C

c. Typedef :
The C programming language provides a keyword called typedef, which you can use to give a type a
new name. Following is an example to define a term BYTE for one-byte numbers −

typedef unsigned char BYTE;

After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned
char, for example..

BYTE b1, b2;

By convention, uppercase letters are used for these definitions to remind the user that the type name
is really a symbolic abbreviation, but you can use lowercase, as follows −

typedef unsigned char byte;

You can use typedef to give a name to your user defined data types as well. For example, you can
use typedef with structure to define a new data type and then use that data type to define structure
variables directly as follows –
#include <stdio.h>
#include <string.h>

typedef struct Books {


char title[50];
char author[50];
char subject[100];
int book_id;
} Book;

int main( ) {

Book book;

strcpy( book.title, "C Programming");


strcpy( book.author, "Nuha Ali");
strcpy( book.subject, "C Programming Tutorial");
book.book_id = 6495407;

Page 9 of 11 IGNOU Help


MCS-011 ASSIGNMENT

printf( "Book title : %s\n", book.title);


printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);

return 0;
}

Output :
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407

d. Goto statement
goto is a jumping statement in c language, which transfer the program’s control from one statement to
another statement (where label is defined).
goto can transfer the program’s within the same block and there must a label, where you want to
transfer program’s control. Syntax:
goto label_name;
here label name is the point where you want your program to go.
/*To print numbers from 1 to 10 using goto statement*/
#include <stdio.h>
int main()
{
int number;
number=1;

repeat:
printf("%d\n",number);
number++;

if(number<=10)
goto repeat;

return 0;
}

Output :
1
2
3
4
5
6
7
8
9
10

e. Break statement
The break statement in C programming has the following two usages −
• When a break statement is encountered inside a loop, the loop is immediately terminated and
the program control resumes at the next statement following the loop.
• It can be used to terminate a case in the switch statement.

Page 10 of 11 IGNOU Help


MCS-011 ASSIGNMENT

If you are using nested loops, the break statement will stop the execution of the innermost loop and
start executing the next line of code after the block.
Syntax
The syntax for a break statement in C is as follows –
break;
#include <stdio.h>
#include <conio.h>
void main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;

if( a > 15) {


/* terminate the loop using break statement */
break;
}
}
getch();
}

Output :

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

Page 11 of 11 IGNOU Help

S-ar putea să vă placă și