Breaking News
You are here: Home / Paper Solutions / SEM-2 / C Programming / C Programming Paper solution OCTOBER/NOVEMBER 2007

C Programming Paper solution OCTOBER/NOVEMBER 2007

C Programming Paper solution OCTOBER/NOVEMBER 2007

Q:-1 Answer the following:

 

(a)  Compare the following in terms of their functions, the following pair of statements.

(i)  Break Vs Continue 
Break Continue
Break statement is use to break the loop. Continue statement is used to continuethe loop.
Syntax:-             break; Syntax:-             continue;
When a break statement is encounteredinside the loop, the loop is immediatelyexit  and  the  program  continues  withStatement following the loop. Continue statement skip the followingstatement and continue with the nextIteration. 
Break cause loop terminate.  Continue cause the loop to be continuedWith next iteration.
e.g.

void main(){

int i;

clrscr();

for(i=1; ; i++)

{

if(i>5)

break;

printf("%d ",i); // 5 times

only

}

getch();

}

Output:-

1 2 3 4 5

e.g:-

void main(){

int i;

clrscr();

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

{

if(i==6)

continue;

printf(" %d",i);

// 6 is omitted

}

getch();

}

Output:-

1 2 3 4 5 7 8 9 10

 

(ii) While loop VS For loop

While loop

For loop

While having three different part likeInitialization, condition, and increment. For have inbuilt syntax for initialization, condition, and increment.
Syntax:-while(condiion){Body of loop;   } Syntax:-for (initialization;condition;increment){Body of loop; }
e.g.

void main()

{

int i=1,n;

clrscr();

printf("Enter any number: ");

scanf("%d",&n);

while(i<=n)

{

printf("%d ",i);

i++;

}

}

Output:-

1 2 3 4 5

e.g.

void main()

{

int i,n;

clrscr();

printf("Enter any number: ");

scanf("%d",&n);

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

{

printf("%d ",i);

}

}

Output:-

1 2 3 4 5

 

 

(B) what is the use of control string in an output statement?

Answer:- control string is used to print characters that will be print on screen hey appear. It is also used for format specification that define the out format for display each item.

 

It can also used for escape sequence each as \n, \t..

 

(C)  How can you assign hexadecimal and octal value to a variable?

Answer:- Integer constants can be defined in octal or hex by using the associated prefix. e.g., to define an integer as an octal constant use %o

 

int  sum = %o567;

 

To define an integer as a hex constant use %0x int sum = %0x7ab4;

 

(D) Determine the value of each of the following logical expressions.

A=5 b=10 c=-6

 

(i)  a>b&&a<c

(ii)  a= =c ll b>a

 

(E)When do we use the following ?

(i) The size of operator:-

This operator is used to find out the memory size occupies by the variable in memory. It return the memory size in byte unit.

For example:

Void main()

{

int x,y; char a; y=sizeof(x);

printf(“%d”,y);

printf(“%c”,sizeof(a);

}

 

(ii)The Bit Wise operator:-

C’s powerful features are a set of bit manipulation operators. These permit the programmer to access and manipulate individual bits within a piece of data. The various Bitwise Operators available in C are as follow:

 

Operator Meaning
Bitwise exclusive OR ^
Right shift >>
Left shift <<
Bitwise AND &
Bitwise OR |

 

(F)Differentiate between if and goto statement.

If Goto
If is conditional statement. Goto is control statement.
If  condition is true the normal execution During running time a program when met
will take place otherwise that block will be with goto statement, the flow of control will
skipped. jump  to  the  statement  immidiately  the
label.
Syntax:- Syntax:-
If(expression) goto label;
{ where, label is valid variable name.
Statements block;  
}
e.g. e.g.
void main() void main ()
{ {
int n; int x=0;
printf(“Enter any number”); label1:
scanf(“%d”,&n); x=x+1;
if (n==0) printf(“%d “,x);
{ if(x!=5)
printf(“it is zero”); goto label1;
} getch();
getch(); }
}  

 

(G) What is null statement? Explain a typical use of it.

Answer:- A string constant is terminated by a null ( ‘\0’ ). For example,

 

char name[ ] = { ‘H’, ‘A’, ‘E’, ‘S’, ‘L’, ‘E’, ‘R’, ‘\0’ } ;

 

Each character in the array occupies one byte of memory and the last character is always ‘\0’. ‘\0’ is called null character.

 

null (‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends.

 

Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically.

 

(H)How can we use ‘for’ loop when the numbers of iterations are not known ?

Answer:-

 

Q:-2

 

(1) Explain type conversion. Also discuss the condition in which either implicit or explicit conversion is preferred?

Answer:- Type conversion means it convert one data type of variable into other compatible type. It take place from lower to higher data types.

 

There are two type of type conversion.

 

  1. Implicit type casting
  2. Explicit type casting

 

  • Discuss the conditions in which either implicit or explicit conversion:-

An implicit conversion does not require any special syntax in the source code. An explicit conversion uses a type conversion keyword.

 

Implicit conversion:-

 

C automatically convert any intermediate value to the proper type. so that expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion.

 

Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

 

e.g.

int a=5; float b; b=a;

 

Here, the value of a has been promoted from int to float and we have not had to specify any type-casting operator.

 

Explicit conversion:-

In this type of conversion we force to convert type to another data type which is different from automatic type conversion.

 

Syntax:- (type)expression

Where, type is the standard C data types.

 

The expression may be constant, variable, expression.

Example Action
x=(int)7.5 7.5 is converted to integer.
a=(int)21.5/(int)4.5 Evaluate as 21/4 and result
would be 5.
y=(int)(a+b) The result of a+b is convert to
integer.
z=(int)a+b a is convert to integer and then
added to b.

casting can be used to round off a given value.

 

(2) Write a program to read the age of 100 persons and count the number of persons in the age group of 20-40, 50-60 and 70-80.

 

#include<stdio.h>
#include<conio.h> void main()
{
int a[100],n,i,b=0,c=0,d=0; clrscr();
for (i=1;i<=100;i++)
{
printf("a[%d]",i);
scanf("%2d",&a[i]); if(a[i]>=20 && a[i]<=40)
b++;
else if(a[i]>=50 && a[i]<=60) c++;
else if(a[i]>=70 && a[i]<=80) d++;
}
printf("\nThe number of people in the group 20-40 %d\n",b); printf("The number of people in the group 50-60 %d\n",c); printf("The number of people in the group 70-80 %d\n",d); getch();
}

 

OR

(2) Write a program to generate following output.

1

1  2

1 2 3

1 2 3 4

1 2 3 4 5

 

#include<stdio.h>
#include<conio.h> void main()
{

int n,i,k,j; clrscr(); 

printf("Enter any number :"); scanf("%d",&n); for(i=1;i<=n;i++)
{
for(k=i;k<=n-1;k++) printf(" ");
for (j=1;j<=i;j++) printf("%d ",j);
printf("\n"); 

}

getch();

}

 

(3)Explain  GOTO statement in brief.

Answer:- In the goto statement we specify a label in the program and the program is started back from that label, irrespective of any constrains. Most important thing to be remembered is that when we use goto statement we must use always specify it in a condition. Otherwise, the program shall never stop and go into infinite mode.

 

Syntax: label:

——-

——-

goto label; statements;

 

Where; label is any valid variable name, and must be followed by a colon.

  • the lable can be anywhere in the program either before or after goto statement.
  • During running time a program when met with goto statement, the flow of control will jump to the statement immidiately the label.
  • If a label is before the statement goto label, some statements will be executed repeatedly. Such a jump is known as a backward jump.
  • If a label is after the statement goto label, some statements will be skipped and a jump is known as a forward jump.

 

example:

#include<stdio.h>
#include<conio.h> 
void main ()
{
int x=0; clrscr(); label1: x=x+1;
printf("%d ",x); if(x!=5)
goto label1; getch();
}

 

Q:-3

 

(1)Explain any three-string functions with their syntax.

 

(i)strcmp():-

This is a function which compares two strings to find out whether they are same or different.

syntax:-

strcmp ( string1, “string2” ) ;

 

where string1 and string2 my be string variable or string constant. Example:

 

void main()
{

char string1[ ] = "Jerry" ; char string2[ ] = "Ferry" ; int i, j, k ;

i = strcmp ( string1, "Jerry" ) ; j = strcmp ( string1, string2 ) ;

k = strcmp ( string1, "Jerry boy" ) ; printf ( "\n%d %d %d", i, j, k ) ;

}

 

And here is the output…

0 4 -32

 

(ii)strlen:-

this function count and return the number of character in a string.

syntax:-

n=strlen(string);

 

where, n is an integer variable, which receives the length of the string.

Example:-

void main()
{
int n,l; char a[10];
scanf("%s",&a);

l=strlen(a);

printf("The length is: %d",l); getch();

}

 

output…

 

fybca

The length is:5

 

(iii) strcpy():-

this function is used to copy a string to another string.

Syntax:-

 strcpy(string1,string2);

 

Here, assign the content of string2 to string1. Example:-

 

#include<stdio.h>
#include<conio.h> 
void main()
{
char a;

printf("Enter string for a:"); scanf("a=%s",&a); printf("%s",a); strcpy(a,"vtcbb"); printf("a=%s",a);

getch();

}

 

Output:-a=FYBCA a=vtcbb

 

===OR===

 

(1)Explain any 3 mathematical functions with their syntax.

 

(i)sqrt():-

The sqrt() function returns the non-negative square root of x in the double data type. An error occurs if x is negative.

 

Syntax:-sqrt(x);

 

here, x is positive variable.

Example:-

void main()
{
float i,j;
printf("Enter any positive number:"); scanf("%f",&i);
j=sqrt(i);
printf("%.2f",j);
getch();
}

 

Output:-

Enter any positive number:64 8.00

 

(ii) pow():-

this return the power of one value to another value. Syntax:-

 

pow(x,y);

 

here, x and y are variable. the value of the variable x is raised to the power of y.

Example:-

void main()
{
float i,j;
printf("Enter any number:"); scanf("%f",&i);
j=pow(i,5);
printf("%.2f",j);
getch();

}

 

Output:-

Enter any number:2 32.00

 

(iii) ceiling():-

this function rounded up to the nearest integer value. Syntax:-

 

ceil(x);

 

here, x is a variable.

Example:- 

void main()
{
float i,j;
printf("Enter any number:"); scanf("%f",&i);
j=ceil(i);
printf("%.2f",j);
getch();
}

 

Output:-

Enter any number:6.1 7.00

 

(2)How is ‘.’ (dot) operator used in structure.

In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator.

 

The dot (.) operator is used to access member of structure tha are themeselves member of a larger structure.

 

No parentheses are needed to force a special order of evolution; a member operator expression is simply evaluate left to right.

 

example:

main( )
{
struct book
{
char name ; float price ; int pages ;
} ;

struct book b1, b2, b3 ;

printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ; 
scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;

scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ; 
scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ; 
printf ( "\nAnd this is what you entered" ) ;

printf ( "\n%c %f %d", b1.name, b1.price, b1.pages ) ; 
printf ( "\n%c %f %d", b2.name, b2.price, b2.pages ) ; 
printf ( "\n%c %f %d", b3.name, b3.price, b3.pages ) ;

}

 

Output…

Enter names, prices and no. of pages of 3 books

A 100.00 354

C 256.50 682

F 233.70 512

 

And this is what you entered

A 100.000000 354

C 256.500000 682

F 233.700000 512

 

 

(3)Write a program to print following series.

0,1,1,3,5,8……….

 

#include<stdio.h>
#include<conio.h> 
void main()
{
int n,i,f1=1,f2=0,f3=0; clrscr();
printf("Enter any number:"); scanf("%d",&n); for(i=1;i<=n;i++)
{
f3=f1+f2; printf(" %d",f3); f1=f2;
f2=f3;
}
getch();
}

 

Q-4:

(1) Given are 2-l dimensional arrays A and B which are sorted in descending order. Write a program to merge them into a single sorted (ascending) array C that contains every item from arrays A and B in descending order.

Answer:-

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n,m,a[50],b[50],c[50],t;

clrscr();

printf("\nEnter number of elements in first array:"); scanf("%d",&n);

printf("\t\t\t:Enter first array:"); 
for(i=1;i<=n;i++)
{ 

printf("\nEnter %d element:",i);
scanf("%d",&a[i]);

}

for(i=1;i<=n;i++)
{

for(j=1;j<=n;j++)
{

if(a[i]>a[j])
{

t=a[i];

a[i]=a[j];

a[j]=t;

}

}

}

printf("\nEnter number of elements in second array:"); 
scanf("%d",&m);

printf("\t\t\t:Enter second array:"); 
for(i=1;i<=m;i++)
{
printf("\nEnter %d element:",i); scanf("%d",&b[i]);

}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}

}

for(j=1;j<=m;j++,k++)
c[k]=b[j];

printf("\n The ascending order is:\n\n"); for(i=1;i<=m+n;i++)
{

for(j=1;j<=m+n;j++)
{

if(c[i]>=c[j])
{

 t= c[i]; c[i]=c[j]; c[j]=t;
}

}

}

for(i=1;i<=m+n;i++)
printf("\t\t%d\n\n",c[i]);

getch();

}

 

(2) Explain multidimensional array in ‘C’. Write different ways to initialize 2-dimensional array during the declaration.

Answer:-

we use arrays with only one dimension. It is also possible for arrays to have two or more dimensions. The two-dimensional array is also called a matrix.

 

Traditionally, the array elements are being stored and accessed row wise. However, you can access the array elements column wise as well.

 

Initializing a 2-Dimensional Array:——–

 

like the one-dimensional arrays, two dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

For example,

int table[2][3]={0,0,0,1,1,1};

 

initializes the elements of the first row to zero and the second rows to one. 000 111

The initialization is done row by row. we can also written above statement as by surrounding the elements of the each row by braces.

int table[2][3]={{0,0,0}, {1,1,1}};

 

We can also initialize a two-dimensional array in the form of matrix as shown below:

int table[2][3]={

 {0,0,0},

 {1,1,1}

};

 

here, commas are required after each row braces that closes off a row, except in case of the last row.

 

if the values are missing in an initialize, they are automatically set to zero.

int table[2][3]={

 {0,0},

{1}

};

 

When all the elements of each row is zero, the following short-cut method may be used.

int m[3][5]={0,0};
main( )
{

int stud[4][2]={1,2,3,4,5,6,7,8}; int i, j ;

for ( i = 0 ; i <= 3 ; i++ )

printf ( "\n%d %d", stud[i][0], stud[i][1] ) ;

}

 

Output:—-

1 2

3 4

5 6

7 8

 

Q5:-

(1)Define a union hotel with room id, room_type, and 6 room_cost. Write a program to read 10 records and print room type wise information in proper format.

 

#include<stdio.h>
#include<conio.h> 
struct hotel
{

int room_id;
char room_name[50]; 
char room_type[50]; int room_cost;

}a[10]; 

void main()
{
int i,t,j; clrscr();
for(i=1;i<=10;i++)
{
printf("Enter room_id:"); 
scanf("%d",&a[i].room_id); 
printf("Enter room_name:"); scanf("%s",a[i].room_name); 
printf("Enter room_type:"); scanf("%s",a[i].room_type); 
printf("Enter room_cost:"); scanf("%d",&a[i].room_cost);

}

for(i=1;i<=10;i++)
{

for(j=1;j<=10;j++)
{

 if(strcmp(a[i].room_type,a[j].room_type)<0)
{
strcpy(t,a[i].room_type ); strcpy(a[i].room_type,a[j].room_type); strcpy(a[j].room_type,t);
}

}

}

printf("\n***********************************************************"); 
printf("\nroom_id \t room_name \t room_type \t room_cost"); printf("\n*********************************** ***********************");

for(i=1;i<=10;i++)
{ 
printf("\n %d\t",a[i].room_id); 
printf("\t %s \t",a[i].room_name); 
printf("\t %s\t",a[i].room_type); 
printf("\t %d",a[i].room_cost);
}

getch();
}

 

 

(2)5 candidates contest an election. The candidates are numbered 1 to 5 and marking the candidate number on the ballot paper does the voting. Write a program to read the ballots and count the votes casted for each candidate using an array variable ‘count’. In case a number read is outside the range 1 to 5, the ballot should be considered as “spoiled ballot” and program should also count the number of “spoiled counts”.

 

#include<stdio.h>
#include<conio.h> 
void main()
{
int ch,i,a=0,b=0,c=0,d=0,e=0; 
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter your choice:"); 
scanf("%d",&ch);
switch(ch)
{
case 1: a++; break;
case 2: b++; break;
case 3: c++; break;
case 4: d++; break;
case 5: e++; break;

default:
printf("Spoiled bollet \n");

}

}
printf("\nThe no. of vote of candidate 1 is: %d",a);
printf("\nThe no. of vote of candidate 2 is: %d",b); 
printf("\nThe no. of vote of candidate 3 is: %d",c); 
printf("\nThe no. of vote of candidate 4 is: %d",d); 
printf("\nThe no. of vote of candidate 5 is: %d",e); 
getch();

}

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top