You are here

C programs

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 a 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

Output of program: 
Quadratic equation program output

C program to implement stack data structure

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 program in C

Anagram program in C to check whether two strings are anagrams or not. They are assumed to contain only lower case letters. They are anagrams of each other if the letters of one of them can be rearranged to form the other. So, in anagram strings, all characters occur the same number of times. For example, "ABC" and "CAB" are anagrams, as every character, 'A,' 'B,' and 'C' occur the same number of times (one time here) in both the strings.

C program to delete duplicate elements from 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++;
      }
   }
           

Binary search in C

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 isn't 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.

C programming code for binary search

 

C program to check orthogonal matrix

C code to check whether a matrix is orthogonal or not. For an orthogonal matrix AAT = I. Example orthogonal matrix:

1 0
0 1

C programming code

#include<stdio.h>
#include<conio.h>

main()
{
   int m, n, p, c, d, k, sum = 0;
   int matrix[10][10], transpose[10][10], product[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix\n");

   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&matrix[c][d]);
                 

C program to print diamond pattern

The diamond pattern in C language: This code prints a diamond pattern of stars. The diamond shape is as follows:

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

C programming code

#include <stdio.h>

int main()
{
  int n, c, k, space = 1;

  printf("Enter number of rows\n");
  scanf("%d", &n);

  space = n - 1;

  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space--;

    for (c = 1; c <= 2*k-1; c++)
      printf("*");

    printf("\n");
  }

  space = 1;

  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)

Pages

Subscribe to RSS - C programs