Saturday, 15 October 2011

(l) If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item.


#include<conio.h>
#include<stdio.h>
int main()
{
    float cost, sale, costitem, profit;

    printf("Please enter total selling price of 15 items: ");
    scanf("%f",&sale);
   
    printf("\nNow enter total profit on 15 items: ");
    scanf("%f",&profit);
   
    //Calculations
    cost = sale - profit;
    costitem = cost / 15;

   
    printf("\nCost price per item: %f",costitem);
   
    getche();
}



(m) If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example if the number that is input is 12391 then the output should be displayed as 23402.

/* This program takes a five-digit integer from user, and shows its reverse.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
/*Declaring and initializing variables*/
    int value,dig1,dig2,dig3,dig4,dig5,temp;

/*prompt to take the input*/
    printf("Please enter a five-digit number: ");

/*Taking input*/  
    scanf("%d",&value);
    temp = value ;

/*Calculations*/
    //separating digits 
    dig1 = value % 10;
    value = value / 10;
    dig2 = value % 10;
    value = value / 10;
    dig3 = value % 10;
    value = value / 10;
    dig4 = value % 10;
    value = value / 10;
    dig5 = value % 10;
    
    //adding 1 to each digit
    dig1 = ((dig1+1)%10);
    dig2 = ((dig2+1)%10);
    dig3 = ((dig3+1)%10);
    dig4 = ((dig4+1)%10);
    dig5 = ((dig5+1)%10);

/*showing Results*/  
    printf("\n   Solution: ");
    printf("\n   __________");
    printf("\n\n   By adding one in each digit of %d it becomes : %d%d%d%d%d",temp,dig5,dig4,dig3,dig2,dig1);
   
    getch();
}

  

  


Friday, 14 October 2011

Chapter 2: [A] What would be the output of the following programs:


(a) 

main( )
{
    int   a = 300, b, c ;
    if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
}

output

garbage-value 200




(b)

main( )
{
    int   a = 500, b, c ;
    if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
}

output

200 300



(c)

main( ) 
{ 
    int x = 10, y = 20 ; 
    if ( x == y ) ; 
    printf ( "\n%d %d", x, y ) ; 
} 

output

10  20



(d)

main( ) 
{ 
   int x = 3, y = 5 ; 
   if ( x == 3 ) 
   printf ( "\n%d", x ) ; 
   else ; 
   printf ( "\n%d", y ) ; 
} 

output  
 
3 
5





(e) 

main( ) 
{ 
    int x = 3 ; 
    float y = 3.0 ; 
    if ( x == y ) 
    printf ( "\nx and y are equal" ) ; 
    else
    printf ( "\nx and y are not equal" ) ;
} 


output

x and y are equal



(g) 

main( ) 
{ 
   int k = 35 ; 
   printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ; 
} 

output

0  50  0



(h)

main( ) 
{ 
int i = 65 ; 
char j = 'A' ; 
if ( i == j ) 
printf ( “C is WOW” ) ; 
else 
printf( "C is a headache" ) ; 
} 

output 

C is WOW 




(i) 

main( ) 
{ 
   int a = 5, b, c ; 
   b = a = 15 ; 
   c = a < 15 ; 
   printf ( "\na = %d b = %d c = %d", a, b, c ) ;
} 


output

a = 15 b = 15 c = 0




(j) 

main( ) 
{ 
   int x = 15 ; 
   printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ; 
} 


output

1  20  1



Chapter 2: [B] Point out the errors, if any, in the following programs:



(a) 

main( )
{
    float   a = 12.25, b = 12.52 ;
    if ( a = b )
       printf ( "\na and b are equal" ) ;
}

No syntax error however logical error can occur due to missing relational operator == inside if condition.

 
 
(b)  

main( )
{
  int   j = 10, k = 12 ;
  if ( k >= j )
 {
   {
      k = j ;
      j = k ;
   }
 }
} 

No Error



(c)

main( )
{
    if ( 'X' < 'x' )
    printf ( "\nascii value of X is smaller than that of x" ) ;
} 

No Error



(d)  

 main( )
{
    int  x = 10 ;
    if ( x >= 2 ) then
       printf ( "\n%d", x ) ;
}

then is not a keyword in c



(e)

main( )
{
    int  x = 10 ;
    if x >= 2
       printf ( "\n%d", x ) ;
}


condition is not enclosed in braces 


  
(f)

main( )
{
    int  x = 10, y = 15 ;
    if ( x % 2 = y % 3 )
          printf ( "\nCarpathians" ) ;
}


Relational operator (==) is missing inside condition.



(g)
 
main( )
{
    int x = 30 , y = 40 ;
    if ( x == y )
       printf( "x is equal to y" ) ;
    elseif ( x > y ) 
       printf( "x is greater than y" ) ;
    elseif ( x < y )
       printf( "x is less than y" ) ;
}


There should be a space between else and if

(h) 

main( )
{
    int  x = 10 ;
    if ( x >= 2 ) then
       printf ( "\n%d", x ) ;
}

then is not a keyword in c.


(i)

main( )
{
    int a, b ;
       scanf ( "%d %d",a, b ) ;
    if ( a > b ) ;
       printf ( "This is a game" ) ;
    else
       printf ( "You have to play it" ) ;
}

& missing inside scanf