Tuesday, November 1, 2011

Type-cast operator

The type cast operator is very important in C. Cast operator uses in convert one data type to another data types. Type casting may be two types:
  1. Implicit type cast
  2. Explicit type cast
Implicit type cast

In C, implicit type cast are automatically handled by compiler i.e. when two or more data types are getting execution then the final data-type will be that data type as it is declared, i.e it is not depend on conversion of data type.
It is clear understand by example as:

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int i,j;
  float f;
  double d;
  i=d*f+f*j;
 }

what you think, what will be data type of i?
it is double!! No, right answer is int. You see in program that double has high priority or  precedence of float and int, so result of data type will be comes in double but when result is assign in i, it will be convert in int because i is declared as int. It is implicit type casting.

Explicit type cast

An explicit type cast is a cast that we should specify invoke with either the cast. The compiler does not automatically invoke to resolve the data type conversion.
Let's understand explicit with example:

/*A student marks of three subject as m1,m2,m3 and calculate percentage(per)*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int m1=70,m2=70,m3=100,total;
 float per;
 total=m1+m2+m3;
 per=total/300*100;
 printf("%f",per);
}
output:- 0.000000

Surprise, let's explain to me, it is a common type cast mistake, look at per=total/300*100; statement. In this statement first of all total/300 will be solve so total(240) and 300 are int hence 240/300 will be produce a float value so result is 0.000000. These mistake may me solved by three ways as:

1. We mention type cast in above program as:
 /*demonstration of type casting*/

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int m1=70,m2=70,m3=100,total;
  float per;
  total=m1+m2+m3;
  per=(float)total/300*100;
  printf("%f",per);
 }

 output:- 80.000000


2. We used total as float variable.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int m1=70,m2=70,m3=100;
  float per,total;
  total=m1+m2+m3;
  per=total/300*100;
  printf("%f",per);
 }

 output:- 80.000000


3. we convert int to float to the 300 by adding decimal portion.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int m1=70,m2=70,m3=100,total;
  float per;
  total=m1+m2+m3;
  per=total/300.0*100;
  printf("%f",per);
 }

 output:- 80.000000


Monday, October 31, 2011

Quadrilateral pyramid

Q. Write a program in C to display the following output:



1




2 1 2


3 2 1 2 3
4 3 2 1 2 3 4

3 2 1 2 3


2 1 2




1



Ans.
/*c program for quadrilateral number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num=4,r,c,z,sp;
 for(r=1; num>=r; r++)
 {
   for(sp=num-r; sp>=1; sp--)
      printf(" ");
   for(c=r; c>=1; c--)
      printf("%d",c);
   for(z=2; z<=r; z++)
      printf("%d",z);
   printf("\n");
 }
 for(r=1; num>=r; r++)
 {
   for(sp=r; sp>=1; sp--)
      printf(" ");
   for(c=num-r; c>=1; c--)
      printf("%d",c);
   for(z=2; z<=num-r; z++)
      printf("%d",z);
   printf("\n");
 }
 getch();
 return 0;
}


/******************Output*****************



1




212


32123
4321234

32123


212




1


***************************************/

Saturday, October 22, 2011

programmable logical array

hi! guys I am a student of b.tech computer science...and have got a project work on progrmmable logical array(PLA)..which I do not find an easy task... so all I want to say is...."please arrange me a very easy C language code and I have to submit it before 26 oct 2011 so please do it fast..I am totally counting on you" try to make the programme easy and short and it must be on C...no other

Friday, October 21, 2011

switch-case statement

In some programming situation, when there are number of choices and we want to choose only appropriate choice, in that situation C provided switch statement. Thus switch statement allows us to make a decision from the number of choices.
The switch statement also known as switch-case-default.
Since, the switch statement known for decision maker.
Syntax of switch statement:

switch(integer expression)
{
 case 1 :
   do this;
   break;
 case 2 :
   do this;
   break;
 case 3 :
   do this;
   break;
 case 4 :
   do this;
   break;
 default :
   and this;
   break;
}

Example of switch-case-default statement:

/*program to demonstration of switch statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {

  int r=4;
  switch(r)
  {
   case 1 :
     printf("\nI am in case 1");
     break;
   case 2 :
     printf("\nI am in case 2");
     break;
   case 3 :
     printf("\nI am in case 3");
     break;
   case 4 :
     printf("\nI am in case 4");
     break;
   case 5 :
     printf("\nI am in case 5");
     break;
   default :
     printf("\nI am in default");
     break;
  }
  getch();
 return 0;
 }


Output: I am in case 4

Different types of switch statement

(a) In switch statement case arrangement may be ascending order, descending order, scrambled order. So the following program should be valid and run:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int r=44;
  switch(r)
  {
   case 210 :
     printf("\nI am in case 210");
     break;
   case 10 :
     printf("\nI am in case 10");
     break;
   case 1 :
     printf("\nI am in case 1");
     break;
   case 44 :
     printf("\nI am in case 44");
     break;
   default :
     printf("\nI am in defalut");
     break;
  }
  getch();
 return 0;
 }
Output: I am in case 44

(b) C also allowed to use char values in case and switch statement as:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  char a='n';
  switch(a)
  {
    case 'm' :
      printf("\nI am in case m"); 
    case 'n' :
      printf("\nI am in case n");
    case 'o' :
      printf("\nI am in case o");
    case 'p' :
      printf("\nI am in case p");
    default :
      printf("\nI am in default");
  }
  getch();
  return 0;
 }

Output: I am in case n

Note: When above program run, 'm', 'n', 'o', 'p' are actually replaced by subsequent ASCII(American Standard Code for Information Interchange) code.

(c) C language allows us to check the value of any expression in a switch statement like as:
   switch(r+c*i)
   switch(10+20%2*r) 
   switch(r+10*c)
   switch(r>10 || c<20) 
Above all switch statement are valid.
And expression can also be used in cases provided they are constant expression.
Thus case 10+15 is correct,
and however case r+c is incorrect.

(d) The switch statement is very useful while writing menu driven program.

(e) The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch.

(f) The case keyword is followed by an integer or a character constant.

continue statement

when we write program, if we want to take control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed, in this situation we uses continue. continue is c reserved keyword in C. When continue is encountered inside any loop, control automatically passes to the beginning to the loop.
A continue is usually associated with an if.
Let's understand continue with example:

/*demonstration of continue statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int i,j;
  for(i=1; i<=2; i++)
  {
   for(j=1; j<=2; j++)
   {
    if(i==j)
     continue;
    printf("\n%d %d",i,j);
   }
  }
  getch();
  return 0;
 }

Output:
 1 2
 2 1

Explanation: When the value of i equals that of j, the continue statement takes the control to the for loop(inner) bypassing the rest of the statements execution in the for loop(inner).

Let's understand continue statement with another example:

/*demonstration of continue statement*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 for(i=1; i<=10; i++)
 {
   if(i==6)
     continue;
   printf("%d ",i);
 }
 printf("\nNow we are out of for loop");
 getch();
 return 0;
}


Output:-

 1 2 3 4 5 7 8 9 10
 Now we are out of for loop

Explanation: As when the value of i will becomes 6 it will move for the next iteration by skipping the iteration i=6.

break statement

When we writing programming code, we often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
The keyword break, breaks the control only from the while in which it is placed.
Let's understand break with c program example:

/*demonstration of break statement through c program*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int i=5;
 clrscr();
 while(i>=1)
 {
  if(i==2)
  {
   printf("\nIt is equal to two!!");
   break;
  }
  printf("%d\n",i);
  i--;
 }
 getch();
}

Output:
5
4
3
It is equal to two!!

Thus, it is clear that a break statement takes the execution control out of the loop.
In above program, if we omitted break statement then what will be output? The answer is
5
4
3
It is equal to two!!
2
1


because when i's value is 2, condition will be satisfy and executed printf statement, after that compiler goes to next statement i.e. printf("%d",i); because loop do not terminate and it is run till the i value is not less than 1.
See more example of break statement

Thursday, October 20, 2011

Number seriess


/*program to print number series as
1 4 9 25....100 */
#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1,y=2,z,s;
 for(; x<=8; )
 {
   s=x*x;
   printf(" %d",s);
   z=x+y;
   x=y;
   y=z;
 }
 getch();
 return 0;
}

Output:1 4 9 25 64

/*program of print following number series
-10 -8 -6 -4 -2 0 2 4 6 8 10 */
#include<stdio.h>
#include<conio.h>
int main()
{
 int x;
 for(x=-10; x<=10;x=x+2)
    printf(" %d",x);
 getch();
 return 0;
}
Output:  -10 -8 -6 -4 -2 0 2 4 6 8 10