Basic C Programs(Part 2)

6.To Check Vowel:

#include <stdio.h>
int main()
{
  char ch;
 
  printf("Enter a character\n");
  scanf("%c", &ch);

  // Checking both lower and upper case, || is the OR operator

  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 isn't a vowel.\n", ch);
     
  return 0;
}

7.Removing Vowels from String:

#include <string.h>
int check_vowel(char);

int main()
{
  char s[100], t[100];
  int c, i = 0;
  printf("Enter a string");
  gets(s);

  for(c = 0; s[c] != '\0'; c++) 
    {
    if(check_vowel(s[c]) == 0) 
    {   
      t[i] = s[c];
      i++;
    }
  }

  t[i] = '\0';

  strcpy(s, t); 

  printf("On Removing Vowel: %s\n", s);

  return 0;
}

int check_vowel(char ch)
{
    if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
      return 1;
    else
      return 0;
}

8.GCD of two numbers:

#include <stdio.h>
int main()
{
    int n1, n2, i, gcd;

    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);

    for(i=1; i <= n1 && i <= n2; ++i)
    {
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }

    printf("G.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;
}

9.To check the number Palindrome 

#include<stdio.h>  
int main()    
{    
int n,r,sum=0,temp;    
printf("enter the number=");    
scanf("%d",&n);    
temp=n;    
while(n>0)    
{    
r=n%10;    
sum=(sum*10)+r;    
n=n/10;    
}    
if(temp==sum)    
printf("palindrome number ");    
else    
printf("not palindrome");   
return 0;  
}   

10.To check a String is a Palindrome:

int main()
{
  char a[100], b[100];

  printf("Enter a string\n");
  gets(a);

  strcpy(b, a);
  strrev(b);

  if (strcmp(a, b) == 0)
    printf("The string is a palindrome: %s\n", b);
  else
    printf("The string isn't a palindrome: %s\n", b);

  return 0;
}

Note: Any Related short programs regarding the above code, put it in comment session.

Other Related Posts:

Basic C Programs(PART 1)

Basic C Programs(PART 3)

Basic C Programs(PART 4)

Basic C Programs(PART 5)

Posted on by