Blogger templates

Pages

Showing posts with label C PROGRAMS. Show all posts
Showing posts with label C PROGRAMS. Show all posts

Friday, 31 August 2012

Explanation of Local and Global Variables with Example

Local and Global variables:
Local and Global variables:

Local variables: 
Variable whose existence is known only to the main program or functions are called local variables. Local variables are declared with in the main program or a function.

Global variables: 
Variables whose existence is known to the both main() as well as other functions are called global variables. Global variables are declared outside the main() and other functions.
The following Program illustrates the concept of both local as well as global variables.

Statement of C Program: This program does not accept anything from the keyboard. It initialises the variables a to 10 outside the main() function and value().

#include<stdio.h>
int a = 10;                                 /* global declaration */
main()
{
int b;
printf("  = %d\n" , i);
b = value(i)
printf(" j = %d\n");
}                                                              /* End of main() */
                    /* Function to compute value */
int value(i)
int i;
{
int c;
c = i + 10;
return(c);
}                                           /* End of Function */

Output:
 i = 10
 b = 20

Explanation:
The statement int  i = 10;   that appears before the main() is a global declaration. The value of i is accessed by the main program as well as the function value(). The variable c is local to the function value(). It has no existence in the function value(). The variable b is also local to the main() but has no scope in the function value().

                                                                  That's All

Wednesday, 15 August 2012

WAP of C Language that illustrates the Function with no arguments and no Return values


Functions with no Arguments and no Return values:
The called Function does not receive any data from the calling Function and it does not return any data back to the calling Function. Hence, there is no data transfer between the calling Function and called Function. So, let us understand this with the help of Program:

Statement of C Program: This Program illustrates the Function with no Arguments and no Return value:
#include<stdio.h>
#include<conio.h>
void main()
{
read_value();
}
read_value()
{
char name[10];
printf(" Enter your name\n");
scanf("%s" , name);
printf(" Your name is %s , name);
}

Output:
Enter your name
shubham
Your name is shubham

                                                                  That's All

WAP of C Language that illustrates the Function with no Arguments and Return values

Category of Functions

Functions with no Arguments and Return value:
The called Function does not recieve any data from the calling Function. It is also a one-way data communication between the calling Function and the called Function. So let us understand this with the help of Program:

Statement of C Program: This Program illustrates the Function with no Arguments and Return values:

#include<stdio.h>
#include<conio.h>
void main()
{
float sum;
float total();
clrscr();
sum = total();
printf(" Sum = %f\n" , sum);
}
float total()
{
float a, b;
a = 5.0 ;
b = 15.0 ;
return(a+b);
}

Output:
Sum = 20.000000

                                                                    That's All

Tuesday, 14 August 2012

Detailed Discription of Category of Functions with Examples

Category of Functions

Category of Functions:
We have some other type of Functions where arguments and return value may be present or absent. Such functions can be categorised into:
  1. Function with arguments but no return value.
  2. Function with no arguments and return value.
  3. Functions with no arguments and no return values

So Let's have a look on Category of Functions one by one:

1. Function with Arguments but no return value: 
Here the called Function recieves the data from the calling function but the called function does not return any value back to the calling Function.So, let us understand with the help of Program:

Statement of C Program: This Program illustrates the Function with arguments but no return value:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
printf(" Enter the value of a and b\n");
scanf("%d%d" , &a ,&b);
largest(a, b);
}

/* Function to compute largest */

largest(c, d)
int c, d;
{
if(c>d)
{
printf(" Largest = %d\n");
}
else                                                    /* (c<d) */
{
printf(" Largest = %d\n");
}
return();
}

Output:
Enter the values of a and b
20
15
Largest = 20

                                     Function with no Arguments and Return Values                                

                                  Functions with no Arguments and no Return Values                             

Thursday, 9 August 2012

WAP of C Languge Function that Calculates the Square of a accepted number

Square

Arguments and Parameters:
Arguments and parameters are variables used in a Function. Variables used in a Function calling( Function reference) are called Arguments. These are written within the parentheses followed by the name of the Function.
Variables used in the Function definition are called Parameters. Parameters must be written within the parenthesis followed by the name of the Function, in the Function Definition.So let us understand Parameters and Arguments with the help of Program.

Statement of C Program: This program accepts a Number and prints its Square.

#include<stdio.h>
#include<conio.h>

void square(int i);                                   /* Function Declaration */

void main()
{
int a;
clrscr();
printf(" Enter the value of a\n");
scanf("%d" , &a);

square(a);                                                  /* Function calling */

getch();
}

void square( int i )                                   /* Function Definition*/
{
int b;
b= i*i;
printf(" square = %d\n" , b);
}
Output:
Enter the value of a
2
square = 4


                                                                   That's All

Friday, 3 August 2012

WAP to determine whether the Number is Prime or Not by using while and if Statement

C break Statement
Break Statement:
There are many situations where we want to jump out of a loop instantly. 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. So Let us understand the break statement with the help of Program :-

Statement of C Program: This Program accepts a number and determined whether the number is Prime or not.

Prime Number:
A Prime number is one, which is divisible by 1 or itself.  Ex: 3 , 5 , 7 , 11 , 13 , 17 , 23 , 19 etc.

#include<stdio.h>
#include<conio.h>
void main()
{
int a , b ;
clrscr();

printf(" Enter the value of a\n");
scanf("%d" , &a);

b = 2;
while( b <= a-1 )
{
if(a % b == 0)
{
printf(" Entered Number is not Prime\n");
break;
}
b = b + 1;                                                   /*   b++     or    b += 1   */
}
if(b = a)
{
printf(" Prime Number\n");
}
getch();
}

Output1:
Enter the value of a
7
Prime Number

Output2:
Enter the value of a
9
Entered Number is not Prime

Explanation:
  • In the above Program, if the condition { i.e a % b } turns out to be zero, then the message " Entered number is not Prime " is printed and after that the break statement encountered which causes control out of while loop.
  • If the condition { i.e b == a } is true, then the message " Prime Number " is printed.
                                                                      
                                                                    That's All

Wednesday, 1 August 2012

WAP of C Language that calls many Functions

C Function Program

Statement of C Program : This Program prints the Messages or Instructions when different Functions calls: 

#include<stdio.h>

void England();                                      /* Function declaration */
void Australia();                                     /* Function declaration */
void India();                                           /* Function declaration */

int main()
{
printf(" I am in main\n");

England();                                                 /*  Function call */
Australia();                                                /*  Function call */
India();                                                      /* Function call */

return(0);
}

void England()                                             /* Function definition */
{
printf(" I am in England\n");
}

void Australia()                                             /* Function definition */
{
prinf(" I am in Australia\n");
}

void India()                                                   /* Function definition */
{
printf(" I am in India\n");
}

Output:
I am in main
I am in England
I am in Australia
I am in India

Note:  Number of Conclusions drawn from the above Program:
  • A C Program is a collection of one or more Function or you can say A Function is a basic building block of C program.
  • If a C program contain only one Function, then it must be main(), because Program execution always begins with main().
  • In C program, as many number of Functions exist i.e there is no limit on the number of Functions.
  • Control returns to main() after each Function has doing its work or thing.
  • The Program ends, when main() runs out of statements and Function calls.

                                                               

                                                                 That's All

Friday, 27 July 2012

Write a Simple C Function Program that prints the Messages i.e Good Morning? & Good Evening!

Messages

Statement of C Program: This Program prints the Messages i.e Good Morning? & Good Evening! with the help of Function :

This is the Simplest C Function Program. Functions are the basic building blocks of C Program.

#include<stdio.h>
void msg();                                            /* Function Declaration */

int main()
{
msg();                                                         /* Function Call */
printf(" Good Morning?");
return(0);
}

void msg()                                               /* Function Definition */
{
printf(" Good Evening!");
}

 Output:
 Good Evening!
 Good Morning?

Explanation:
Above, we have defined two Functions- main() and msg(). We have used the word msg at three places in the Program. So Let us understood the meaning of each.

1. 
void msg();
This Function declaration indicates that msg() is a Function which after completing its execution does not return anything. The keyword void is used before the msg() to indicate that it 'does not return anything'.

2.
void msg()
{
printf(" Good Evening");
}
This is Function definition. In this definition we are using only printf(), but we can also use for , switch , if , while etc inside Function definition.

3. 
msg();
The Function msg() is being called by main() i.e main() calls the function msg(). We mean that control passes to the function msg().

                                                                       That's All

Thursday, 26 July 2012

WAP to Find the Largest Number among Two Numbers by using Function

Largest Number

Statement of C Program: This Program prints the Largest Number among two Numbers by using Function.

#include<stdio.h>
#include<conio.h>
void main()
{
float a , b , max;
clrscr();
float large(float x , float y);                              /* Function declaration */

printf(" Enter the value of two numbers\n");
scanf("%f%f" , &a , &b);

printf("a = %f and b = %f\n" , a, b);
max = large(a,b);
printf(" The largest number is = %f\n" , max);

}                                                                   /* End of main() */
   /* Function to determine the largest number */

float large(float x , float y)
{
if(x>y)
{
return(x);
}
else                                                               /* ( y>x )  */
{
return(y);
}
}

Output:
 12.5
 10.5
 a = 12.5 and b = 10.5
 The largest number is = 12.5

                                                                     That's All

Wednesday, 25 July 2012

WAP to Compute the Sum of two Number by using Function

Sum

Statement of C Progream: This Program accepts two Integers and Computes their Sum via addnums() :
There are two methods to write this Program
  1. NON ANSI style
  2. ANSI style
1. NON ANSI style:

#include<stdio.h>
#include<conio.h>
void main()
{
int a , b , result;
clrscr();
printf(" Enter the value of two numbers\n");
scanf("%d%d" , &a , &b);
result = addnums(a , b);
printf(" sum of %d and %d = %d\n" , a , b , result);

}                                                      /* End of main */

/* Function to add two numbers */

int addnums( var1 , var2)
int var1 , var2;
{
int sum;
sum = var1 + var2;
return(sum);
}                                               /* End of Function */

Output:
Enter the value of two numbers
24
68
sum of 24 and 68 = 92


2. ANSI style:

#include<stdio.h>
#include<conio.h>
void main()
{
int a , b , result;
clrscr();
printf(" Enter the value of two numbers\n");
scanf("%d%d" , &a , &b);

result = addnums(a , b);
printf(" sum of %d and %d = %d\n" , a , b , result);

}                                                      /* End of main */

/* Function to add two numbers */

int addnums( int var1 , int var2)
{
int sum;
sum = var1 + var2;
return(sum);

}                                               /* End of Function */

Output:
Enter the value of two numbers
14
18
sum of 14 and 18 = 32

                                                                 That's All
                                                                   

Thursday, 19 July 2012

WAP to Find the Average Male Height and Female Height in the Class

Average Height

Statement of C Program: This Program Prints the Average Height of Male and Female :

#include<stdio.h>
#include<conio.h>
void main()
{

int mh[3] , fh[3] , i ;           /* mh stands for Male Height and fh stands for Female Height */

float mtot , ftot ;          /* mtot stands for Male total height and ftot stands for Female total Height */

float mavg , favg ;       /* mavg stands for Males Average Height and favg stands for Females Average Height */

clrscr();
mavg = 0;
favg = 0;

for(i=0 ; i<3 ; i++)
{
printf(" Enter height of male %d :" , i+1);
scanf("%d" , &mh[i] );
mtot = mh[i] + mtot;
}
mavg = mtot/3;
printf("\n");

for(i=0 ; i<3 ; i++)
{
printf(" Enter height of female %d :" , i+1);
scanf("%d" , &fh[i] );
ftot = ftot + fh[i];
}
favg = ftot/3;
printf("\n");

printf(" Average of male height = %f\n" , mavg);
printf(" Average of female height = %f\n" , favg);
getch();
}                                                 /* End of main() */
 Output:
 Enter height of male 1 : 150
 Enter height of male 2 : 170
 Enter height of male 3 : 190

 Enter  height of female 1 : 160
 Enter  height of female 2 : 180
 Enter  height of female 3 : 200

 Average of male height = 170
 Average of female height = 180 

                                                                  
                                                                That's All

Wednesday, 18 July 2012

WAP to Find the Largest and Second Largest Number out of 10 Array Elements

Largest and Second Largest Number

Statement of C Program: This Program Prints the Largest and Second Largest Number out of 10 Array Elements:

#include<stdio.h>
#include<conio.h>
void main()
{
int A[10];
int i , j , b;
clrscr();

for(i=0 ; i<10 ; i++)
{
printf(" Enter the Number %d :" , i+1 );
scanf("%d" , &A[i] );
}

for(i=0 ; i<10 ; i++)
{
for(j=i ; j<10 ; j++)
{
if(A[i] < A[j])
{
b = A[j] ;
A[j] = A[i] ;
A[i] = b ;
}
}
}

printf(" Largest Number is : %d\n" , A[0] );
printf(" Second Largest Number is : %d\n" , A[1] );
getch();
}                                                     /* End of main() */

Output:
Enter the Number 1 : 90
Enter the Number 2 : 80
Enter the Number 3 : 70
Enter the Number 4 : 60
Enter the Number 5 : 50
Enter the Number 6 : 40
Enter the Number 7 : 30
Enter the Number 8 : 20
Enter the Number 9 : 10
Enter the Number 10 : 110

Largest Number is : 110
Second Largest Number is : 90

                                                                     That's All

Saturday, 7 July 2012

Write a C Program to Find the Sum of All the Positive and Negative Numbers in Array

Positive and Negative Number Sum

Statement of C Program: This Program reads N integers (+ve , -ve and zero) into an Array A and Finds
  1. The sum of positive numbers
  2. The sum of negative numbers
  3. The average of all input numbers.
#include<stdio.h>
#include<conio.h>
#define MAX 8
void main()
{
int Array[MAX] ;
int i , N ;

int negsum , posum;    
/* negsum stands for Sum of Negative Numbers and posum stands for Sum of Positive Numbers */

float total , average;
clrscr();
negsum = 0 ;
posum = 0;
total = 0;

printf(" Enter the value of N\n");
scanf("%d" , &N);

printf(" Enter %d numbers( -ve , +ve and 0 )\n" , N);
for(i=0 ; i<N ; i++)
{
scanf(" %d" , &Array[i] );
}

printf(" Array elements\n");
for(i=0 ; i<N ; i++)
{
printf("%d\n" , Array[i] );
}

for(i=0 ; i<N ; i++)
{
if(Array[i] < 0)
{
negsum + = Array[i];
}
else if(Array[i] > 0)
{
posum + = Array[i] ;
}
else if(Array[i] == 0)
{
       ;
}
total + = Array[i];
}
average = total / N;

printf(" Sum of all positive numbers =%d\n" , posum);
printf(" Sum of all negative numbers =%d\n" , negsum);
printf(" Average of all input numbers %f\n" , average);

}                                                  / * End of main() */

 Output:
 Enter the value of N
  5
 Enter 5 numbers (-ve , +ve and 0)
  6
 -7
  0
 -3
  5
 Array elements
  6
 -7
  0
 -3
  5
 Sum of all positive numbers = +11
 Sum of all negative numbers = -10
 Average of all input numbers = 0.20

                                                                   That's All

Friday, 6 July 2012

Write a C Program to Calculate the Trace and Normal of a Matrix

Trace and Normal of Matrix
Statement of C Program: This Program accepts a square matrix and prints the Trace and Normal of the Matrix.

Trace of Matrix



Trace: Trace of a Matrix can be defined as the sum of the main diagonal elements.






Normal of Matrix 



Normal: Normal can be defined as the square root of the sum of squares of all the elements of a Matrix.





#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , n ;                                       /* 'i' used for Rows and 'j' used for Columns */
int Row , Column;
int Matrix[9][9];
float trace , sum , normal;

clrscr();
trace = 0;
sum = 0;

printf(" Enter the order of Matrix\n");
scanf("%d * %d" , &Row , &Column);

printf(" Enter the elements of Matrix\n");
for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
scanf("%d" , &Matrix[i][j] );
}
}

                                                  /* Trace and Normal */

for(i=0 ; i<Row ; i++)
{
for(j=0 ; j<Column ; j++)
{
if(i==j)
{
trace + = Matrix[i][j];                   /* It can also be Written as  trace=trace+Matrix[i][j]  */
}
sum + = pow( Matrix[i][j] , 2 );       
}
}
normal = sqrt(sum);

printf(" Trace = %f\n" , trace);
printf(" Normal = %f\n" , normal);
getch();
}                                                               /* End of main() */
 

Thursday, 5 July 2012

Write a C Program to Print the Multiplication of Two Matrices

    Multiplication of Matrix
    Statement of C Program: This Program accepts two Matrices of different or same order and Find the product of these Matrices and prints the Product Matrix:

    Condition: The Column of First Matrix must be Equal to the Row of the Second Matrix.




    Matrix Multiplication Condition

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int Matrix A[9][9] , MatrixB[9][9] , Matrixsproduct [9][9] ;

    int n , i , j , k;                              /* 'i' used for rows and 'j' used for columns */

    int Row1 , Row2 , Column1 , Column2;
    clrscr();
    printf(" Enter the order of Matrix A\n");
    scanf("%d * %d " , &Row1 , &Column1);

    printf(" Enter the order of Matrix B\n");
    scanf("%d * %d " , &Row2 , &Column2);

    if(Column1 == Row2)
    {
    printf(" Enter the elements of Matrix A\n");
    for(i=0 ; i<Row1 ; i++)
    {
    for(j=0 ; j<Column1 ; j++)
    {
    scanf("%d" , &Matrix A[i][j] );
    }
    }

    printf(" Enter the elements of Matrix B\n");
    for(i=0 ; i<Row2 ; i++)
    {
    for(j=0 ; j<Column2 ; j++)
    {
    scanf("%d" , &Matrix B[i][j] );
    }
    }

                                                         /* Product of two Matrices */

    for(i=0 ; i<Row1 ; i++)
    {
    for(j=0 ; j<Column2 ; j++)
    {
    Matrixproduct[i][j] = 0 ;
    for(k=0 ; k<Row2 ; k++)
    {
    Matrixproduct[i][j] = Matrixproduct[i][j] + ( Matrix A[i][k] * Matrix B[k][j] );
    }
    }
    }

    printf(" Product Matrix\n");
    for(i=0 ; i< Row1 ; i++)
    {
    for(j=0 ;j< Column2;j++)
    {
    printf("%d" , Matrixproduct[i][j] );
    }
    printf("\n");
    }
    }                                                                    /* End of if */

    else
    {
    printf(" Invalid order so Multiplication not possible\n");
    }                                                                 /* End of main() */

     Output:
     Enter the order of Matrix A
     2 * 2
     Enter the order of MatrixB
     2 * 2
     Enter the elements of Matrix A
     1
     2
     3
     4
     Enter the elements of Matrix B 
     5
     6
     7
     8
     Product Matrix
     19      22
     43      50


                                                                  That's All

    Write a C Program to Print the Transpose of a Matrix

    Transpose of Matrix

    Transpose: Transpose of a Matrix means changing Rows into Columns and vice-versa.


    Statement of C Program: This Program accepts the Matrix and prints its Transpose.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int A[2][3] , B[3][2];
    int i, j;                                   /* 'i' used for rows and 'j' used for columns */
    clrscr();
    printf(" Enter the elements of A\n");
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<3 ; j++)
    {
    scanf("%d" , &A[i][j] );
    }
    }

    printf(" Matrix is\n");
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<3 ; j++)
    {
    printf("%d\t" , A[i][j] );                            /* '\t' used for Tab */ 
    }                                                          
    printf("\n");                                       /* '\n' used for next line character */
    }


    for(i=0 ; i<3 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    B[i][j] = A[j][i];
    }
    }

    printf(" After Transpose\n");
    for(i=0 ; i<3 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    printf("%d\t" , B[i][j] );                            
    }                                                          
    printf("\n");                                      
    }

    getch();
    }                                                       /* End of main */

     Output:
     Enter the elements of A
     4
     6
     3
     9
     1
     7
     Matrix is
     4      6
     3      9
     1      7
     After Transpose
     4      3       1
     6      9       7

                                                                        That's All

    Tuesday, 3 July 2012

    Write a C Program to Print the Sum of Two Matrices of Same Order

    Sum of Matrices

    Statement of C Program: This Program accepts two Matrices and Find the Sum of these Matrices and prints the Sum Matrix.

    Condition: Both the Matrices are of Same Order i.e Same Order Matrices can be Added or Substracted.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int Matrix1 [2][2] , Matrix2 [2][2] , Matrixsum[2][2] ;             
    int i, j;                                        /* 'i' used for rows and 'j' used for columns */
    clrscr();

    printf(" Enter the elements of Matrix1\n");
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    scanf("%d" , &Matrix1[i][j] );
    }
    }                        /* Elements of Matrix Read (Input) with the help of scanf() Function */


    printf(" Enter the elements of Matrix2\n");
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    scanf("%d" , &Matrix2[i][j] );
    }
    }

                                                                       / * Sum of two Matrices  */
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    Matrixsum[i][j] = Matrix1[i][j] + Matrix2[i][j] ;
    }
    }

    printf(" Sum Matrix \n");
    for(i=0 ; i<2 ; i++)
    {
    for(j=0 ; j<2 ; j++)
    {
    printf("%d" , Matrixsum[i][j] );
    }
    printf("\n");
    }

    getch();
    }                                                      /* End of main */

    Output:
    Enter the elements of Matrix1
    3
    8
    4
    6
    Enter the elements of Matrix2 
    4
    0
    1
    -9
    Sum Matrix
    7     5
    8    -3

                                                                       That's All