0% found this document useful (0 votes)
60 views11 pages

Answer 1

The document contains examples of different C programming concepts including enumerated data types, macros, and typedef. An enumerated data type defines a user-defined data type consisting of integral constants. A macro replaces an identifier with a predefined string or value using the #define preprocessor statement. Typedef assigns an alternative name to existing C data types. Examples are provided to demonstrate how each concept can be implemented in C code.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views11 pages

Answer 1

The document contains examples of different C programming concepts including enumerated data types, macros, and typedef. An enumerated data type defines a user-defined data type consisting of integral constants. A macro replaces an identifier with a predefined string or value using the #define preprocessor statement. Typedef assigns an alternative name to existing C data types. Examples are provided to demonstrate how each concept can be implemented in C code.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Answer 1.

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
 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
 Initialize Y to Y divided 16
 Increment Len
 for(Initialize I to Len-1 ; Condition I Greater than -1 ; Decrement I )
 If HEXD[I] Less than 10
 HEXC[I]=HEXD[I]+48
 Else
 HEXC[I]=HEXD[I]+55
 Initialize HEXC[I] to NULL
 Print HEXC

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:-
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
: "); for(i=len-1;i>=0;i-
-)
{
switch(hexd[i])
{
case 10:
printf("A"); break;
case 11:
printf("B"); break;
case 12:
printf("C"); break;
case 13:
printf("D"); break;
case 14:
printf("E"); break;
case 15:
printf("F"); break;
default :
printf("%ld",hexd[i]);
}
}
}

//===================================
=============
void main()
{
long int num;
clrscr();
printf("Enter the decimal number : ");
scanf("%ld",&num);
dec_hex(num);
getch();
}
ANSWER 2.

#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';
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\tP :- PASSED\t\tF :- FAILED”);

Answer 3.

#include
<stdio.h> int
main()
{
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;
}
Answer 4.
#include<stdio.h>
int Fibonacci(int); int main()
{
int n, i = 0, c;
scanf("%d",&n); printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i)); i++;
}
return 0;
}

int Fibonacci(int n)
{
if ( n == 0 ) return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

Answer 5

#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");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
scanf("%d",&B[I][J]);
}
}
printf("ENTER 3X3 MATRIX C VALUES\n");
for(I=0;I<3;I++)
{
for(J=0;J<3;J++)
{
scanf("%d",&C[I][J]);
}
} 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();
}

Answer 6.

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");
printf("Enter Any string [below 20 chars] : ");
gets(str);
pt = str;
while (*pt != '\0') {
i++;
pt++;
}
printf("Length of String : %d", i);

return 0;
}

Answer 7.

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); 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);
Answer 8.
Enumerated data type

Ans: An enumeration is a user-defined data type that consists of integral constants. To


define an enumeration, keyword enum is used.

Example: Enumeration Type


#include <stdio.h>

enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

int main()
{
enum week today;
today = wednesday;
printf("Day %d",today+1);
return 0;
}
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

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


alternative name to C's already exist data types. This keyword, typedef typically
employed in association with user-defined data types in cases if the names of datatypes
turn out to be a little complicated or intricate for a programmer to get or to use within
programs.

Example:
#include<stdio.h>
#include<string.h>

typedef struct professor


{
char p_name[50];
int p_sal;
} prof;
void main(void)
{
prof pf;
printf("\n Enter Professor details: \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
printf("you are not Eligible");
printf("Enter you age:"); scanf("%d", &age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s getch();
}
(e) Break statement

Ans. 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 (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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy