C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
5. Program to find the sum of "n" elements
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Calculating sum
   for(i=0;i<n;i++)
    {
     sum = sum + *(ptr+i);
    }  
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
    }
 
    // Displaying sum
    printf("\nSum of elements : %d",sum);

    // Deallocating memory
    free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

6. Program to sort "n" elements in ascending order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i,j, temp;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
       printf("%d ",*(ptr+i));
    }
 
    // Sorting elements
   for(i=0;i<n;i++)
        {
     for(j=i+1;j<n;j++)
     {
      if( *(ptr+i) > *(ptr+j) )
      {
       temp=*(ptr+i);
       *(ptr+i)=*(ptr+j);
       *(ptr+j)=temp;
      }
     }
  }  
    
 
 // Displaying sorted elements
 printf("\nElements sorted in ascending order : ");
 for(i=0;i<n;i++)
        {
        printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

7. Program to search an element
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    //Declaring variable es=element to be searched, flag=indicates if element found or not
    int es, flag=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
    // Inputing element to be searched
    printf("\nEnter element to be searched : ");
    scanf("%d",&es);
    
    // Searching element
   for(i=0;i<n;i++)
    {
     if( *(ptr+i)==es)
     {
      flag=1;
      break;
     }
 }  
 
 // Displaying element searched or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

8. Program to enter three strings and sort them in ascending order alphabetically
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
#include<string.h>
int main()
{
 int i,j;
 
 // Declaring variable temp=tempeorary helps in swapping strings
 char temp[50];
 
 // Here 3 = number of string which we can enter
 char *p1[3];

 // Inputing string
 printf("Enter three strings : ");
 for(i=0;i<3;i++)
 {
  p1[i]=(char *)malloc(12*sizeof(char));
  gets(p1[i]);
 }
 
 // Performing sorting
 for(i=0;i<3;i++)
 {
  for(j=i+1;j<3;j++)
  {
   // swapping strings
   if(strcmp(p1[i],p1[j])>0)
   {
    strcpy(temp,p1[i]);
    strcpy(p1[i],p1[j]);
    strcpy(p1[j],temp);
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 for(i=0;i<3;i++)
 puts(p1[i]);

 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
1. Program to dynamically allocate memory using calloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)calloc(n,sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

2. Program to dynamically allocate memory using malloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

3. Program to dynamically allocate memory using malloc for "n" integer elements, input elements and then display it. Then reallocate memory for the same pointer variable, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 printf("\nEnter number of elements : ");
    scanf("%d",&n);
    
    // Reallocating memory 
 ptr=realloc(ptr,n);
 
 // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
    
    getch(); // Linux user - Remove this

    return 0;
}

4. Program to find the length of the string
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
void main()
{
 char *ptr;
 
 // Declaring variable i=to iterate loop, l=lenght of string
 int i, l=0;
 
    // Allocating memory
 ptr=(char *)malloc(12*sizeof(char));
 
 // Inputing string
 printf("Enter string : ");
 gets(ptr);
 
 // Displaying string
 printf("Entered string : ");
 puts(ptr);
 
 // Calculating length
 while(ptr[l]!='\0')
 {
 l++;
 }
 
 // Printing length of string
 printf("Lenght of string : %d",l);
 
 // Deallocating memory
 free(ptr);
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
6. Program to find maximum element of an array of elements using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10, i=to iterate loop, max=maximum element
 int a[10], i, max;
 
 // Declaring pointer variable pointing to the the base address i.e ( a[0] )of array 'a'  
 int *ptr=a; // It is equivalent to : int *ptr; ptr=&a;
 
 // Inputing array elements
 printf("Enter 10 elements in an array : ");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Assigning first element to max, ptr points to second element after post increment
 max=*ptr++;
 
 // Determining maximum value
 for(i=0;i<10;i++)
 {
  if(*ptr > max)
  {
   max=*ptr;
   ptr++;
  }
 }
 
 // Printing maximum value
 printf("\nMaximum value : %d",max);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to print sum of all the elements in 2D array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring 2D array and initializing it
 int a[3][4]={{3,4,5,6},{5,6,7,8},{8,9,10,11}};
 
 // Declaring varaible sum
 int sum=0;
 
 // Declaring pointer variable
 int *ptr, *ptre;
 
 ptr=&a[0][0]; // points to address of first element
 ptre=ptr+12; 
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++;
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to sort an array of elements using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it
 int a[]={5,6,3,4,5};

 // Declaring pointer variable *p, *q and temp=tempeorary that helps in swapping numbers
 int *p,*q,temp;

 // Declaring variable i, j to iterate loop
 int i,j;

 //Displaying array elements
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 printf("\n");
 
 // Pointing to the address of first element
 p=&a[0];
 
 // Performing sorting
 for(i=0;i<5;i++)
 {
  for(j=i+1;j<5;j++)
  {
   // Swapping elements
   if(*(p+i) > *(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }

 // Displaying sorted elements
 printf("Sorted elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

9. Program to sort string using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str
 char str[50], *p;
 
 // Declaring variable i, j to iterate loop
 int i, j;
 
 // Declaring variable l=length, temp=tempeorary that helps in swapping
 int l=0, temp;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 p=&str[0];
 
 // finding length
 while(str[l]!='\0')
 l++;
 
 // Performing sorting
 for(i=0;i<l;i++)
 {
  for(j=i+1;j<l;j++)
  {
   // Swapping characters
   if(*(p+i)>*(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 p=&str[0];
 
 for(i=0;i<l;i++)
 {
  printf("%c\n",*(p+i));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

10. Program to search given element in an array using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it, *p=points to the address of first element
 int a[]={5,6,3,4,5}, *p;
 p=&a[0];
 
 // Declaring variable i = to iterate loop
 int i;
 
 /* Declaring variable es = element to be searched, flag = to indicate 
 if element is present or not */
 int es, flag=0;
 
 // Displaying array element
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",*(p+i));
 }
 
 // Inputing element to be searched
 printf("\nEnter element to be searched : ");
 scanf("%d",&es);
 
 // Searching element
 for(i=0;i<5;i++)
 {
  if(*(p+i)==es)
  {
   flag=1;
   break;
  }
 }
 
 // Displaying whether element found or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program to demonstrate the use of pointer to pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int x,*p,**q;
             
    printf("\nEnter a number : ");
    scanf("%d",&x);
    
    p=&x;
    q=&p;
    
    printf("\nValue of x=%d",x);
    printf("\nValue of x through pointer=%d",*p);
    printf("\nVal. of x through pointer to  pointer=%d",**q);
    printf("\nValue of p=%u",p);
    printf("\nValue of q=%u",q);
    printf("\nAddres of p=%u",q);
     
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : POINTERS

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
1. Program to accept two numbers and print its address along with the numbers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Printing address along with number
 printf("First number : %d, Address : %d\n",a, &a);
 printf("First number : %d, Address : %d\n",*(&a), &a);
 
 printf("Second number : %d, Address : %d\n",b, &b);
 printf("Second number : %d, Address : %d",*(&b), &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to accept two numbers and print the sum of given two numbers by using pointers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Declaring variale s=sum
 int s=0;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 // Inputing second number
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Determining sum
 s=*(&a)+*(&b);
 
 // Printing sum
 printf("Sum = %d",s);
 
 getch( ); // Linux user - Remove this

 return 0;
}

3. Program to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values, swap to interchange value
 int a, b, swap;
 
 // Declaring pointer that holds address of two values
 int *c, *d;
 
 // Storing address
 c=&a;
 d=&b;
 
 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Interchanging value
 swap=*c;
 *c=*d;
 *d=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",a, b);
  
 getch(); // Linux user - Remove this

 return 0;
}

4. Implement a function interchange() to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values
 int a,b;

 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Calling function interchange
 interchange(&a, &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

void interchange(int *x, int *y)

{
 int swap;
 
 // Interchanging value
 swap=*x;
 *x=*y;
 *y=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",*x, *y);
}


5. Program to sum all the elements of an array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10 and initializing it
 int a[10]={21, -2, 4, 6, 9, 12, 43, 22, 4, 8}, sum=0;
 
 // Declaring pointer type variable pointing to the the base address i.e ( a[0] ) of array 'a'  
 int *ptr=a; // It is equivalent to : int *ptr; ptr=&a;
 
 // Declaring variable ptre = stores address of 11th element
 int *ptre;
 ptre = ptr+10;
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++; // sum=sum + *ptr++
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d ",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}


C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
21. Program to accept a string and print each word of the string separately also print total number of words
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, word=to store the word
 char str[50], word[50];
 
 /* Declaring variable cw=count words, i=to iterate loop, 
 j=works as index position of word */
 int cw=0, i, j=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 /* We are concatinating a space here assuming user does not 
 give space at the last */
 strcat(str," ");

 // Printing string
 printf("Entered string : %s",str);
 
 //Determing total number of words and displaying it
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]!=' ')
  {
   word[j++]=str[i];
  }
  else
  {
   word[j]='\0';
   printf("\nWord : %s",word);
   cw++;
   j=0;
  }
 }
 
 // Printing total words
 printf("\nTotal number of words : %d",cw);
 
 getch(); // Linux user - Remove this

 return 0;
}

22. Program to accept a string and display vowels frequency( total number of vowels)
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 /* Declaring variable cv=count vowel, i=to iterate loop 
 ( i works as index position) */
 int cv=0, i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Checking if vowels and counting them
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || 
  str[i]=='i' || str[i]=='I' || str[i]=='o' || str[i]=='O' || 
  str[i]=='u' ||str[i]=='U')
  {
   cv++;
  }
 }
 
 // Printing total number of vowels
 printf("Entered String : %s",str);
 printf("\nTotal number of vowels : %d",cv);
 
 getch(); // Linux user - Remove this

 return 0;
}

23. Program to accept a string and display frequency of each vowel along with vowel
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable ca=count vowel a
 int ca=0, ce=0, ci=0, co=0, cu=0;
 
 // Declaring variable i=to iterate loop ( i works as index position)
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining frequency of each vowel
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]=='a' || str[i]=='A')
  ca++;
  
  if(str[i]=='e' || str[i]=='E')
  ce++;
  
  if(str[i]=='i' || str[i]=='I')
  ci++;
  
  if(str[i]=='o' || str[i]=='O')
  co++;
  
  if(str[i]=='u' || str[i]=='U')
  cu++;
  
 }
 
 // Printing string
 printf("Entered String : %s",str);
 
 // Printing frequency of string
 printf("\nFrequency of 'a' or 'A' : %d",ca);
 printf("\nFrequency of 'e' or 'E' : %d",ce);
 printf("\nFrequency of 'i' or 'I : %d",ci);
 printf("\nFrequency of 'o' or 'O' : %d",co);
 printf("\nFrequency of 'u' or 'U' : %d",cu);
 
 getch(); // Linux user - Remove this

 return 0;
}

24. Program to enter a word and check whether it is palindrome or not using string function
// palindrom - When word = reverse of word then it is called palindrome
// Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char word[50], word1[50];
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // Copying word to word1
 strcpy(word1,word);
 
 // checking if palindrome or not
 if(strcmp(word,strrev(word1))==0)
 {
  printf("Entered word is a palindrome ");
 }
 
 else
 printf("Entered word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}


25. Program to enter a word and check whether it is palindrome or not without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char word[50], revword[50];
 
 /* Declaring variable i=to iterate loop, l=length, 
 c=count the number of matched character */
 int i, j, l=0, c;
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // finding length
 while(word[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revword[j]=word[i];
  j++;
 }
 revword[j]='\0';
 
 //checking if palindrome or not
 c=0;
 for(i=0;word[i]!='\0';i++)
 {
  if(revword[i]==word[i])
  c++;
 }
 
 if(c==l)
 {
  printf("Word is a palindrome ");
 }
 
 else
 printf("Word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
16. Program to convert all characters in a string to lower case using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strlwr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

17. Program to convert all characters in a string to lower case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=65 && ch<97)
        {
            ch+=32; // ch1=ch1+32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}
18. Program to convert all characters in a string to upper case using string function

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strupr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

19. Program to convert all characters in a string to upper case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=97 && ch<=122)
        {
            ch-=32; // ch1=ch1-32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

20. Program to enter 5 string and print them with their length
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 /* Declaring variable str[5][50]- This means that 5 string is to be input 
 with max size of each string = 50 */
 char str[5][50];
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 for(i=0;i<5;i++)
 {
  printf("Enter %d string : ", i+1);
  gets(str[i]);
 }
 
 // Printing string with its length
 for(i=0;i<5;i++)
 {
  printf("\nString %d : %s ",i+1, str[i]);
  printf("\nString length : %d",strlen(str[i]));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
10. Program for Compare two String using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Comparing string
 if(strcmp(str1,str2)==0)
 {
  printf("Same string or string are equal");
 }
 
 else if(strcmp(str1,str2)<0)
 {
  printf("First string is smaller ");
 }
 
 else if(strcmp(str1,str2)>0)
 {
  printf("Second string is smaller ");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program for Compare two String without using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
  // Declaring variable str1=string1, str2=string2
  char str1[50],str2[50];
  
  // Declaring variable ch1=character of string1, ch2=character of string2
  char ch1,ch2;
  
  // Declaring variable i=to iterate loop, flag = to indicate comparision
     int i,flag=0;
               
        // Inputing string       
        printf("\nEnter First String : " );
        gets(str1);
        
        printf("\nEnter Second String : " );
     gets(str2);
     
     // Comparing string
  for(i=0;str1[i]!='\0';i++)
        {
            ch1=str1[i];
            ch2=str2[i];
            if(ch1>=97 && ch1<=122)
            {
                ch1-=32; // ch1=ch1-32
            }
            if(ch2>=97 && ch2<=122)
            {
                ch2-=32; // ch2=ch2-32
            }
            if(ch1!=ch2)
            {
                flag=1;
                break;
            }
        }
               if(flag==0)
                   {
                        printf("\nString  '%s' and  '%s' are equal ",str1,str2);
                   }
               else
                   {
                        printf("\nString  '%s' and  '%s' are Not equal ",str1,str2);
                   }

 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program for String Reverse with String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",strrev(str));
 
 getch(); // Linux user - Remove this

 return 0;
}

13. Program for String Reverse without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string, revstr[50]=reverse string
 char str[50], revstr[50];
 
 // Declaring variable i=to iterate loop, l=length
 int i, j,l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // finding length
 while(str[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revstr[j]=str[i];
  j++;
 }
 revstr[j]='\0';
 
 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",revstr);
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program for String Concatenation with String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Concatinating string
 strcat(str1,str2); // str2 is added to str1
 
 // Printing conactinated string
 printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}

15. Program for String Concatenation without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[100], str2[50];
 
 // Declaring variable l=length of string1
 int l1=0;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Finding length of string1 and string2
 while(str1[l1]!='\0')
 l1++;
 
 // Concatinating string
 for(i=0;i<str2[i]!='\0';i++)
 {
  str1[l1++]=str2[i];
 }
 str1[l1]='\0';

 // Printing Concatinated stirng
  printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
6. Program to find length of a string using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining string length
 l=strlen(str);
 
 // Printing string length
 printf("String : %s\nString length : %d",str,l);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to find length of a string without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining string length
 while(str[l]!='\0')
 {
  l++;
 }
 
 // Printing string length
 printf("String : %s\nString length : %d",str,l);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program for Copy a String to another using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string , cs=copied string
 char str[50], cs[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Copy string
 strcpy(cs,str);
 
 // Printing string
 printf("String : %s\n",str);
 printf("Copied string : %s",cs);
 
 getch(); // Linux user - Remove this

 return 0;
}

9. Program for Copy a String to another without using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string , cs=copied string
 char str[50], cs[50];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 while(str[l]!='\0')
 {
  cs[l]=str[l];
  l++;
 }
 cs[l]='\0';
 
 // Printing string
 printf("String : %s\n",str);
 printf("Copied string : %s",cs);
 
 getch(); // Linux user - Remove this

 return 0;
}


C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
1. Program to accept a string and print it
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=to store string inputted by user
 char str[80];
 
 // Inputing string
 printf("Enter any sentence : ");
 gets(str);
 
 // printing string
 printf("\nEntered String : ");
 puts(str);
 
 // Another way of printing string
 printf("\nEnterd string : %s",str);
  
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to accept a character in the uppercase and print in lower case
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 //Declaring variable ch to hold character value and cl=change to lower case 
 char ch,cl;

 // Inputting character
 printf("Enter a character in uppercase : ");
 ch=getchar();
 
 // Converting it to lower case
 cl=ch+32;
 
 // Printing lower case character
 printf("The given character in lowercase is : %c \n",cl);
 
 // Another way of priting
 printf("The given character in lowercase is : ");
 putchar(cl);
 
 getch(); // Linux user - Remove this

 return 0;
}

3. Program to accept a character in Lower case and print it in upper case
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable ch=character, cu=change character to upper case
 char ch,cu; 
 
 // Entering character
 printf("Enter a charcater in lower case: ");
 scanf("%c",&ch);
 
 // Converting it to upper case
 cu=ch-32;
 
 // Printing upper case character
 printf("The given character in upper case is : %c \n",cu);
 
  getch( ); // Linux user - Remove this

  return 0;
}


4. Program to accept a character in any case and print in another case
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable ch to hold character value and cch=change character
 char ch,cch;

 // Inputting character
 printf("Enter a character in anycase : ");
 scanf("%c",&ch);

 // Changing case
 if(ch>=65 && ch<=90)
 cch=ch+32;
 
 else if(ch>=97 && ch<=122)
 cch=ch-32;
 
 // Printing Changed case
 printf("Changed character : %c ",cch);
 
 getch(); // Linux user - Remove this

 return 0;
}


5. Program to accept a string and print it by using while loop
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable ch to store input character
 char ch; 

 // Inputing and displaying string
 printf("Enter a string : ");
 while(( ch=getchar( ))!='\n')
 putchar(ch); 

 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS
16. Programs to multiply two Matrices
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b, c with max size=10 X 10
 int a[10][10],b[10][10],c[10][10];
 
 // Declaring variable m, n, p, q = size of matrix entered by user
 int m, n, p, q;
 
 // Declaring variable i, j, k = to iterate loop
 int i, j, k;

 // Inputing size of first matrix
 printf("Enter the size of first matrices : \n");
 
 printf("Enter number of rows : ");
 scanf("%d",&m);
 
 printf("Enter number of columns : ");
 scanf("%d",&n);
 
 // Inputing size of second matrix
 printf("Enter the size of second matrices : \n");
 
 printf("Enter number of rows : ");
 scanf("%d",&p);
 
 printf("Enter number of columns : ");
 scanf("%d",&q);


 // Checking if multiplication is possible or not if possible then proceed
 if(n==p)
 {
 // Inputing first matrx elemets
 printf("Enter first matrices elements : ");
 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 
 // Inputing second matrx elemets
 printf("Enter second matrix elements : ");
 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   scanf("%d",&b[i][j]);
  }
 }
 
 // Performing multiplication
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   c[i][j]=0;
   for(k=0;k<n;k++)
   {
   c[i][j]=c[i][j]+a[i][k]*b[k][j];
   }
  }
 }
 
 // Printing multiplied matrix
 printf("The multiplication of Matrices : \n"); 
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
  printf("%d ",c[i][j]);
  }
  
  printf("\n");
 }
}

 else
 printf("Multiplication is not possible");

 getch(); // Linux user - Remove this

 return 0;
}

17. Program to print a diagonal matrix with diagonal value enter by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variale a with size = 4 X 4
 int a[4][4],i,j;
 
 // Declaring varibale d = diagonal value by user
 int d;
 
 // Inputing diagonal value
 printf("Enter diagonal value : ");
 scanf("%d",&d);
 
 // Setting diagonal value as "d" and all other value as "0"
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i==j)
   a[i][j]=d;
   else
   a[i][j]=0;
  }
 }
 
 // Printing diagonal matrix
 for(i=0;i<4;i++)
 {
 
  for(j=0;j<4;j++)
  printf("%2d",a[i][j]);
  printf("\n");
 }
  
 
 getch(); // Linux user - Remove this

 return 0;
}

18. Program to print a anti diagonal matrix with diagonal value enter by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variale a with size = 4 X 4
 int a[4][4],i,j;
 
 // Declaring varibale d = diagonal value by user
 int d;
 
 // Inputing diagonal value
 printf("Enter diagonal value : ");
 scanf("%d",&d);
 
 // Setting diagonal value as "d" and all other value as "0"
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i+j==3)
   a[i][j]=d;
   else
   a[i][j]=0;
  }
 }
 
 // Printing anti diagonal matrix
 for(i=0;i<4;i++)
 {
 
  for(j=0;j<4;j++)
  printf("%2d",a[i][j]);
  printf("\n");
 }
  
 getch(); // Linux user - Remove this

 return 0;
}

19. Program to print the sum of diagonal values and anti-diagonal values of a matrix
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring 2D array
 int a[4][4];
 
 // Declaring variable i, j to iterate loop
 int i, j;
 
 // Declaring variabl sd=to store sum of diagonal value, sa=store sum of anti diagonal value
 int sd=0, sa=0; 
 
 // Inputing matrix
 printf("Enter matrix element of size 4 X 4 : ");
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 
 // Calculating sum of diagonal values and anti diagonal values
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   if(i==j)
   sd=sd+a[i][j];
   
   if(i+j==3)
   sa=sa+a[i][j];
  }
 }

 // Printing element of matrix 
 printf("\nEntered elements in matrix : \n");
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   printf("%d ",a[i][j]);
  }
  printf("\n");
 }

 // Printing sum and diagonal of matrix
 printf("\nSum of diagonal values : %d ",sd);
 printf("\nSum of anti diagonal values : %d ",sa);
 
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS
11. Program to generate histogram of entered number
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declarring array to enter elements
 int a[10];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting 10 numbers
 printf("Enter 10 numbers :\n");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Generating histogram
 printf("%s %12s %15s\n","Element", "Value", "Hisogram");
 for(i=0;i<10;i++)
 {
  printf("%4d %12d           ",i, a[i]);
  for(j=0;j<a[i]; j++)
  {
   printf("*");
  }
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program to enter values into a two-dimensional integer array of size (4 X 3) and then display it in matrix form
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable info with row size = 4 and column size = 3
 int info[4][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting value
 
 for(i=0;i<4;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter value of info[%d][%d] : ",i ,j);
   scanf("%d",&info[i][j]);
  }
 }
 
 // Diplaying elements
 printf("Elements elements in matrix form :\n");
 for(i=0;i<4;i++)
 {
 for(j=0;j<3;j++)
 {
  printf("%d ",info[i][j]);
 }
 printf("\n");
 }
 getch(); // Linux user - Remove this

 return 0;
}

13. Program to enter values into a two-dimensional integer array of size (4 X 3) and then display the elements of the second row
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable info with row size = 4 and column size = 3
 int info[4][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i, j;
 
 // Inputting value
 
 for(i=0;i<4;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter value of info[%d][%d] : ",i ,j);
   scanf("%d",&info[i][j]);
  }
 }
 
 // Diplaying elements of second row
 printf("Elements of second row : ");
 for(j=0;j<3;j++)
 {
  printf("%d ",info[1][j]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program to find the sum of elements that are greater than 5 within a two-dimensional array through a function receiving the array as arguments
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// funciton prototype
int sum(int[][4], int, int);

int main()
{
 // Declaring variable a with row size=2 and column size=4
 int a[2][4];
 
 // Declaring variable "i" and "j" to iterate loop for rows and columns
 int i, j;
 
 // Declaring variable result = to hold the returned result 
 int result=0;
 
 // Inputting elements
 for(i=0;i<2;i++)
 {
  for(j=0;j<4;j++)
  {
   printf("Enter value of a[%d][%d] : ",i ,j);
   scanf("%d",&a[i][j]);
  }
 }
 
 // Calling function sum 
 result=sum(a, 2, 4);
 
 // Printing returned result
 printf("Sum of elements > 5 in an array = %d ",result);
 
 getch(); // Linux user - Remove this

 return 0;
}

int sum(int p[][4], int m, int n) // m is row size, n is column size
{
 // Declaring variable s=sum to hold the sum
 int s=0;
 
 // Declaring variable "i" and "j" to iterate loop for rows and columns
 int i, j;
 
 // Calculating sum
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   // Checking whether element is greater than 5
   if(p[i][j]>5)
   {
    s=s+p[i][j];
   }
  }
 } 
 
 // Returning sum
 return s;
}

15. Program To Accept 5 Student - Roll No, Marks in 3 Subjects of each student and Calculate Total, Average and Print it along with student roll Number
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring rn=roll no
 int rn[5];
 
 // Declaring 2D array 
 int marks[5][3];
 
 // Declaring variable "i" and "j" to iterate loop
 int i,j;
 
 // Declaring variable s=sum and avg=average
 int s[5]={0}, avg[5]={0};

 // Inputting name, roll no and marks
 for(i=0;i<5;i++)
 {
  printf("\nEnter %d student Roll no : ",i+1);
  scanf("%d",&rn[i]); 
  
  // Inputing stuent marks
  for(j=0;j<3;j++)
  {
   printf("Enter Marks of %d subject : ",j+1 );
   scanf("%d",&marks[i][j]); 
  }
 }
 
 // Calculating total and average
 for(i=0;i<5;i++)
 {
  for(j=0;j<3;j++)
  {
   s[i]=s[i]+marks[i][j];
  }
  
  avg[i]=s[i]/3;
 }
 
 // Printing details
 printf("\nDetails of student : \n");
 for(i=0;i<5;i++)
 {
  printf("\nStudent Roll Number : %d",rn[i]);
  printf("\nStudent Total marks = %d, Average marks = %d\n",s[i],avg[i]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAM : Arrays
6. Program to find Minimum element in an array
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a with size five to store five elements
 int a[5];
 
 // Declaring variable "i" to iterate loop
 int i;
 
 // Declaring variable min=minimum
 int min;
 
 // Inputting Element of an array
 printf("Enter five element in an array : \n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Initialising min with first element assuming it to be minimum
 min=a[0];
 
 // Determining minimum element
 for(i=0;i<5;i++)
 {
 if(min>a[i+1])
 min=a[i+1];
 }
 
 // Displaying minimum element
 printf("Minimum Element = %d",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to find highest minimum(-) temperature and lowest maximum(+) temperature
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int i,j;
 int counternegative=0, counterpositive=0;
 int temp[12];
 int max=0;
 int min=0;
 printf("Enter temperature of 12 months : " );
 
 for(i=0;i<12;i++)
 {
  scanf("%d",&temp[i]); 
 }
 
 for(i=0;i<12;i++)
 {
  if(temp[i]>0 && counterpositive==0)
  {
  min=temp[i];
  counterpositive=1;
  }
  
  else if(temp[i]<0 && counternegative==0)
  {
  max=temp[i];
  counternegative=1;
  }
 }
 
 for(i=0;i<12;i++)
 {
    if(temp[i]<0)
    {
     if(max<temp[i])
     {
     max=temp[i];
     }
    }
    
    else if(temp[i]>0)
    {
     if(min>temp[i])
     {
      min=temp[i];
     }
    }
  }
 
 printf("Max negative temperature : %d \n",max);
 printf("Min positive temperature : %d ",min);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{ 

// Declaring variabel i = for iteration of loop
int i,a[10];

// Inputting elements
printf("Enter 10 elements : ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}

// Printing first 5 numbers in original order
printf("Printing first 5 numbers in original order : ");
for(i=0;i<=4;i++)
printf("%d ",a[i]);

// Printing last five numbers in reverse order
printf("\nPrinting last 5 numbers in reverse order : ");
for(i=9;i>=5;i--)
printf("%d ",a[i]);

getch( ); // Linux user - Remove this

return 0;
}


9. Program showing Passing array to function. Program finds the average of 5 marks input by user
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

/* We have declared return type as float since value to be returned 
i.e "average" is a float value. */
float avg(float x[5])
{
      // Declaring variable sum to hold the sum of five elements entered
   float sum=0;
   
   // Declaring variable "i" to iterate loop
   int i;
   
   // Calculating sum 
      for(i=0;i<5;i++)
      {
              sum=sum+x[i];
      }
      
      // Returning average i.e sum/5
      return sum/5;
}
int main()
{
    // Declaring array variale marks with maximum size "5"
    float marks[5];
    
     // Declaring variable "i" to iterate loop
     int i;
    
    // Inputting elements or marks
    printf("Enter marks for five subject : ");
    for(i=0;i<5;i++)
    {
            scanf("%f",&marks[i]);
    }
 
 // Calling function and printing the returned resutl ( i.e average )
 printf("Average marks = %f",avg(marks));

 getch(); // Linux user - Remove this

 return 0;
}

10. Program to initialize a character array and display the it in reverse order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable names of size 5 and initializing it
 char names[5]={'h','e','l','l','o'};
 
 // Declaring variable "i" to iterate loop
        int i;
    
        // Printing in reverse order
        printf("Initialized characters in reverse order : ");
  for(i=4;i>=0;i--)
  {
  printf("%c ",names[i]);
 }

 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : ARRAYS

C PROGRAMS : ARRAYS

C PROGRAMS : Array
1. Program to accept elements in an array and display it
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    // Declaring array variable with max size=10
    int a[10];

   // Declaring variable i = to iterate loop
   int i;

   // Inputting elements
   printf("Enter 10 elements in an array : ");
   for(i=0; i<10; i++)
   {
        scanf("%d",&a[i]);
   }

   // Displaying entered elements
   printf("Entered elements are : ");
    for(i=0; i<10; i++)
   {
        printf("%d ",a[i]);
   }

   // to hold the screen defined in conio.h   
   getch(); // Linux user - Remove this

   return 0;
}

2. Program to accept elements in an array and display sum of all the elements
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
    // Declaring array variable with size=10
    int a[10]; 

   // Declaring variable s=sum to store the sum, i = to iterate loop
   int i, s=0;

   // Inputting elements
   printf("Enter 10 elements in an array : ");
   for(i=0; i<10; i++)
   {
        scanf("%d",&a[i]);
   }

   // Displaying entered elements
 printf("Entered elements are : ");
    for(i=0; i<10; i++)
   {
        printf("%d ",a[i]);
   }

   // Performing sum of all elements
    for(i=0; i<10; i++)
   {
        s = s + a[i];
   }

   // Displaying result
   printf("\nSum of all elements  :  %d ",s);

   getch(); // Linux user - Remove this

   return 0;
}

3. Program to insert element in between an array. ( INSERTION )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable "i" to iterate loop
int i;

// Declaring variable pos=postion from where we want to delete element
int pos;

// Declaring n=number which we want to insert and "m" to store "n"
int n,m;

// Declaring and initialising array
int a[6]={2,4,6,8,10};

// Printing values in array before insertion
printf("Elements before deletion\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\n");

// Inputting position
printf("Enter the position of element for insertion : ");
scanf("%d",&pos);

// Inputting value
printf("Enter the value which you want to insert : ");
scanf("%d",&n);
m=n;

// Performing insertion
for(i=5;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=m;

// Printing elements after insertion
printf("Elements after insertion\n");
for(i=0;i<6;i++)
{
printf("%d ",a[i]);
}

getch(); // Linux user - Remove this

return 0;
}


4. Program to delete element from an array. ( DELETION )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable "i" to iterate loop
int i;

// Declaring variable pos=postion from where we want to delete element
int pos;

// Declaring and initialising array
int a[5]={2,4,6,8,10};

// Printing values in array before deletion
printf("Elements before deletion\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\n");

// Inputting position
printf("Enter the position of element for deletion\n");
scanf("%d",&pos);

// Performing deletion
for(i=pos-1;i<5;i++)
{
a[i]=a[i+1];
}

// Printing elements after deletion
printf("Elements after deletion\n");
for(i=0;i<4;i++)
{
printf("%d ",a[i]);
}

getch(); // Linux user - Remove this

return 0;
}


5. Program to perform Transpose of a Matrix
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

// Declaring variable i,j to iterate loop 
int i,j;

// Declaring variable a to store matrix element
int a[2][2];

// Inserting element in Matrix
printf("Enter 2X2 matrix\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}

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

// Performing transpose of Matrix and displaying transposed Matrix
printf("\n");
printf("Transpose of matrix is \n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d ",a[j][i]);
}
}

getch(); // Linux user - Remove this

return 0;
}
C PROGRAMS : RECURSION

C PROGRAMS : RECURSION

C PROGRAMS : RECURSION
6. Program to find whether a number is Palindrome or not using recursion
/*

Palindrome Number : If Reverse of a number = Number, the number is called Palindrome No.

LOGIC : Reverse of a number is found by r=(r*10)+d
Here r=reverser and d=digit extracted from the number.

 */

#include<conio.h> // Linux user - Remove this

#include<stdio.h> 

// Declaring global variable r=reverse, d=digit
int r=0, d=0;

// Defining function with parameter n = number
int rev(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return r;

// Continue calling function rev or function invoke itself
else
{
 // Extracting digit
 d=n%10;
 
 // Finding reverse
 r=(r*10+d);
 
 // function invoke itself
 rev(n/10);
}
}

int main()
{
// Declaring variable n = number
int n;

// Declaring variable "r" to hold the reverse number
int r;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling function "rev" with actual parameter "n" passed to it
r=rev(n);

// Checking and Displaying if a Number is palindrome or Not
if(r==n)
printf("%d is a Palindrome Number ",n);

else
printf("%d is not a Palindrome Number ",n);

getch(); // Linux user - Remove this

return 0;
}

7. Program to find whether a number is Armstrong or not using recursion
/*

Armstrong Number : If sum of digits cube = Number then it is called an Armstrong Number

LOGIC : Sum of digits cube of a number is found by s=s+d*d*d
Here s=sum and d=digit extracted from the number.

 */

#include<conio.h> // Linux user - Remove this

#include<stdio.h>

// Declaring global variable r=reverse, d=digit
int s=0, d=0;

// Defining function with parameter n = number
int sum(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return s;

// Continue calling function sum or function invoke itself
else
{
 // Extracting digit
 d=n%10;
 
 // Finding reverse
 s=s+d*d*d;
 
 // function invoke itself
 sum(n/10);
}
}

int main()
{
// Declaring variable n = number
int n;

// Declaring variable "s" to hold the sum of digits cube of number
int s;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling function "sum" with actual parameter "n" passed to it
s=sum(n);

// Checking and Displaying if a Number is Armstron or Not
if(s==n)
printf("%d is an Armstrong Number ",n);

else
printf("%d is not an Armstrong Number ",n);

getch(); // Linux user - Remove this

return 0;
}


8. Program to print the fibonacci series upto nth terms using recursion
/* 

Fibonacci Series : 0 1 1 2 3 5 8 13 21 34 upto nth terms.

*/

#include<conio.h>
#include<stdio.h> // Linux user - Remove this

// Defining function with parameter n = number
int fibo(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==1)
return 0;

else if(n==2)
return 1;

// Continue calling function fibo
else if(n>2)
return fibo(n-1)+fibo(n-2);
}

int main()
{
// Declaring variable n = number
int n;

/* Declaring variable "i" to iterate loop and 
"term"=holds the current number to help print the fibonacci series */
int i, term;

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

// Calling function fibo with actual parameter "n" passed to it and displaying the value.
for(i=1;i<=n;i++)
{
 term=fibo(i);
 printf("%d ",term);
}

getch(); // Linux user - Remove this

return 0;
}


9. Program to print first "n" natural numbers in reverse order
#include<conio.h> // Linux user - Remove this

#include<stdio.h>

// Defining function with parameter n = number
void natural(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n<=1)
printf("%d ",n);

// Continue calling function natural
else
{
 printf("%d ",n);
 natural(n-1);
}


}
int main()
{
// Declaring variable n = number
int n;

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

// Displaying the value.
printf("First %d Natural Numbers in reverse order : \n",n);

// Calling function natural with actual parameter "n" passed to it 
natural(n);

getch(); // Linux user - Remove this

return 0;
}


10. Program to print pattern :
/*

*
* *
* * * 
* * * *
* * * * *

 */

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int recursion(i,j)
{
if(i>5)
return 0;
else if(j<=i)
{
 printf("* ");
 recursion(i,j+1);
}
else
{
printf("\n");
recursion(i+1,1); 
}
}
int main()
{ 
 recursion(1,1);
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program to print pattern :
/*

*
* *
* * * 
* * * *
* * * * * till n terms

 */


#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int recursion(i,j,n)
{
if(i>n)
return 0;
else if(j<=i)
{
 printf("* ");
 recursion(i,j+1,n);
}
else
{
printf("\n");
recursion(i+1,1,n); 
}
}
int main()
{ int n;
 printf("Enter the value till which you want to print the patter:");
 scanf("%d",&n);
 recursion(1,1,n);
 
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : RECURSION

C PROGRAMS : RECURSION

C PROGRAMS : RECURSION
1. Program to find the factorial of a Number using recursion
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int fact(int); // Function prototype

int main()
{
// Declaring variable n=number
int n;

// Decalring variable f = to hold the value of factorial
int f;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling Factorial function
f=fact(n);

// Printing Factorial
printf("Factorial of %d = %d ",n,f);

getch(); // Linux user - Remove this

return 0;
}

int fact(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==1)
return 1;

// Continue calling function fact
else
return n*fact(n-1);
}

2. Program to print sum of n natural numbers from 1 to n ( Number ) using recursion
#include<conio.h> // Linux user - Remove this
#include<stdio.h>

// Defining function with parameter n = number
int add(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return 0;

// Continue calling function add
else
return n+add(n-1);
}
int main()
{
// Declaring variable n = number
int n;

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

// Calling function add with actual parameter "n" passed to it and displaying the value.
printf("Sum of first n numbers = %d",add(n));

getch(); // Linux user - Remove this

return 0;
}


3. Program to calculate the power using recursion. Example a^b ( Here ^ = Power sign )
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Defining function with parameter a=Number and b=power 
int power(int a, int b)
{
 /* Base condition : Any condition where a recursive function 
 or method does not invoke itself. */
 if(b==0)
 return 1;
 
 // Continue calling function power or function invoke itself
 else
 return a*power(a,b-1);
}

int main()
{
 // Declaring variable a= Number and b=Power
 int a, b;
 
 // Inputting Number 
 printf("Enter Number : ");
 scanf("%d",&a);
 
 // Inputting Power
 printf("Enter Power : ");
 scanf("%d",&b);
 
 // Calling funciton power and displaying value returned by it.
 printf("%d ^ %d = %d ",a, b, power(a,b));
 
 getch(); // Linux user - Remove this

 return 0;
}
4. Program to find the GCD ( Greatest Common Divisior ) or HCD ( Highes Common Factor ) using recursion
/* 

LOGIC : 

The gcd of "a" and "b" is the same as the gcd of "a" and "a%b"

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Defining function with parameter a = First Number and b=Second Number
int gcd(int a, int b)
{
 /* Base condition : Any condition where a recursive function 
 or method does not invoke itself. */
 if(b==0)
 return a;
 
 // Continue calling function gcd or function invoke itself
 else
 return gcd(b,a%b);
}

int main()
{
 // Declaring variable a=First Number and b=Second Number
 int a,b;
 
 // Inputting First Number
 printf("Enter First Number : ");
 scanf("%d",&a);
 
 // Inputting Second Number
 printf("Enter First Number : ");
 scanf("%d",&b);
 
 // Calling funciton gcd and displaying value returned by it.
 printf("GCD of %d, %d = %d",a,b,gcd(a,b));
 
 getch(); // Linux user - Remove this

 return 0;
}

5. Program to Reverse a Number using recursion
/*
LOGIC : Reverse of a number is found by r=(r*10)+d
Here r=reverser and d=digit extracted from the number.

 */

#include<conio.h> // Linux user - Remove this
#include<stdio.h>

// Declaring global variable r=reverse, d=digit
int r=0, d=0;

// Defining function with parameter n = number
int rev(int n)
{
/* Base condition : Any condition where a recursive function 
or method does not invoke itself. */
if(n==0)
return r;

// Continue calling function rev or function invoke itself
else
{
 // Extracting digit
 d=n%10;
 
 // Finding reverse
 r=(r*10+d);
 
 // function invoke itself
 rev(n/10);
}
}

int main()
{
// Declaring variable n = number
int n;

// Inputting Number
printf("Enter the Number : ");
scanf("%d",&n);

// Calling function "rev" with actual parameter "n" passed to it and displaying the value.
printf("Reverse of number = %d",rev(n));

getch(); // Linux user - Remove this

return 0;
}

C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION
6. Write a function that receives a positive integer as input and returns the leading digit in its decimal representation
// For example : the leading digit of 4567 is 4

#include<stdio.h>
#include<conio.h> // Linux user - Remove this


int leading_digit(int n)
{
 // Declaring variable d=digit to store the digits extracted from number
 int d;
 
 // Performing digit extraction
 while(n)
 {
  d=n%10;
  n=n/10;
 }
 
 // Returning leading digit
 return (d);
}

int main()
{
 // Declaring variable "n" to input a number
 int n;
 
 // Inputting number
 printf("Enter any positve integer number : ");
 scanf("%d",&n);
 
 // Calling function and printing result
 printf("Leading digit of number : %d = %d ",n, leading_digit(n));
 getch(); // Linux user - Remove this

 return 0;
}

7. Implement a function that receives an integer value and returns the number of prime factors of that number. If the number is itself a prime number then return 0
/*
For example : 
for number 21, the function should return 2 as the prime factors of 21 is 3, 7
for number 3, 71 the function should return 0 as 3, 71 are prime numbers
*/
 
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int prime_factors(int n)
{
 // Decaring variable "i", "j" to iterate loop, c=counter
 int i, j, c;
 
 // Declaring variable cp=count prime to count the number of prime numbers
 int cp = 0;
 
 // Determining number of prime factors
 for(i=2; i<=n/2; i++)
 {
  c=0;
  if(n%i==0)
  { 
  for(j=1; j<=i/2; j++)
  {
   if(i%j==0)
   {
    c++;
   }
  }
  
  if(c<=1)
  {
   cp++;
  }
  }
  
 }
 
 // Returning number of prime factors
 return (cp);
 
}


int main()
{
 // Declaring variable "n" to input a number
 int n;
 
 // Inputting number
 printf("Enter any positve integer number : ");
 scanf("%d",&n);
 
 // Calling funtion and printing result
 printf("Number of prime factors of %d = %d ",n, prime_factors(n));
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to find maximum number between three numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Function prototype
int maximum(int x, int y, int z);

int main()
{
 // Declaring variable n1, n2, n3 to take the input of three numbers
 int n1, n2, n3;
 
 // Inputting value
 printf("Enter first number : ");
 scanf("%d",&n1);
 
 printf("Enter second number : ");
 scanf("%d",&n2);
 
 printf("Enter third number : ");
 scanf("%d",&n3);
 
 // Calling function maximum() and printing the returned maximum value
 printf("Maximum between three numbers : %d ", maximum(n1, n2, n3));
 
 getch(); // Linux user - Remove this

 return 0;
}

int maximum(int x, int y, int z)
{
 // Decalring variable max to store max value
 int max = x; // Assuming x to be maximum
 
 if(max<y)
 {
  max=y;
 }
 
 if(max<z)
 max=z;
 
 // Returning maximum value
 return (max);
 
}

9. Program to accept a number and print the sum of given and Reverse number using function
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{

 // Declaring variable n=to store the entered number by user
 int n; 

 /* Declaring variable r=reverse to store the reverse of a number, 
 s=sum to store the sum of given and reverse number */
 int r, s;
 
 printf("Enter a number : ");
 scanf("%d",&n);
 
 // Calling funciton rev and storing the result in "r"
 r=rev(n);
 
 // Displaying reverse number
 printf("Reverse of a number = %d",r);
 
 // Calling function add
 s=add(n,r); 
 
 // Displaying result or sum
 printf("\nSum of a given and reverse number = %d",s);
 
 getch( ); // Linux user - Remove this

 return 0;
 }
 
 int rev( int n)
 {
  /* Declaring variable d=digit to store the extracted digit, 
  rev=reverse to store reversed number */
  int d,rev=0;
  
  while(n>0)
  {
   d=n%10;
   rev=rev*10+d;
   n=n/10;
  }
  
  // Returning reversed number
  return rev;
  }
  
  int add(int n, int r)
  {
   // Returning sum of given and reverse number
   return n+r;
  }
C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION
1.Program to find sum of two numbers using function sum(int,int) that returns sum value
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int sum( int a, int b )
{
return a+b;
}

int main()
{
 
// Declaring variable x, y to take the input of two numbers
int x, y;

// Inputting value
printf("Enter the value of x:");
scanf("%d", &x);
printf("Enter the value of y:");
scanf("%d", &y);

// Displaying value by calling sum function
 printf("Sum of %d and %d : %d", x, y, sum(x, y));
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to print the area of a rectangle
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

void area()
{
 // Declaring variable l=length, b = breadth
 int l,b;
 
 // Inputting length and breadth 
 printf("Enter length of rectangle : ");
 scanf("%d",&l);
 
 printf("Enter breadth of rectangle : ");
 scanf("%d",&b);
 
 // Calculating and printing area
 printf("Area of rectangle %d * %d = %d ", l, b, l*b);
 
}

int main()
{
 area();
 
 getch(); // Linux user - Remove this

 return 0;
}

3. Program to find the factorial of two numbers and add them and print the result
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Function prototype 
int factorial(int);

int main()
{
 // Decalring variable n1, n2 to input two number
 int n1, n2;
 
 // Declaring variable f1 and f2 to store the factoial of n1, n2 respectively
 int f1, f2;
 
 // Declaring variable sum to store the sum of f1 and f2
 int sum=0;
 
 // Inputting two numbers
 printf("Enter first number : ");
 scanf("%d", &n1);
 
 printf("Enter second number : ");
 scanf("%d", &n2);
 
 // Calling function factorial
 f1 = factorial(n1);
 f2 = factorial(n2);
 
 // Calculating sum and displaying result
 sum = f1 + f2;

 printf("Factorial of first number %d = %d \n", n1, f1);
 printf("Factorial of second number %d = %d \n", n2, f2);
 printf("Sum of factorial of two numbers %d and %d = %d ",n1, n2, sum);
  
 getch(); // Linux user - Remove this

 return 0;
}

int factorial(int n)
{
 // Declaring variable "i" to iterate loop
 int i;
 
 // Declaring variabl fact to store factorial
 int fact=1;
 
 // Calculating factorial
 for(i=1;i<=n;i++)
 {
  fact = fact * i;
 }
 
 // Returning factorial
 return (fact);
}

4. Program to implement functions that returns HCF OR GCD and LCM of two numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int calculatehcf(int x, int y)
{
 // Declaring varibale "i' to iterate loop
 int i;
 
 // Declaring variable hcf to store calculated hcf
 int hcf=1;
 
 for(i=1; i<=x;i++)
 {
  if(x%i==0 && y%i==0)
  {
   hcf=i;
  }
 }
 
 // Returnig hcf
 return hcf;
}

int calculatelcm(int x, int y, int h) // h is passed value of hcf
{
 // Declaring variable lcm to store calculated lcm
 int lcm=1;
 
 // lcm is calculated by formula : hcf * lcm =1
 lcm = (x*y)/h;
 
 // Returning lcm
 return lcm;
}

int main()
{
 // Declaring variable "a" and "b" to input two numbers
 int a, b;
 
 /* Declaring variable hcf=to hold hcf returned to it, 
 lcm=to hold lcm returned to it */
 int hcf, lcm;
 
 // Inputting two numbers
 printf("Enter first number : ");
 scanf("%d", &a);
 
 printf("Enter first number : ");
 scanf("%d", &b);
 
 // Calling function calculatehcf
 hcf=calculatehcf(a, b);
 
 // Calling function calculatelcm
 lcm=calculatelcm(a, b, hcf);
 
 // Printing hcf and lcm
 printf("hcf = %d ",hcf);
 printf("\nlcm = %d ",lcm);
 
 getch(); // Linux user - Remove this

 return 0;
}

5. Implement a function that takes two values of integer type and interchange them
// 1. without using Third variable. You may use arithmetic operators
// 2. With the use of third variable. You should not use arithmetic operator

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

void swap_without_third_variable(int a, int b)
{
 // performing swapping
 a=a+b;
 b=a-b;
 a=a-b;
 
 // Printing interchange value
 printf("\nInterchange value without use of third variable : a=%d, b=%d", a, b);
 
}

void swap_with_third_variable(int a, int b)
{
 // Declaring varibale swap to help in interchanging value
 int swap;
 
 // Performing swapping
 swap=a;
 a=b;
 b=swap;
 
 // Printing interchange value
 printf("\nInterchange value without use of third variable : a=%d, b=%d", a, b);
}

int main()
{
 // Declaring variable a, b to input two numbers
 int a, b;
 
 // Inputting number
 printf("Enter value of a : ");
 scanf("%d",&a);
 
 printf("Enter value of b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Orginal value a = %d, b = %d ",a, b);
 
 // Calling functions
 swap_without_third_variable(a, b);
 swap_with_third_variable(a, b);
 
 getch(); // Linux user - Remove this

 return 0;
}