C programming examples

c program to count vowels in a string

C program to count vowels in a string: C programming code to find how many number of vowels are present in a string, For example in the string "c programming" there are three vowels 'o', 'a' and 'i'. In the program both lower and upper case are considered i.e 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O' and 'U'. In our code we scan the complete string and check every character in it if it's a vowel then counter is incremented by one, consonants and special characters are ignored.

C programming code

#include<stdio.h>
 
int count_vowels(char []);
int check_vowel(char);

C program for pattern matching, pattern matching in c

Pattern matching in c: C programming code to check if a given string is present in another string, For example the string "programming" is present in "c programming". If the string is present then it's location (i.e. at which position it is present) is printed. We create a function match which receives two character pointers and return the position if matching occurs otherwise returns -1. We are implementing naive string search algorithm in our c program

C programming code

#include <stdio.h>
#include <string.h>
 
int match(char [], char []);
 
int main() {

C program to find next prime palindrome

C program to find next prime palindrome: In this code user will enter a number(say n) and we have to find a number greater than n which is a palindrome as well as prime. For example if the input is 7 then output will be 11 as it is prime as well as palindrome, if input is 21 then output will be 101. In our code we first check if a number is palindrome and then check if it is prime as it will take less time as primes occur more frequently then palindromes.

C programming code

#include <stdio.h>
#include <math.h>
#define TRUE 1
 
int main()
{
  long n, t, r = 0, c, d;

c program to find roots of quadratic equation

c program to find roots of quadratic equation: This program calculates roots of a quadratic equation. Coefficients are assumed to be integers but roots may be real.. Discriminant (b*b-4*a*c) decides the nature of roots. In the program j stands for iota.

C programming code

#include <stdio.h>
#include <math.h>
 
int main()
{
   int a, b, c, d;
   double root1, root2;
 
   printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
   scanf("%d%d%d", &a, &b, &c);
 
   d = b*b - 4*a*c;
 
   if (d < 0) { //complex roots
Compiler used: 
GCC
Output of program: 
Quadratic quation program output

stack program in c, c program to implement stack

Stack program in c: C program to implement stack using array.

C programming code

#include<stdio.h>
#include<stdlib.h>
 
#define MAX_SIZE 5
 
int stack[MAX_SIZE];
void push();
int pop();
void traverse();
int is_empty();
int top_element();
int top = -1;
 
main()
{
   int element, choice;
 
   while(1)
   {
      printf("Stack Operations.\n");
      printf("1. Insert into stack (Push operation).\n");
      printf("2. Delete from stack (Pop operation).\n");
      printf("3. Print top element of stack.\n");
      printf("4. Check if stack is empty.\n");

C program to find ncr and npr

c program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!

C program to find nCr using function

#include<stdio.h>
 
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
 
main()
{
   int n, r;
   long ncr, npr;
 
   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);
 
   ncr = find_ncr(n, r);
   npr = find_npr(n, r);
 
   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);
 
   return 0;
}
 
long find_ncr(int n, int r)
{
   long result;
 

Anagram in c

Anagram in c: c program to check whether two strings are anagrams or not, string is assumed to consist of alphabets only. Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings.

c program to delete duplicate elements in an array

c program to delete duplicate elements in an array

C programming code

#include<stdio.h>
 
main()
{
   int n, a[10], b[10], count = 0, c, d;
 
   printf("Enter number of elements in array\n");
   scanf("%d",&n);
 
   printf("Enter %d integers\n", n);
   for(c=0;c<n;c++)
      scanf("%d",&a[c]);
 
   for(c=0;c<n;c++)
   {
      for(d=0;d<count;d++)
      {
         if(a[c]==b[d])
            break;
      }
      if(d==count)
      {
         b[count] = a[c];
         count++;
      }
   }
 

C program for binary search

C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but it's fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort it using some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list. If the element to be searched is found then its position is printed.

The code below assumes that the input numbers are in ascending order.

C programming code for binary search

 

C program for prime number

Prime number program in c: c program for prime number, this code prints prime numbers using c programming language. To check whether a number is prime or not see another code below. Prime number logic: a number is prime if it is divisible only by one and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. A number greater than one can be factorized into prime numbers, For example 540 = 22*33*51

Pages

Subscribe to RSS - C programming examples