Documente Academic
Documente Profesional
Documente Cultură
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 :
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");
}
#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:
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:
#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:
}
}
/* 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();
}
void main()
{
char str[20], *pt;
int i = 0;
clrscr();
pt = str;
i++;
pt++;
}
getch();
}
Output:
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);
}
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>
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;
}
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 −
After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned
char, for example..
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 −
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>
int main( ) {
Book book;
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.
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++;
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