Friday, December 31, 2021

Use of #define in C program

Use of #define 

In the C Programming Language, the #define directive allows the definition of macros within your source code.

Macros are a portion of the code which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code.

In the following example, we have used

#define DISPLAY printf

During execution all the DISPLAY word will be replaced with printf first.

Example of #define in C program


#include<stdio.h>
#include<Windows.h>
#define DISPLAY printf

int main()
{
    int i;
    for(i=0; i<3; i++)
    {
      DISPLAY("..... Good Bye 2021 .....");
      Sleep(500);
      system("cls");
      Sleep(500);
    }
    
    for(i=0; i<3; i++)
    {
      DISPLAY("..... WEL COME 2022 .....");
      Sleep(500);
      system("cls");
      Sleep(500);
    }
    return 0;
}

Output of the program

This will blinks three times

 ..... Good Bye 2021 ..... 

and then blinks three times 

..... WEL COME 2022 ..... 



Sunday, August 1, 2021

C Program to perform Matrix Subtraction

Matrix Subtraction

We will write a C program to perform matrix subtraction operation.

Key points for subtraction of matrices

  • Two matrices should be of same order (number of rows=number of columns).
  • Subtract the corresponding element of matrices.

Example: Subtraction of matrix of order 3 * 3

Subtraction of Matrix (A-B) is as under:

A - B = C


C Program to perform Matrix Subtraction

Program Code:

#include<stdio.h>
int main()
{
 int A[3][3],B[3][3],C[3][3],i, j;
 //Loops for Reading A matrix
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf("Matrix A[%d][%d]:",i,j);
     scanf("%d", &A[i][j]) ;
   }   
 }
 //Loops for Reading B matrix
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf("Matrix B[%d][%d]:",i,j);
     scanf("%d", &B[i][j]) ;
   }   
 }
 printf("Matrix A is as under: \n") ;
 /* Print Matrix A, B */
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",A[i][j] );
   }
   printf("\n");
 }
 printf("Matrix B is as under: \n") ;
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",B[i][j] );
   }
   printf("\n");
 }
 printf("Matrix Subtraction is as under: \n");
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",A[i][j] - B[i][j] );
   }
   printf("\n");
 }
 return 0;
}

Program Output:

Matrix A[0][0]:4
Matrix A[0][1]:5
Matrix A[0][2]:6
Matrix A[1][0]:7
Matrix A[1][1]:8
Matrix A[1][2]:9
Matrix A[2][0]:10
Matrix A[2][1]:11
Matrix A[2][2]:12
Matrix B[0][0]:1
Matrix B[0][1]:3
Matrix B[0][2]:2
Matrix B[1][0]:5
Matrix B[1][1]:4
Matrix B[1][2]:3
Matrix B[2][0]:2
Matrix B[2][1]:3
Matrix B[2][2]:6
Matrix A is as under:
 4        5       6
 7        8       9
 10       11      12
Matrix B is as under:
 1        3       2
 5        4       3
 2        3       6
Matrix Subtraction is as under:
 3        2       4
 2        4       6
 8        8       6

* * * * *

* * * * *








Saturday, May 8, 2021

Flowchart to input 5 numbers from user and to print odd or even number.

Flowchart to read 5 numbers from user and to print odd or even number.

[ This article is posted for a request received from the visitor of the blog.]

Following flowchart will input 5 numbers one by one from the user and prints whether it is Odd or Even number.

Flowchart to input 5 numbers from user and to print odd or even number.
Fig. Flowchart to input 5 numbers and to print Odd or Even

Output of the flowchart:

1 is odd number
2 is even number
3 is odd number
4 is even number
5 is odd number
----Run complete.  35 symbols evaluated.----






Monday, February 15, 2021

C program to find square of given number without an argument and a return value

C Programming => Functions

C program to find square of given number using function concept. This program demonstrate use of function without an argument and a return value.

Program code

#include<stdio.h>
int square(); //function prototype declaration.
int number; //use of global variable

int main()
{
  int answer;
  answer = square(); //Call function.
  printf("Square of %d is %d.", number, answer);
  return 0;
}

//User defined function to find square()
int square()
{
  printf("Enter your number:");
  scanf("%d", &number);
  //return answer to main function
  return(number*number);
}

Output of the program

Enter your number:5
Square of 5 is 25.





Monday, January 18, 2021

Flowchart to check Odd or Even Number between 1 to 10

Flowchart to check odd or even number between 1 to 10 numbers

Flowchart to check Odd or Even Number between 1 to 10



Output of the flowchart:

1 is odd

2 is even

3 is odd

4 is even

5 is odd

6 is even

7 is odd

8 is even

9 is odd

10 is even

----Run complete.  55 symbols evaluated.----


* * * * *



This flowchart is generated using Raptor tool. Read more about RAPTOR here


Top 10 Flowcharts Examples

You may like to visit following top 10 flowchart examples.