Friday, 24 September 2021

Program to check whether the reverse string is a palindrome

 

#include<stdio.h>
#include<string.h>

int main()
{
	//declare variables
	char str1[30];
	int i,len,flag=0;
	
	printf("Enter a string:");
	scanf("%s",str1);
	
	len=strlen(str1);//get the string length
	
	for(i=0;i<len;i++)
	{
		//str1[i] is not equal to str1[len-i-1]
		if(str1[i]!=str1[len-i-1])
		{
			flag =1;
			break;//exit from if statement
			
		}
	}
	
	if(flag)
	{
		printf("%s is not a palindrome string ",str1);
		
	}
	else
	{
		printf("%s is a palindrome",str1);
	}
	return 0;
	
	
}










YouTube Video

Monday, 13 September 2021

print all ASCII value in on Program ..

 

#include<stdio.h>
int main()
{
	int asciTable;
	
	printf("The complete ASCII table of the characters in the C");
	
	for(asciTable=0;asciTable<255;asciTable++)
	{
		printf("\n The value of '%c' charactor is:%d",asciTable,asciTable);
	}
	
	return 0;
}


YouTube Video

How to print ASCII code the special characters use for loop.

 #include<stdio.h>

int main()
{
	int specialCh;
	
	for(specialCh=33;specialCh<48;specialCh++)
	{
		printf("\n The ASCII value of '%c' special character is:%d",specialCh,specialCh);
	}
	
	for(specialCh=58;specialCh<65;specialCh++)
	{
		printf("\n The ASCII value of '%c' special character is:%d",specialCh,specialCh);
	}
	
	for(specialCh=123;specialCh<127;specialCh++)
	{
		
		printf("\n The ASCII value of '%c' special character is:%d",specialCh,specialCh);
	}
	
	return 0;
}


YouTube Video

How to print ASCII code for given charactor by the user.

 

 #include<stdio.h>
 int main()
 {
 	char arr[30];//declare the size of character array
 	int count=0;//declare a count variable
 	
 	//enter any name to get the ascii codes
 	
 	printf("\nEnter the name to get the ASCII codes:");
 	scanf("%s",&arr);
 	
 	//use while loop to sequentially iterate every character of the array
 	
 	while(arr[count]!='\0')
 	{
 		//display the character name one by one 
 		
 		printf("\n The ascii code of the character %c is %d",arr[count],arr[count]);
 		
 		count++;//increment one by one
 		
	 }
	 return 0;
	 
 }









YouTube Video


How to print ASCII code the small latter form a to z use for loop.


#include<stdio.h> int main(){ int small; for(small =97;small<123;small++) { printf("\n The ASCII value of %c is %d",small, small); } return 0; 
    } 










YouTube Video


How to print ASCII code the capital latter form A to Z use for loop.

#include<stdio.h>

int main()
{
	int caps;
	//use for loop to print the capital latter form A to Z
	
	for(caps = 65;caps<91;caps++)
	{
		printf("\nThe ASCII value of %c is %d",caps,caps);
		
	}
	return 0;
}











YouTub Video

 

Wednesday, 8 September 2021

What is Calloc in C.

 

Calloc in C

This topic will discuss how to create dynamic memory allocation using the calloc() function in the C programming language. Before going through the concepts, let's discuss the dynamic memory allocation in C. Dynamic memory is a structure programming procedure that allows users to allocate the memory at the run time of a program. Using dynamic memory allocation, we can increase or decrease the memory during the execution of a program. In this way, it avoids the wastage of computer memory. A memory allocation is divided into two parts are malloc() and calloc() function.

Calloc in C

calloc() function is a predefined library function that stands for contiguous memory allocation. A calloc() function is used to create multiple blocks at the run time of a program having the same size in the memory. A calloc function is defined inside the stdlib.h header file. It has two parameters, no. of blocks and the size of each block. When the dynamic memory is allocated using the calloc() function, it returns the base address of the first block, and each block is initialized with 0. And if memory is not created, it returns a NULL pointer.

For example, suppose we want to create three blocks of memory using the calloc() function, we need to pass two parameters, a number of blocks (3) and the size of each block (int, char, float, etc.) in the byte. In this way, it creates three blocks whose size is the same inside the computer memory.

Syntax:-

ptr = (cast_type *) calloc ( number_of_blocks, size_of_block);  

In the above syntax, the calloc() function has two parameters. The first parameter defines the number of blocks and the second parameter defines the size of each block in memory. The size of the blocks and cast_type can be in int, char, float, etc.

Return: It returns the base address of the first block to the ptr variable.

Program to check dynamic memory is allocated using calloc() function

Let's write a simple program to check whether the dynamic memory is allocated in C.

program.c

#include <stdio.h>  
#include <stdlib.h>  
int main()  
{  
int *ptr;  
/* use calloc() function to define the no. of blocks and size of each blocks. */  
ptr = calloc (4, sizeof(int)); // here 4 is the no. of block and int is the size of block  
if (ptr != NULL)  
{  
printf (" Memory is created successfully \n");  
}  
else   
printf (" Memory is not created ");  
return 0;  
}  

Output:

Memory is created successfully

Program to demonstrate the use of the calloc() function

Let's consider creating dynamic memory allocation using calloc() function and storing data into the memory blocks.

Program2.c

#include <stdio.h>  
#include <conio.h>  
#include <stdlib.h>  
      
void main()  
{     
    int n, *ptr, *p, i, sum = 0;  
    /* n = number of elements, *ptr = store base address of the dynamic memory,  
    *p store temporary address of the *ptr */   
    printf (" Enter the number of elements: ");  
    scanf (" %d", &n); // it takes number of elements   
      
    // use calloc syntax to create memory block of int data type   
    ptr = (int *) calloc (n, sizeof(int));  
    p = ptr; // assign the address of ptr  
    if (ptr == NULL) // it checks whether the memory is allocated   
    {  
        printf (" Memory is not allocated. ");  
        exit(0); // exit from the program  
    }  
    printf (" Enter %d numbers \n", n);  
    for ( i = 1; i <= n; i++)  
    {  
        scanf ( "%d", ptr);  
        sum = sum + *ptr;  
        ptr++;  
    }  
      
    printf (" Elements are: ");  
    for (i = 1;  i <= n; i++)  
    {  
        printf (" %d", *p);  
    p++;  
    }  
    printf (" \n The addition of the elements is: %d ", sum);  
    getch();  
}  

Output:

Enter the number of elements: 5
 Enter 5 numbers
1
2
3
4
5
 Elements are:  1 2 3 4 5
 The addition of the elements is: 15

Program to release dynamic memory allocation using free() function

free() function: A free() function is used to release the dynamic memory which is created either calloc() or malloc() function. These allocated memories cannot be freed to their own, and they will exist till the end of the program. So, it is our responsibility to release that memory that can be reused, and hence we explicitly use the free() function to release the memory.

Syntax:-

free (ptr);  

Here free() is a function that releases the allocated memory using the pointer ptr variable.

Let's consider creating dynamic memory allocation using the calloc() function and then releasing occupied space using the free() function in the C program.

Release.c

#include <stdio.h>  
#include <conio.h>  
#include <stdlib.h>  
      
void main()  
{     
    int n, *ptr, *p, i, sum = 0;  
    printf (" Define the number of elements to be entered: ");  
    scanf (" %d", &n);   
      
    // use calloc syntax to create memory block of int data type   
    ptr = (int *) calloc (n, sizeof(int));  
    p = ptr; // store the base address in p  
    if (ptr == NULL)  
    {  
        printf (" Out of memory ");  
        exit(0);  
    }  
    printf (" Enter the elements \n", n);  
    for ( i = 1; i <= n; i++)  
    {  
        scanf ( "%d", ptr);  
        sum = sum + *ptr;  
        ptr++;  
    }  
      
    printf (" Elements are: ");  
    for (i = 1;  i <= n; i++)  
    {  
        printf (" %d", *p);  
    p++;  
    }  
    printf (" \n The addition of the elements is: %d ", sum);  
    free(ptr); /* Use free() function to release the dynamic memory allocation */   
    getch();  
}  

Output:

Define the number of elements to be entered: 6
 Enter the elements
2
4
6
8
10
12
 Elements are:  2 4 6 8 10 12
 The addition of the elements is: 42

Write calculator program in C using function and switch statement.

 

Example 4: Calculator Program in C using function and switch statement

Let's create a Calculator program using function and switch case statement in C

 

#include <stdio.h>  
#include <conio.h>  
#include <math.h>  
#include <stdlib.h>  
      
// function declarations   
int addition();  
int subtract();  
int multiply();  
int divide();  
int sq();  
int sqrt1();  
void exit();  
  
int main()  
{  
    // declaration a local variable op;  
    int op;  
    do  
    {  
        // displays the multiple operations of the C Calculator  
        printf (" Select an operation to perform the calculation in C Calculator: ");  
        printf (" \n 1 Addition  \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n");
         printf(" 5 Square \t \t 6 Square Root \n 7 Exit \n \n Please, Make a choice ");      
          
        scanf ("%d", &op); // accepts a numeric input to choose the operation  
      
      
    // use switch statement to call an operation  
    switch (op)  
    {  
        case 1:  
            addition(); /* It call the addition() function  to add the given numbers */  
            break; // break the function  
              
        case 2:  
            subtract(); /* It call the subtract() function  to subtract the
             given numbers */  
            break; // break the function  
              
        case 3:  
            multiply(); /* It call the multiply() function  to multiply the given
                         numbers */  
            break; // break the function  
              
        case 4:  
            divide(); // It call the divide() function  to divide the given numbers  
            break; // break the function  
              
        case 5:  
            sq(); // It call the sq() function  to get the square of given numbers  
            break; // break the function  
              
        case 6:  
            sqrt1(); /* It call the sqrt1() function  to get the square root of
 given numbers */  
            break; // break the function  
              
        case 7:  
            exit(0); // It call the exit() function  to exit from the program  
            break; // break the function  
              
        default:  
            printf(" Something is wrong!! ");  
            break;                        
    }  
    printf (" \n \n ********************************************** \n ");  
    } while (op != 7);  
      
  
    return 0;        
}  
  
  
  
// function definition  
int addition()  
{  
    int i, sum = 0, num, f_num; // declare a local variable   
    printf (" How many numbers you want to add: ");  
    scanf ("%d", &num);  
    printf (" Enter the numbers: \n ");  
    for (i = 1; i <= num; i++)  
    {  
        scanf(" %d", &f_num);  
        sum = sum + f_num;  
    }  
    printf (" Total Sum of the numbers = %d", sum);  
    return 0;  
}  
  
// use subtract() function to subtract two numbers  
int subtract()  
{  
    int n1, n2, res;  
    printf (" The first number is: ");  
    scanf ("  %d", &n1);  
    printf (" The second number is: ");  
    scanf ("  %d", &n2);  
    res = n1 - n2;    
    printf (" The subtraction of %d - %d is: %d", n1, n2, res);  
}  
  
// use multiply() function to multiply two numbers  
int multiply()  
{  
    int n1, n2, res;  
    printf (" The first number is: ");  
    scanf ("  %d", &n1);  
    printf (" The second number is: ");  
    scanf ("  %d", &n2);  
    res = n1 * n2;    
    printf (" The multiply of %d * %d is: %d", n1, n2, res);  
}  
  
// use divide() function to divide two numbers  
int divide()  
{  
    int n1, n2, res;  
    printf (" The first number is: ");  
    scanf ("  %d", &n1);  
    printf (" The second number is: ");  
    scanf ("  %d", &n2);  
      
    if (n2 == 0)  
    {  
        printf (" \n Divisor cannot be zero. Please enter another value ");  
        scanf ("%d", &n2);        
    }  
    res = n1 / n2;    
    printf (" \n The division of %d / %d is: %d", n1, n2, res);  
}  
  
// use sq() function to get the square of the given number  
int sq()  
{  
    int n1, res;  
    printf (" Enter a number to get the Square: ");  
    scanf ("  %d", &n1);  
      
    res = n1 * n1;    
    printf (" \n The Square of %d is: %d", n1, res);  
}  
  
// use sqrt1() function to get the square root of the given number   
int sqrt1()  
{  
    float res;  
    int n1;  
    printf (" Enter a number to get the Square Root: ");  
    scanf ("  %d", &n1);  
  
    res = sqrt(n1);   
    printf (" \n The Square Root of %d is: %f", n1, res);  
}  




Program to check whether the reverse string is a palindrome

  # include < stdio.h > # include < string.h > int main ( ) { //declare variables char str1 [ 30 ] ; int i , len...