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

Tuesday, 26 March 2019

C Program to Check Whether a Character is Vowel or Consonant


This program demonstrates the functioning of if else statements. Program asks user for an alphabet and gives the output whether alphabet is vowel or consonant.
The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
//C Program to Check Whether a alphabet is Vowel or Consonant
#include <stdio.h>
int main()
{
    char ch;
    printf("Enter an alphabet: ");
    scanf("%c",&ch);
     if((ch=='a' || ch=='A') || (ch=='e' || ch=='E') || (ch=='i' || ch=='I') || (ch=='o' || ch=='O') || (ch=='u' || ch=='U'))
        printf("%c is a vowel.\n", ch);
    else
        printf("%c is a consonant.\n", ch);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.

C Program to check whether a number is even or odd

This program demonstrates the functioning of if else statements. Program asks user for an integer number and gives the output whether number is even or odd.
An integer is even if it is divisible by 2 meaning if by dividing number by 2, remainder is zero, then number is even otherwise it is an odd number.
//C Program to demonstrate the functioning of if else statements
//check if number is divisible by 2. If it is, then number is even otherwise it is odd number
#include <stdio.h>
int main()
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    if(num % 2 == 0)
        printf("%d is even number.\n", num);
    else
        printf("%d is odd number.\n", num);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 19 March 2019

C program to swap two numbers using bit-wise XOR (method 3)

Swapping is used in sorting algorithms like in bubble sort,that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.
Problem:
Given two numbers num1=a and num2=b, task is exchange values of these numbers i.e num1=b and num2=a;

Method 3: Using bit-wise XOR
#include<stdio.h>
int main(){
    int num1=10,num2=20;
    void swapXOR(int,int);  //function prototype
   printf("Values before swapping by call by value:num1=%d,num2=%d.\n",num1,num2);
    swapXOR (num1,num2);    
    return 0;
}
//function definition : using bitwise XOR operator
void swapXOR (int n1,int n2){
  n1 = n1 ^ n2;
  n2 = n1 ^ n2;
  n1 = n1 ^ n2;
  printf("Inside swap3 : num1=%d,num2=%d\n",n1,n2);
}
Output:
Please comment if you find anything incorrect, or you want to improve the topic discussed above.

C program to swap two numbers without using temporary variable(method 2)

Swapping is used in sorting algorithms like in bubble sort,that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.
Problem:
Given two numbers num1=a and num2=b, task is exchange values of these numbers i.e num1=b and num2=a;
Method 2: Without using third variable
#include<stdio.h>
int main(){
    int num1=10,num2=20;
    void swapWithoutTemp (int,int);  //function prototype
   printf("Values before swapping by call by value: num1=%d,num2=%d.\n",num1,num2);
    swapWithoutTemp (num1,num2);     
    return 0;
}
//function definition : without using third variable
void swapWithoutTemp(int n1,int n2){
  n1=n1+n2;
  n2=n1-n2;
  n1=n1-n2;
  printf("Inside swap2 : num1=%d,num2=%d\n",n1,n2);
}


Output:
Please comment if you find anything incorrect, or you want to improve the topic discussed above.

C program to swap two numbers using temporary variable(method 1)

Swapping is used in sorting algorithms like in bubble sort,that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.
Problem:
Given two numbers num1=a and num2=b, task is exchange values of these numbers i.e num1=b and num2=a;
Method 1: Using third variable
#include<stdio.h>
int main(){
    int num1=10,num2=20;
    void swapUsingTemp(int,int);  //function prototype
   printf("Values before swapping by call by value: num1=%d,num2=%d.\n",num1,num2);
    swapUsingTemp (num1,num2);
    return 0;
}
//function definition: using third variable
void swapUsingTemp (int n1,int n2){
  int temp;
  temp=n1;
  n1=n2;
  n2=temp;
  printf("Inside swap1 : num1=%d,num2=%d\n",n1,n2);
}
Output:
Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Monday, 11 March 2019

C program to find number of words or tokens in a string

Following program returns the number of words contained in a string.Suppose, we have a string str="Computer Science & Engineering",functions return 4.

#include<stdio.h>
int main(void)
{
  char str[]="Computer Science & Engineering";
  int t=1,i,n=0;
  i=0;
  while(str[i]!='\0')
  {
      if(i==0)
      while(str[i]==' ')
           i++;
      if(str[i]==' ')
        t++;
    while(str[i]==' ')
         i++;
       i++;
  }
  printf("\nTokens=%d\n",t);
  return 0;
}

Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Sunday, 10 March 2019

C program to copy last n characters a string to another string

This function copies the last n characters of a string to another string. For example, suppose string1 contains "Computer Science" ,then copying last 5 characters of string1 will make string to contain "ience".

#include<stdio.h>
#include<string.h>
void strncopylast(char *str1,char *str2,int n)
{   int i;
    int l=strlen(str1);
    if(n>l)
    {
        printf("\nCan't extract more characters from a smaller string.\n");
        exit(1);
    }
    for(i=0;i<l-n;i++)
         str1++;
    for(i=l-n;i<l;i++)
    {
        *str2=*str1;
         str1++;
         str2++;
    }
    *str2='\0';
}
int main()
{
    char str1[]="Computer Science & Engineering";
    char str2[50];
    int n;
    printf("\nEnter number of character to be copied?");
    scanf("%d",&n);
    strncopylast(str1,str2,n);
    printf("str2=");
    puts(str2);
    return 0;
}
Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Thursday, 28 February 2019

Displaying elements of an array in reverse order

 Problem : Displaying the elements of an array in reverse order
  Input :
          Array[5]={10,20,30,40,50};
  Output:
          50,40,30,20,10
/*
C program to display elements in reverse order: program will not change the order in memory.it will simply display in reverse order
*/
#include<stdio.h>
int main(){
    int array[10];
    int i;

    //Entering numbers into  array
    for(i=0; i<10; i++){
        printf("Enter a number\n");
        scanf("%d",&array[i]);
    }
   
    //Reading and Displaying numbers from  array in their original order
    printf("\nArray elements in their original order:\n");
    for(i=0;i<10;i++){
        printf("%d\t",array[i]);
    }

    printf("\nArray elements in reverse order:\n");
    //Reading and Dislaying numbers from  array in reverse order
    for(i=9;i>=0;i--){
        printf("%d\t",array[i]);
    }
    printf("\n");
    return 0;
}

Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Wednesday, 27 February 2019

Finding number of elements in an array

Problem: Given an array of integers ,find the number of elements in it.
Examples:
       Input : array[]={76,90,87,98,56,77,91}
       Output :
          No of Elements :7
         
//C program to find the number of elements in an array
#include<stdio.h>
    int main(){
       int array[]={76,90,87,98,56,77,91};
       int arraySize=0;

      /*key here is sizeof operator which return the size in byte: sizeof(array) 
       determines the size of array in bytes and sizeof(array[0]) determines of first 
       element in byte.*/

      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(array[0]);

       printf("Number of Elements in the array are %d.\n",arraySize);
  return 0;
}
Output:













Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 26 February 2019

C program to find minimum and maximum elements in an array

Problem : Given an array of integers ,find the smallest element in it.
Examples:
       Input : array[]={76,90,87,98,56,77,91}
       Output :
          Maximum : 98
          Minimum : 56
Solution:
   Minimum Element:
  1. We assume that first element of the array is the minimum one.
  2. Then iterate the array and compare each element with the assumed minimum element.
  3. If current element is minimum than assumed minimum element, we update the assumed minimum element to current element. This process continues till all elements are checked.

Maximum Element:
  1. We assume that first element of the array is the maximum one.
  2. Then iterate the array and compare each element with the assumed maximum element.
  3. If current element is larger than assumed largest element, we update the assumed maximum element to current element. This process continues till all elements are checked.


// C program to find the minimum and maximum elements in an array
#include<stdio.h>
    int main(){
       int array[]={76,90,87,98,56,77,91};
       int max,min,arraySize=0,i;
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
       max=array[0];  //assume that first element is the maximum one
       min=array[0];  //assume that first element is the minimum one
       for (i = 1; i < arraySize; i++){             
         if (array[i]>max){
            max=i;
         }
        if (array[i]<min){
            min=i;
         }            
       }
       printf("Maximum Element is %d at position %d.\n",array[max],max);
       printf("Minimum Element is %d at position %d.\n",array[min],min);
 return 0;
}
Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

C program to find smallest element in an array

Problem : Given an array of integers ,find the smallest element in it.
Examples:
       Input : array[]={12,34,65,98,15,34}
       Output : 12
       Input : array[]={22,30,95,90,15,54}
       Output : 15
Solution:
  1. We assume that first element of the array is the smallest one.
  2. Then iterate the array and compare each element with the assumed smallest element.
  3. If current element is smaller than assumed smallest element, we update the assumed smallest element to current element. This process continues till all elements are checked.

// C program to find the smallest element in an array
#include<stdio.h>
int main(){
      int array[]={76,90,87,98,56,77,91};
       int min,arraySize=0;
      
       //function declaration
       int minElement(int array[],int n);
      
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       //function calling
       min = minElement (array,arraySize);

       printf("Smallest Element is %d.\n", min);
            return 0;
}

//function definition
    int minElement(int array[],int n){
       int min,i;
       min =array[0];  //assume that first element is the smallest one
       for (i = 1; i < n; i++){                 
         if (array[i]< min){
            min=array[i];
         }          
       }
     return min;
      }
Output:
. 







                                                                                                                                                                                                

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

C program to find the largest element in an array

Problem : Given an array of integers ,find the largest element in it.
Examples:
       Input : array[]={12,34,65,98,15,34}
       Output : 98
       Input : array[]={22,30,95,90,12,54}
       Output : 95
Solution:
  1. We assume that first element of the array is the largest one.
  2. Then iterate the array and compare each element with the assumed largest element.
  3. If current element is larger than assumed largest element, we update the assumed largest element to current element. This process continues till all elements are checked.

// C program to find the largest element in an array
#include<stdio.h>
int main(){
      int array[]={76,90,87,98,56,77,91};
       int max,arraySize=0;
      
       //function declaration
       int maxElement(int array[],int n);
      
        //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       //function calling
       max=maxElement(array,arraySize);

       printf("Maximum Element is %d.\n",max);
            return 0;
}

//function definition
      int maxElement(int array[],int n){
            int max,i;
            max=array[0];  //assume that first element is the largest one
       for (i = 1; i < n; i++){                 
         if (array[i]>max){
            max=array[i];
         }          
       }
               return max;
      }
Output:












Please comment if you find anything incorrect, or you want to improve the topic discussed above.