Skip to main content

Structured Programming - set 2- 2015 solve


Set:2

Question(a):

Explain the following  operation with example:----

i.                  Logical operator

ii.               Relational operator

iii.            Increment and decrement operator

iv.             Size of and conditional operator

Answer:

LOGICAL OPERATORS :  These  operators  are  used  to  perform  logical  operations  on the given expressions.

There  are  3  logical operators  in  C  language. They  are, logical  AND (&&),  logical   OR (||)  and   logical  NOT (!).

&& (logical AND) : (x > 5 )  &&  (y  <  5)

It  returns  true  when  both  conditions  are true

|| (logical OR) : (x  >=  10) || (y  >= 10)

It returns true when at-least one of the condition is true.

! (logical NOT) : !((x > 5)  && (y < 5))

It reverses the state of the operand “((x > 5) && (y  < 5))”

If “((x > 5) && (y < 5))” is true, logical NOT operator makes it false.

Increment  and  decrement operator :  C has two special  unary  operators  called  increment  (++)   and decrement (--)  operators. These  operators  increment and decrement value of a variable by 1.

++x  is  same  as  x = x + 1  or  x += 1

--x   is same as  x = x - 1 or  x -= 1

Increment  and  decrement  operators  can be used  only  with variables. They can’t  be  used  with  constants  or expressions.

Relational operator:  decision  depending  on  their  relation  are  called relational  operator.  Some  relational  operators are  The  operators  which are  used to compare  two  numbers  take.

 

1:  (<) means less than

2:  (<=) means less than or equal to

3:  (>) means greater than

4:  (>=) means greater than or equal to

5:  (==) means equal to

6:  (!=) means not equal to

An expression such as: 10<20 , 15<=20 , 20>10  ,  10>9 , 5==5  , 5!=6

Size of operator: e: The size of operator is usually used with an operand which may be variable,constant or data type qualifier.This operator returns the number of bytes the operand occupies.Sizeof operator is a compile time operator.Some examplea of use of sizeof operator are

X  =  sizeof(a);

Y  =  sizeof(float);

The size of operator is usually used to determine the length of arrays and structures when their sizes are not known.It is also used in dynamic memory allocation.


Question(b):

write  a C program to  convert  the given temperature in Fahrenheit  to  temperature  in Celsius  where  cel =( (fah-32)*5/9) and  Fahrenheit  is  denoted  by  fah .

Answer :



scroll left-right to get the full program code in mobile screen
#include<stdio.h>
#include<conio.h>
void main()
{
    float fah,cel;

    printf("Enter temperature in Fahrenheit:");

    scanf("%f",&fah);

    cel = (fah - 32)/1.8;

    printf("Temperature in Celsius:%.2f\n",cel);
    
    getch();
}



Question(c):Describe the output of the following C program.




#include<stdio.h>
#include<conio.h>
void main()
{
    float fah,cel;

    printf("Enter temperature in Fahrenheit:");

    scanf("%f",&fah);

    cel = (fah - 32)/1.8;

    printf("Temperature in Celsius:%.2f\n",cel);
    
    getch();
}


ANSWER:

In this  following  program the output  must be an infinite value because  in this program the while (k,25) it is an infinite loop which run till  a break  statement is issued explicitly. In most computer programming language a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition .The boolean condition is either true or false .


While(1)  it is an infinite loop which will run till a break statement issued explicitly .Interestingly not while( 1 ) but any integer which is

non-zero will give the similer effect as while( 1 ) .Therefore ,while(1),while(2)or while(k,25) ,all will give infinite loop only.

 

So if the following statement has a break statement it will print a normal output or else it will print  an infinite output because the compiler cannot understand where to stop so its exequte repeatedly and print the output which is infinite.



Question(d):

 What do you mean by a loop? Explain the deference between do loop, while loop  and  for loop with the help of example.

Answer:

Loop:  In  computer programming, a loop is a  sequence of instructions that is continually  repeated  until  a  certain condition  is  reached.

scroll left-right to get the full table in mobile screen

N

o

    Topic

       Do loop

     While loop

          For loop

 

1.

Initialization of  condition

variable

Before the loop

or in the body of

the loop.

Before the loop

In  the  parenthesis of  the  loop

2.

Test

condition

After  the body of  the loop

Before the body

of the loop

Before  the  body of the loop

 

3.

Updating

the condition variable

After the first

execution

After the first

execution

After the first

execution

4.

Types

Exit controlled

loop

Entry controlled

loop

Entry controlled loop

5.

Loop

variable

Sentinel and

Counter

Counter

Counter

 

 

6.

Syntax

do

{

……..

……..

}

while(condition)

Variable

initialization;

while(condition)

{

Statements;

}

Variable

increment or

decrement;

for(initialization;

condition;

increment/decrement

)

{

Statement -block;

}

7.

Example

#include<stdio.h>

Int main()

{

int a, i;

a = 5;

i = 1;

do

{

printf(%d\t”,a*i);

i++;

}

while(i<= 10)

return 0;

}

#include<stdio.h>

 

Int main( )

{

int x;

x = 1;

while(x <= 10)

{

printf(“%d\t”, x);

x++;

}

return 0;

}

#include<stdio.h>

Int main( )

{

int x;

for(x = 1; x <= 10;

x++)

{

printf(“%d\t”, x);

}

return 0;

}

 


Question(e): Replace  the  if-else statement by conditional operators:---


main(){

int code;

scanf(“%d”,&code);

if(code>1)

           pritnf(“\n DHAKA”);

else if(code<1)

                 printf(“\n COMILLA”);

else

     printf(“ \n GAZIPUR”);

}





scroll left-right to get the full program code in mobile screen
#include<stdio.h>
#include<conio.h>
void main()
{
    float fah,cel;

    printf("Enter temperature in Fahrenheit:");

    scanf("%f",&fah);

    cel = (fah - 32)/1.8;

    printf("Temperature in Celsius:%.2f\n",cel);
    
    getch( );
}