Saturday, April 9, 2022

C program to create an array of student names

This program is based on C programming String and an Array concept.


Problem definition: Write a C program to create an array of student names.

//C Program to create an array of student names

#include<stdio.h>

int main(){
	char name[5][20]; 
	//This can store 5 names; maximum 20 characters in each name
	
	int i;
	//Logic to Read name of 5 students
	for(i=0; i<5; i++){
		printf("Enter name %d:", i+1);
		gets(name[i]);
	}
	printf("List of student names stored: \n");
	//Logic to print name of 5 students
	for(i=0; i<5; i++){
		printf("%s \n", name[i]);
	}
	return 0;
}

Output of the program

Enter name 1:Amit
Enter name 2:Sunit
Enter name 3:Kamlesh
Enter name 4:Bhargav
Enter name 5:Alpesh

List of student names stored:
Amit
Sunit
Kamlesh
Bhargav
Alpesh

* * *





Sunday, January 23, 2022

Check whether a character is an alphabet, digit or special character

This program is based on C Programming Decision making concept (use of if..else statement).

Problem definition: Write a C program to check whether a given character is an alphabet, digit or special character.

#include <stdio.h>
int main()  
{  
    char chr;  

    printf("Enter a character: ");  
    scanf("%c", &chr);  
  
    /* logic to check whether it is an alphabet */  
    if((chr >= 'a' && chr<='z') || (chr>='A' && chr<='Z'))  
    {  
        printf("It is an alphabet.\n");
    }  
    else if(chr>='0' && chr<='9') /* to check digit */  
    {  
        printf("It is a digit.\n");  
    }  
    else /* else special character */
    {  
        printf("It is a special character.\n");  
    }  
}

Sample output 1

Enter a character: A
It is an alphabet.

Sample output 2

Enter a character: 5
It is a digit.

Sample output 3
Enter a character: @
It is a special character.




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.