You are on page 1of 14

MCS-011 Solved assignment july

2018-january 2019 session


Question 1. Write an algorithm, draw a flow chart and write its corresponding C
program to convert a decimal number to its equivalent hexadecimal number.
Ans.
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.

Pseudo code

 Input Num
o m
.c
 Initialize Len to zero and Y to Num


While Y is not zero
Save Remainder by Y Mod 16 in array HEXD at index Len
o t
 Initialize Y to Y divided 16
s p



Increment Len

lo g
for(Initialize I to Len-1 ; Condition I Greater than -1 ; Decrement I )


If HEXD[I] Less than 10

. b
HEXC[I]=HEXD[I]
+48
s i te


Else

o u
ig n
HEXC[I]=HEXD[I]



w . +55
Initialize HEXC[I] to NULL

w
Print HEXC

w
Detailed Algorithm:

Step 1: Input NUM

Step 2: LEN = 0 & Y=NUM

Step 3: While (Y > 0)

HEXD[LEN]=Y%16

Y=Y/16

LEN++

Step 4: for(I=LEN-1;I>-1;I–)

IF(HEXD[I]<10)

HEXC[I]=HEXD[I]+48;
ELSE

HEXC[I]=HEXD[I]+55;

Step 5: HEXC[I]=NULL

Step 5: Print HEXC

Flowchart:-

o m
o t.c
s p
lo g
. b
s i te
o u
ig n
w .
w w

#include<stdio.h>

#include<conio.h>

void dec_hex(long int num) // Function Definition

{
long int hexd[50],i=0,len=0;

long int y=num;

while(y>0)

hexd[i]=num%16;

y=y/16;

i++;

len++;

printf("Hexadecimal number : ");


o m
for(i=len-1;i>=0;i--)
o t.c
{
sp
switch(hexd[i])
lo g
. b
te
{

case 10:
s i
o u
n
printf("A");

w . ig
break;

w
case 11:

w printf("B");

break;

case 12:

printf("C");

break;

case 13:

printf("D");

break;
4
Page
case 14:

printf("E");

break;

case 15:

printf("F");

break;

default :

printf("%ld",hexd[i]);

}
o m
}
o t .c
//================================================
s p
void main()
lo g
. b
te
{

long int num;


s i
o u
n
clrscr();

w . ig
w
printf("Enter the decimal number : ");

w scanf("%ld",&num);

dec_hex(num); // Calling function

getch();

Question 2. 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.
Ans.

#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}
o m
};
void main()
{
o t .c
s p
g
int ROL_NO;

lo
void gen_result(int);
clrscr();

. b
te
printf(“ENTER roll no (1 to 12) : “);
scanf(“%d”,&ROL_NO);
if(ROL_NO>0 && ROL_NO<13)
s i
gen_result(ROL_NO);
else
o u
n
printf(“\nYOU HAVE ENTERED WRONG ENROLMENT NO. !!”);

ig
.
getch();
}

{
w w
void gen_result(int ROLL)

w
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';
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);
o m
printf(“\n\t
;
printf(“\n\t\t\tP :- PASSED\t\tF :- FAILED”);
o t .c ”)

s p
lo g
Question 3. Write a C program to generate the following pattern:<br/>
1
. b
te
12
123
1234
s i
o
12345
u
Ans.

ig n
w .
#include <stdio.h>
int main()
{

w w
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i)


{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Question 4. Write a program to generate Fibonacci series using Recursion.
Ans.

#include<stdio.h>

int Fibonacci(int);

int main()

int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");
o m
for ( c = 1 ; c <= n ; c++ )

o t .c
{

s p
printf("%d\n", Fibonacci(i));

lo g
i++;
. b
}

s i te
return 0;
o u
}
ig n
w .
int Fibonacci(int n)

w w
{

if ( n == 0 )

return 0;

else if ( n == 1 )

return 1;

else

return ( Fibonacci(n-1) + Fibonacci(n-2) );

}
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.

Ans.
#include<stdio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],D[3][3],I,J,K;
clrscr();
printf("ENTER 3X3 MATRIX A VALUES\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
scanf("%d",&A[I][J]);
}
}
printf("ENTER 3X3 MATRIX B VALUES\n");
o m
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
o t .c
{

s p
g
scanf("%d",&B[I][J]);

lo
}
}
printf("ENTER 3X3 MATRIX C VALUES\n");

. b
te
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
s i
{

o
scanf("%d",&C[I][J]);
u
}
}

ig n
{
w .
for(I=0;I<3;I++)

w
for(J=0;J<3;J++)
{

w
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.

Ans.

C Program to calculate given string length:


#include<stdio.h>

int main() {
char str[20], *pt;
int i = 0;
printf("Pointer Example Program : Find or Calculate Length of String \n");

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

o
gets(str);

.c
pt = str;
while (*pt != '\0') {
i++;
o t
}
pt++;

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

lo g
b
return 0;
}

te .
s i
o u
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.
ig n
Ans.

w .
w w
Program to write even and odd integers into different files

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

o m
.c
all=fopen("ANYNUMBER","r");

even=fopen("EVENNUMBER","w");
o t
odd=fopen("ODDNUMBER","w");
s p
while((number=getw(all))!=EOF)
lo g
. b
te
{

if(number%2==0)
s i
o
putw(number,even); u
else
ig n
w . putw(number,odd);

ww }

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

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


concepts:

a) Enumerated data type

o m
.c
Ans: An enumeration is a user-defined data type that consists of integral constants. To
define an enumeration, keyword enum is used.

o t
Example: Enumeration Type
s p
#include <stdio.h>
lo g
. b
te
enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

int main()

s i
{
enum week today;
o u
ig n
today = wednesday;
printf("Day %d",today+1);

w .
return 0;

w w
Output

Day 4

b) Macros In C

Ans.  Macro is a process where an identifier in a program is replaced by a predefined string or


value.

 #define is a preprocessor statement and is used to define macros.

C program example for Macros:


#include<stdio.h>
#include<conio.h>
#define square(x) x*x
void main()
{
int n,s;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
s=square(n);
printf("Square of given number = %d",s);
getch();
}

(c) typedef

o m
.c
Ans. typedef is a C keyword implemented to tell the compiler for assigning an

o t
alternative name to C's already exist data types. This keyword, type def typically
employed in association with user-defined data types in cases if the names of datatypes

s p
turn out to be a little complicated or intricate for a programmer to get or to use within
programs.

lo g
Example:
. b
#include<stdio.h>
#include<string.h>
s i te
o
typedef struct professor
u
{

ig
char p_name[50];
n
w
} prof;
.
int p_sal;

w
{ w
void main(void)

prof pf;
printf("\n Enter Professor details: \n \n");
printf("\n Enter Professor name:\t");
scanf("% s", pf.p_name);
printf("\n Enter professor salary: \t");
scanf("% d", &pf.p_sal);
printf("\n Input done ! ");
}

(d) Goto Statement

Ans. C supports a unique form of a statement that is the goto Statement which is used to
branch unconditionally within a program from one point to another. Although it is not a good
habit to use goto statement in C, there may be some situations where the use of goto statement
might be desirable.

The goto statement is used by programmers to change the sequence of execution of a


C program by shifting the control to a different part of the same program.

Example:
#include<stdio.h>

void main()
{
int age;

g: //label name
printf("you are Eligible\n");
s: //label name

m
printf("you are not Eligible");

printf("Enter you age:");

.c o
scanf("%d", &age);
if(age>=18)
o t
else
goto g; //goto label g

s p
getch();
goto s; //goto label s

lo g
b
}

te .
(e) Break statement
s i
o u
Ans. The break statement in C programming has the following two usages –


ig n
When a break statement is encountered inside a loop, the loop is immediately

w .
terminated and the program control resumes at the next statement following the
loop.

w  w
It can be used to terminate a case in the switch statement (covered in the next
chapter).

Example:
#include <stdio.h>

int 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;
}
}

return 0;
}

o m
o t.c
s p
lo g
. b
s i te
o u
ig n
w .
w w

You might also like