Monday 3 October 2011

03-10-2011 Usage of Logical Operators(smallest and highest number)


/*Program takes values from user and tells which one was highest and which one was smallest*/
#include<stdio.h>
#include<conio.h>
main()
{
      int num = 0, smallest=0, highest = 0, counter = 0;
      while (num != -1)
      {
            printf("Please enter the number or -1 to see results: ");
            scanf("%d",&num);
            if ((smallest >= num && num !=-1) || (counter==0))
            {
               smallest = num;
            }
            if ((highest <= num && num != -1)||(counter == 0))
            {
                 highest = num;
            }
            counter++;
      }
      printf("\nSmallest entered value is: %d \nand the highest entered value is: %d \n\nSo the range is %d",smallest,highest,(highest-smallest));
      getche();
}          
           


03-10-2011 Calculation of Power of a digit


/*This program calculates the power of a digit upto another user entered digit*/
#include<stdio.h>
#include<conio.h>
main()
{
      int digit=1, power=1, temp=1, counter;
      printf("Please enter the digit: ");
      scanf("%d",&digit);
      printf("now enter its power: ");
      scanf("%d",&power);
      temp=digit;

      for(counter = 1; counter < power ; counter++ )
          {
          temp = temp * digit;
          }    
      printf("%d raised to power %d answers: %d",digit,power,temp);
      getche();
}
     



     

03-10-2011 Example of Usage of Conditional Operators


/*This program takes marks of student, uses conditional operators to check them and shows either he is pass or fail*/
#include<stdio.h>
#include<conio.h>
main()
{
      int numbers = 0;
      printf("Please enter marks: ");
      scanf("%d", &numbers);
      (numbers >= 60)? printf( "Pass") : printf("Fail");
      getche();
}
/*
is same as
        int numbers = 0;
      printf("Please enter marks: ");
      scanf("%d", &numbers);
      if (numbers >= 60)
         {
         printf("Pass");
         }
      else
          {
          printf("Fail");
          }
      getche();
      return 0;
     
      */


03-10-2011 Example of Usage of if else

/*This program takes marks of student and shows either he is pass or fail*/
#include<stdio.h>
#include<conio.h>
main()
{
      int numbers = 0;
      printf("Please enter marks: ");
      scanf("%d", &numbers);
      if (numbers >= 60)
         {
         printf("Pass");
         }
      else
          {
          printf("Fail");
          }
      getche();
      return 0;

}
    



03-10-2011 Working of Increament and Short Hand Operators


/*Just an example of usage of increment operators and short hand operators*/
#include<stdio.h>
#include<conio.h>
main()
{
      int a = 1, b = 1, c = 1, d = 1 ;
      printf("\n a prints: %d when a = 1",a);
      printf("\na++ prints: %d when a =1",a++);
      printf("\n++b prints: %d when b=1",++b);
      printf("\nwhen a = %d and b = %d then a+=b used inside printf prints: %d",a,b,(a+=b));
      c=a;
      d=b;
      b+=a;
      printf("\nwhen a = %d and b = %d then b+=a used before printf prints: %d",c,d,(b+=a));
      getche();
}

03-10-2011 Checking Problems by No Initialization


/*no Initialization can make logical errors*/
#include<stdio.h>
#include<conio.h>
main()
{
      int a;
      printf("%d",a++);
      getche();
}


03-10-2011 Increament Operator


/*This program prints a++ then a when a is initialized with 1*/
#include<stdio.h>
#include<conio.h>
main()
{
      int a = 1;
      printf("a++ prints: %d when a = 1",a++);
      printf("\nafter doing a++ 'a' prints: %d",a);
      getche();
}