Monday 10 October 2011

10-10-2011 do-while loop (prints counting)


/* This program uses do while loop and prints counting from 1 to 5*/
#include <stdio.h>
#include <conio.h>
int main()
{
    int num = 1;
    do
    {
          printf("%d\n",num);
          num++ ;
    }
    while(num <=5);
    getche();
    return 0;
}

10-10-2011 do-while loop (even odd)




/*program uses do-while loop and prints even numbers as they are entered but exits if an odd number is entered.*/

#include <stdio.h>
#include <conio.h>
int main()
{
    int num;
    do
    {
          scanf("%d",&num);
          if (num%2 !=1 )
                    printf("%d\n",num);
    }
    while (num %2 != 1);
    getche();
    return 0;
}

10-10-2011 Continue Statment


/*Just an example of continue statmenrt.*/


#include <stdio.h>
#include <conio.h>
int main()
{
    int val;
    for(val = 1 ; val <=10 ; val++)
    {
            if (val == 3)
            {
            continue;
            }
            printf("%d ",val);
            }
            getche();
            return 0;
   
   
}


/*Output will be the counting from 1 to 10 except 3.*/

10-10-2011 break statment



/*Just an example of break statmenrt.*/

#include <stdio.h>
#include <conio.h>
int main()
{
    int val;
    for(val = 1 ; val <=10 ; val++)
    {
            if (val == 5)
            {
            break;
            }
            printf("%d ",val);
            }
            getche();
            return 0;
   
   
}

/*Output will be the counting from 1 to 4.*/

10-10-2011 for loop (average)



program calculates average of 100 random values entered by user and if -1 is entered then program terminates at once and shows the average of values, entered until then.
(Uses for loop).
#include <stdio.h>
#include <conio.h>
int main()
{
    int val = 0, count = 0; 
    float sum = 0, avg = 0;
    for ( count = 0 ; count <= 100 ; count++)
    {
        if(val != (-1))
        {
       
        printf("Please enter the %d digit: ",count +1);
        scanf("%d",&val);
                 if(val != (-1))
                 {
                         sum = sum + val;
                 }
                 else
                 break;
       }
       else
       break;
      
    }
    printf("\nSum of entered digits is: %f\n",sum);
    printf("\nTotal number of digits entered are: %d\n",count);
    avg = sum/count;
    printf("\nThe average of entered digits is: %f\n",avg);
    getche();
    return 0;
}

10-10-2011 switch statement


/*Just an example of usage of switch statement*/

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
   
    printf("Enter a number: ");
    scanf("%d", &x);
   
    switch(x)
    {
         case 1:
              printf("good");
              break;
         case 2:
              printf("fair");
              break;
         default:
              printf("bad");
              break;
    }
   
    getche();
    return 0;
}

10-10-2011 A Very Simple Calculator


/*Can say this program a simple Calculator*/
#include <stdio.h>
#include <conio.h>
int main()
{
   
    int dig1 = 0 , dig2 = 0 , opert;
    /*Declaring veriables in float for division operation*/
       float dig1f = 0, dig2f = 0;
    while (opert != 5)
        {
            printf("Please enter first digit: ");
            scanf("%d",&dig1);
            printf("\nPlease enter 2nd the digit: ");
            scanf("%d",&dig2);
            /*assigning same values to float variables*/
                     dig1f = dig1;
                     dig2f = dig2;
           
                     /*Showing Instructions*/
                     printf("\n\n\t\tInstructions\n");
            printf("\n______________________________________________\n");
            printf("\nTo add both digits enter 1\n");
            printf("\nTo subtract 2nd digit from first enter 2\n");
            printf("\nto multiply both digits enter 3\n");
            printf("\nTo divide first digit by 2nd enter 4\n");
            printf("\nTo quit enter 5\n\n");
           
                     /*Taking input for operation*/
                     scanf("%d",&opert);
                     /*Printing results*/
                     switch (opert)
            {
                case 1:
                     printf("\n\n%d + %d = %d\n\n\n",dig1, dig2, dig1 + dig2);
                     break;
                case 2: 
                     printf("\n\n%d - %d = %d\n\n\n",dig1, dig2, dig1 - dig2);
                     break;
                case 3:    
                     printf("\n\n%d x %d = %d\n\n\n",dig1, dig2,dig1 * dig2);
                     break;
                case 4:    
                     printf("\n\n%f / %f = %f\n\n\n",dig1f, dig2f, dig1f / dig2f);
                     break;
                
            }
                     /*rerunning the loop to rerun program*/
            continue;
        }   
    getche();
    return 0;
}

10-10-2011 last task (Averagre Modified)


#include <stdio.h>
#include <conio.h>
int main()
{
    int val = 0, count = 0; 
    float sum = 0, avg = 0;
    printf("Enter zero after entering all digits to get average.\n\n");
    do
    {
       
        printf("Please enter the %d digit: ",count +1);
        scanf("%d",&val);
       
        /*adding values if they are greater than zero*/
        if(val > 0)
        {
               sum = sum + val;
        }
        else
        {
               break;
        }
        /*counting entered digits*/
        count++;
    }
    while(val > 0);
   
    /*printing results*/
    if (val == 0 && count != 0)
    {
        printf("\nSum of entered digits is: %f\n",sum);
        printf("\nTotal number of digits entered are: %d\n",count);
        avg = sum/count;
        printf("\nThe average of entered digits is: %f\n",avg);
    }
    else if (val == 0 && count == 0)
    {
         printf("\nNo average!");
    }
    else
        printf("\nError");
   
    getche();
    return 0;
}