Monday 17 October 2011

17-10-2011 (1st) Function Calls


/*Example to understand calling of functions*/
#include<stdio.h>
#include<conio.h>
void abc();
void xyz();
void mno();
main()
{
      printf("I am in main()\n");
      abc();
      printf("Now I am back in main()");
      getche();
      return 0;
}
void abc()
{
     printf("Now I am in abc()\n");
     xyz();
     mno();
}

void xyz()
{
     printf("Now I am in xyz()\n");
}

void mno()
{
     printf("Now I am in mno()\n");
}


17-10-2011 Sum Using a function

/*program uses a function to add two digits*/
#include<stdio.h>
#include<conio.h>
int sum(int,int);
main()
{
      int dig1,dig2,result;
      dig1 = 10;
      dig2 =5;
     
      /*calling function and storing rturned value in variable result*/
      result = sum(dig1,dig2);
      printf("%d + %d = %d",dig1,dig2,result);
      getche();
      return 0;
}

/*Defining functions*/
int sum(int digi1, int digi2)
{
    int add;
    add = digi1 + digi2;
    return add;
}
     
     
     


17-10-2011 Sum Using a function(function changes values before adding)


/*program uses a function to add two digits but function changes passed values before adding them*/
#include<stdio.h>
#include<conio.h>
int sum(int,int);
main()
{
      int dig1,dig2,result;
      dig1 = 10;
      dig2 =5;
      /*calling function and storing rturned value in variable result*/
      result = sum(dig1,dig2);
      printf("%d",result);
      getche();
      return 0;
}

/*Defining functions*/
int sum(int digi1, int digi2)
{
    digi1 =2;
    digi2 =3;
    int add;
    add = digi1 + digi2;
    return add;
}
     
     
     


17-10-2011 Division Using a function

/*program uses a fubction to divide one digit by another*/
#include<stdio.h>
#include<conio.h>
/*declaring function*/
float div(float,float);
main()
{
      float dig1=0,dig2=0,result=0;
      printf("Please enter a number: ");
      scanf("%f",&dig1);
     
       while(dig2 == 0)
       {
                  printf("\n\n\nNow enter a number to divide first digit: ");
                  scanf("%f",&dig2);
                  if(dig2==0)
                  {
                       printf("\tWarning!\nDenominator can not be zero.");
                   }
                 
       }
       /*calling function and storing rturned value in variable result*/
      result = div(dig1,dig2);
      printf("%f / %f = %f",dig1,dig2,result);
      getche();
      return 0;
}
/*Defining function*/
float div(float digi1, float digi2 )
{
    float opert;
    opert = digi1 / digi2;
    return opert;
}
     
     
     


17-10-2011 Cheking of Greatest number Using a function


/* This program takes three integers from user, calls a function to compares which one is greatest
and shows results inside that function.*/
#include<stdio.h>
#include<conio.h>
/*declaring function*/
void check(int, int, int);
int main ()
{
    /*declaring veriables*/
    int num11, num22, num33;
  
    /*prompt to take the input*/
    printf("Please enter three integers, remember pressing enter after every one: \n");
   
    /*taking input*/
    scanf("%d%d%d",&num11,&num22,&num33);
    check(num11, num22,num33);
    getche();
    return 0;
}
/*Defining function*/
void check(int num1, int num2,int num3)
{
    /*comparison and printing results*/
    printf("\n\nResult: \n");
    printf("___________\n\n");
    if (num1 > num2 && num1 > num3)
    {
         printf("%d is greatest!",num1);
    }
    else if (num2 > num1 && num2 > num3)
    {
         printf("%d is greatest!",num2);
    }
    else if (num3 > num1 && num3 > num2)
    {
         printf("%d is greatest!",num3);
    }
    else if (num1 == num2 && num1 == num3)
    {
         printf("All of the given digits are equal.");
    }
    else
    {
         printf("This is some other case not defined in the program!");
    }
   
}


17-10-2011 Calculator (Calls Functions to Do Operations)

/* A simple calculator which calls function to do operations*/
#include <stdio.h>
#include <conio.h>
/*declaring function*/
float add(float,float);
float sub(float,float);
float mult(float,float);
float div(float,float);

int main()
{
    char exit = ' ';
    do
        {
               
            float dig1 = 0 , dig2 = 0 ;
            char opert;
            printf("Please enter first digit: ");
            scanf("%f",&dig1);
            printf("\n\nNow enter operation to perform: ");
            scanf("%c",&opert);
            opert=getche();
                  
                   /*if operation is division then implementing cheks and conditions to stop entering denominator as zero*/
            if (opert=='/')
            {
                while(dig2 == 0)
                {
                printf("\n\n\nNow enter 2nd the digit: ");
                scanf("%f",&dig2);
                    if(dig2==0)
                    {
                               printf("\tWarning!\nDenominator can not be zero.");
                    }
                }
            }
            else
            {
                printf("\n\n\nNow enter 2nd the digit: ");
                scanf("%f",&dig2);
            }
                           
            /*Checking operation and calling function related to operation*/       
            switch (opert)
            {
                case '+':
                add(dig1,dig2);
                break;
                case '-':
                sub(dig1,dig2); 
                break;
               
                case '*':    
                mult(dig1,dig2);
                break;
                                
                case '/':    
                div(dig1,dig2);
                break;
                default :
                printf("\t\n\nWarning!\n Wrong operation entered.");
            }
            exit = ' ';
            /*To continue or exit*/
            while(exit !='e' && exit != 'c' && exit !='E' && exit != 'C')
            {
                printf("\n\nEnter 'e' to exit or 'c' to continue using program: ");
                scanf("%c",&exit);
                exit = getche();
                printf("\n\n");
            }
        }
        while (exit != 'e' && exit != 'E');   
   
    getche();
    return 0;
}


/*Defining functions*/
/*addition function*/
float add(float digi1add, float digi2add )
{
    printf("\n\n%f + %f = %f\n\n\n",digi1add, digi2add, digi1add + digi2add);
}

/*subtraction function*/
float sub(float digi1sub, float digi2sub )
{
    printf("\n\n%f - %f = %f\n\n\n",digi1sub, digi2sub, digi1sub - digi2sub);
}

/*multiplication function*/
float mult(float digi1mul, float digi2mult )
{
     printf("\n\n%f x %f = %f\n\n\n",digi1mul, digi2mult, digi1mul * digi2mult );
}

/*division function*/
float div(float digi1div, float digi2div )
{
     
    printf("\n\n%f / %f = %f\n\n\n",digi1div, digi2div, digi1div / digi2div);
}


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.*/