Saturday 5 October 2013

Getting IP Address using C

#include<stdlib.h>
 main()
{
   system("C:\\Windows\\System32\\ipconfig");
}

Print Arguments passed using Command Line

#include<stdio.h>
main(int args,char *argv[])
{
    int i=0;
    for(i=0;i<args;i++)
         printf("\n%s",argv[i]);
}

Matrix Subtraction using C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf ("%d",&m1);

printf("\nEnter the number of Columns of Mat1 : ");
scanf ("%d",&n1);

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("\nEnter the Element a[%d][%d] : ",i,j);
    scanf("%d",&a[i][j]);
    }

// ------------------------------------------

printf("\nEnter the number of Rows of Mat2 : ");
scanf ("%d",&m2);

printf("\nEnter the number of Columns of Mat2 : ");
scanf ("%d",&n2);

if ( m1 != m2 || n1 != n2 )
 {
 printf("\nOrder of two matrices is not same ");
 exit(0);
 }

for( i = 0 ; i < m2 ; i++ )
    for( j = 0 ; j < n2 ; j++ )
    {
    printf("\nEnter the Element b[%d][%d] : ",i,j);
    scanf("%d",&b[i][j]);
    }

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    c[i][j] = a[i][j] - b[i][j] ;
    }

printf("\n Matrix after Subtraction : \n");

for( i = 0 ; i < m1 ; i++ )
 {
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("%d\t",c[i][j]);
    }
  printf("\n");
 }
}

Adding all Elements of a matrix using C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[10][10],sum,m,n;
printf("\nEnter the number of Rows : ");
scanf ("%d",&m);

printf("\nEnter the number of Columns : ");
scanf ("%d",&n);

for(i=0;i<m;i++)
       for(j=0;j<n;j++ )
       {
       printf("\nEnter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++ )
       {
       sum = sum + a[i][j];
       }
printf("\nThe Addition of All Elements in the Matrix : %d\n",sum);
}

Addition of Diagonal Elements of a matrix using C

#include<stdio.h>
main()
{
int i,j,a[10][10],sum,m,n;

printf("\nEnter the number of Rows : ");
scanf ("%d",&m);

printf("\nEnter the number of Columns : ");
scanf ("%d",&n);

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
       printf("\nEnter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
        if ( i == j )
        sum = sum + a[i][j];
       }
printf("\n Result after adding Diagonal Elements: %d\n",sum);
}

Convert given string into uppercase

#include <stdio.h>
#include <string.h>
 main()
{
    char string[] = "strupr in c";

    printf("%s\n",strupr(string));
}

Convert given string into Lowercase

#include <stdio.h>
#include <string.h>
 main()
{
    char string[] = "Strlwr in C";

    printf("%s\n",strlwr(string));
 }

Swap 2 numbers without using 3rd variable

#include<stdio.h>
main()
{
int a,b;

printf("nEnter value for num1 & num2 : ");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("nAfter swapping value of a : %d",a);
printf("nAfter swapping value of b : %d",b);
}

Print all ASCII values using C

#include<stdio.h>
main()
{
int i=0;
char ch;
for(i=0;i<256;i++)
  {
    printf("%c ",ch);
    ch = ch + 1;
  }
}

Delete vowels from a string using C

#include <stdio.h>
#include <string.h>
int check_vowel(char);
main()
{
  char s[100], t[100];
  int i, j = 0;

  printf("Enter a string to delete vowels\n");
  gets(s);

  for(i = 0; s[i] != '\0'; i++) {
    if(check_vowel(s[i]) == 0) {       //not a vowel
      t[j] = s[i];
      j++;
    }
  }

  t[j] = '\0';

  strcpy(s, t);    //We are changing initial string

  printf("String after deleting vowels: %s\n", s);
}
 
int check_vowel(char c)
{
  switch(c) {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}

Check if entered String is a Palindrome

#include <stdio.h>
#include <string.h>
 main()
{
   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");
   gets(a);

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

   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
}

Reverse a string using C

#include <stdio.h>
#include <string.h>
 main()
{
   char arr[100];

   printf("Enter a string to reverse\n");
   gets(arr);
   strrev(arr);
   printf("Reverse of entered string is \n%s\n",arr);
}

Concatenate two strings using C

#include <stdio.h>
#include <string.h>
 main()
{
   char a[100], b[100];

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

   printf("Enter the second string\n");
   gets(b);

   strcat(a,b);

   printf("String obtained on concatenation is %s\n",a);
}

Copy a string using C

#include<stdio.h>
#include<string.h>
main()
{
   char source[] = "C program";
   char destination[50];

   strcpy(destination, source);

   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);
}

Compare two strings using C

#include <stdio.h>
#include <string.h>
 main()
{
   char a[100], b[100];

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

   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");
}

Calculate length of a string using C

#include <stdio.h>
#include <string.h>
 main()
{
   char a[100];
   int length;

   printf("Enter a string to calculate it's length\n");
   gets(a);
   length = strlen(a);
   printf("Length of entered string is = %d\n",length);
}

Print a String using C

#include <stdio.h>
 main()
{
    char array[20] = "Hello World";
    printf("%s\n",array);
}

Calculate Simple Interest using C

#include<stdio.h>
main()
{
float amount,rate,time,si;
printf("nEnter Principal Amount : ");
scanf("%f",&amount);
printf("\nEnter Rate of interest : ");
scanf("%f",&rate);
printf("\nEnter Period of Time   : ");
scanf("%f",&time);
si = (amount * rate * time)/100;
printf("\nSimple Intrest : %f\n",si);
}

Convert temperature in Celsius to Fahrenheit using C

#include<stdio.h>
main()
{
float celsius,fahrenheit;
printf("nEnter temp in Celsius : ");
scanf("%f",&celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("nTemperature in Fahrenheit : %f \n ",fahrenheit);
}

Calculate Total marks and percentage of a Student using C

#include<stdio.h>
main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
printf("\nEnter marks of 5 subjects : ");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d",sum);
per = (sum * 100) / total;
printf("\nPercentage : %f\n",per);
}

Calculate Gross Salary using C

#include<stdio.h>
#include<conio.h>
main()
{
float gross_salary,basic,da,ta;
printf("Enter basic salary : ");
scanf("%f",&basic);
da = ( 10 * basic ) / 100;
ta = ( 12 * basic ) / 100;
gross_salary = basic + da + ta;
printf("Gross salary : %f\n",gross_salary);
}

Calculate Area of a Square using C

#include<stdio.h>
main()
{
int side,area;
printf("\nEnter the Length of Side : ");
scanf("%d",&side);
area = side * side ;
printf("\nArea of Square : %d\n",area);
}

Calculate Area of a Rectangle using C

#include<stdio.h>
#include<conio.h>
main()
{
int length,breadth,area;
printf("\nEnter the Length of Rectangle : ");
scanf("%d",&length);
printf("\nEnter the Breadth of Rectangle : ");
scanf("%d",&breadth);
area = length * breadth;
printf("\nArea of Rectangle : %d\n",area);
}

Calculate Area of Right Angled triangle using C

#include<stdio.h>
main()
{
int base,height;
float area;
printf("nEnter the base of Right Angle Triangle : ");
scanf("%d",&base);
printf("nEnter the height of Right Angle Triangle : ");
scanf("%d",&height);
area = 0.5 * base * height;
printf("nArea of Right Angle Triangle : %f\n",area);
}

Calculate Area of Equilateral triangle using C

#include<stdio.h>
#include<math.h>
main()
{
int side;
float area;
printf("\nEnter the Length of Side : ");
scanf("%d",&side);
area = (sqrt(3) / 4)* side * side ;
printf("\nArea of Equilateral Triangle : %f\n",area);
}

Calculate Area of a scalene triangle using C

#include<stdio.h>
#include<math.h>
main()
{
int s1,s2,angle;
float area;
printf("\n Enter Side1 : ");
scanf("%d",&s1);
printf("\n Enter Side2 : ");
scanf("%d",&s2);
printf("\n Enter included angle : ");
scanf("%d",&angle);
area = (s1 * s2 * sin((3.14/180)*angle))/2;
printf("\n Area of Scalene Triangle : %f",area);
}

Calculate Area and Circumference of a circle using C

#include<stdio.h>
main()
{
int rad;
float PI=3.14,area,ci;

printf("\nEnter radius of circle: ");
scanf("%d",&rad);

area = PI * rad * rad;
printf("\nArea of circle : %f ",area);

ci = 2 * PI * rad;
printf("\nCircumference : %f \n",ci);
}

Multiplication of Matrix using C

#include <stdio.h>
main ()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

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

  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);
     
      printf("First Matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",first[c][d]);
      }
      printf("\n");
   }

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

  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
 printf("Second Matrix :-\n");

   for( c = 0 ; c < p ; c++ )
   {
      for( d = 0 ; d < q ; d++ )
      {
         printf("%d\t",second[c][d]);
      }
      printf("\n");
   }
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }
}

Find Transpose of a Matrix using C

#include <stdio.h>
 main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];

   printf("Enter the number of rows and columns of matrix: ");
   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]);
      }
   }
 
   printf("Orignal Entered Matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",matrix[c][d]);
      }
      printf("\n");
   }
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }

   printf("Transpose of entered matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }
      printf("\n");
   }
}

Adding two matrix using C

#include <stdio.h>
main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

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

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c][d]);
 
   printf("First Matrix :-\n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         printf("%d\t",first[c][d]);
      }
      printf("\n");
   }

   printf("Enter the elements of second matrix:\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d", &second[c][d]);
 
   printf("Second Matrix :-\n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         printf("%d\t",second[c][d]);
      }
      printf("\n");
   }
           

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t", sum[c][d]);

      printf("\n");
   }
}

Selection Sort using C

#include <stdio.h>
 main()
{
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements: ");
   scanf("%d", &n);

   printf("Enter %d integers:\n", n);

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

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
}

Insertion Sort using C

#include <stdio.h>
 main()
{
  int n, array[1000], c, d, t;

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

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }

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

    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;

      d--;
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }
}

Bubble Sort using C

#include <stdio.h>
 main()
{
  int array[100], n, c, d, swap;

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

  printf("Enter %d integers\n", n);

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

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
}

Deleting elements from an Array using C

#include <stdio.h>
 main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array: ");
   scanf("%d", &n);

   printf("Enter %d elements:\n", n);

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

   printf("\nEnter the location where you wish to delete element\n");
   scanf("%d", &position);

   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];

      printf("Resultant array is\n");

      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }
}

Inserting elements into an Array using C

#include <stdio.h>
 main()
{
   int array[100], position, c, n, value;

   printf("Enter number of elements in array: \n");
   scanf("%d", &n);

   printf("\nEnter %d elements\n", n);

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

   printf("\nEnter the location where you wish to insert an element: \n");
   scanf("%d", &position);

   printf("\nEnter the value to insert: \n");
   scanf("%d", &value);

   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];

   array[position-1] = value;

   printf("Resultant array is:\n");

   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);
}

Finding frequency of characters in a string using C

#include<stdio.h>
main()
{
char c[10],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a characeter to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='\0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
}

Access elements of an Array using pointers in C

#include<stdio.h>
main()
{
int data[5], i;
printf("Enter elements: ");
for(i=0;i<5;++i)
scanf("%d",data+i);
printf("You entered: ");
for(i=0;i<5;++i)
printf("%d\n",*(data+i));
}

Calculate Average of a given array using C

#include<stdio.h>
main()
{
int n, i;
float num[100], sum=0.0, average;
printf("Enter the numbers of data: ");
scanf("%d",&n);
while (n>100 || n<=0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d",&n);
}
for(i=0; i<n;i++)
{
printf("%d. Enter number: ",i+1);
scanf("%f",&num[i]);
sum+=num[i];
}
average=sum/n;
printf("Average = %.2f",average);
}

Thursday 3 October 2013

Converting Binary to Octal or Octal to Binary using C

#include<stdio.h>
#include<math.h>
int binary_octal(int n);
int octal_binary(int n);
main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'b' to convert octal to binary.\n");
printf("2. Enter alphabet 'o' to convert binary to octal.\n");
scanf("%c",&c);

if (c =='b' || c == 'B')
{
printf("Enter an octal number: ");
scanf("%d", &n);
printf("%d in octal = %d in binary", n, octal_binary(n));
}

if (c =='0' || c == 'O')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in octal", n, binary_octal(n));
}
}

int binary_octal(int n)
{

int octal=0, decimal=0, i=0;
while(n!=0)
{
decimal+=(n%10)*pow(2,i);
++i;
n/=10;
}

i=1;
while (decimal!=0)
{
octal+=(decimal%8)*i;
decimal/=8;
i*=10;
}
return octal;
}

int octal_binary(int n)
{

int decimal=0, binary=0, i=0;
while (n!=0)
{
decimal+=(n%10)*pow(8,i);
++i;
n/=10;
}

i=1;
while(decimal!=0)
{
binary+=(decimal%2)*i;
decimal/=2;
i*=10;
}

return binary;
}

Converting an Octal to Decimal or Decimal to Octal using C

#include<stdio.h>
#include<math.h>
int binary_decimal(int n);
int decimal_binary(int n);
main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert octal to decimal.\n");
printf("2. Enter alphabet 'o' to convert decimal to octal.\n");
scanf("%c",&c);

if (c =='d' || c == 'D')
{
printf("Enter a octal number: ");
scanf("%d", &n);
printf("%d in octal = %d in decimal", n, octal_decimal(n));
}

if (c =='0' || c == 'O')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in octal", n, decimal_octal(n));
}
}

int decimal_octal(int n) /* Function to convert decimal to octal.*/
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}

int octal_decimal(int n) /* Function to convert octal to decimal.*/
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++i;
}
return decimal;
}

Converting a Binary to Decimal or Decimal to binary using C

#include<stdio.h>
#include<math.h>
int binary_decimal(int n);
int decimal_binary(int n);
main()
{
int n;
char c;
printf("Instructions:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
scanf("%c",&c); if (c =='d' || c == 'D')
{
printf("Enter a binary number: ");
scanf("%d", &n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c == 'B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
}

int decimal_binary(int n) /* Function to convert decimal to binary.*/
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}

Simple Calculator using C

# include<stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&operator);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
printf("num2/num1 = %.2f",num1/num2);
break;
default:
printf("Error! operator is not correct");
break;
}

Finding size of int,float,double and char of your system using C

#include<stdio.h>
 main()
 {
 int i;
 float f;
 double d;
 char c;
 printf("Size of int: %d bytes\n",sizeof(i));
 printf("Size of float: %d bytes\n",sizeof(f));
 printf("Size of double: %d bytes\n",sizeof(d));
 printf("Size of char: %d byte\n",sizeof(c));
}

Finding roots of a Quadratic equation using C

#include<stdio.h>
#include<math.h>
 int main()
 {
 float a, b, c, determinant, r1,r2, real, imag;
 printf("Enter coefficients a, b and c: ");
 scanf("%f%f%f",&a,&b,&c);
 determinant=b*b-4*a*c;
 if (determinant>0)
 {
 r1= (-b+sqrt(determinant))/(2*a);
 r2= (-b-sqrt(determinant))/(2*a);
 printf("Roots are: %.2f and %.2f",r1 , r2);
 }
 else if (determinant==0)
 { r1 = r2 = -b/(2*a);
 printf("Roots are: %.2f and %.2f", r1, r2);
 }
 else
 {
 real= -b/(2*a);
 imag = sqrt(-determinant)/(2*a);
 printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
 }
 }

Calculating power of an integer using C

/* C program to calculate the power of an integer*/
#include<stdio.h>
main()
{
int base, exp;
long int value=1;
printf("Enter base number and exponent respectively: ");
scanf("%d%d", &base, &exp);
while (exp!=0)
{ value*=base; /* value = value*base; */
--exp;
}
printf("Answer = %d", value);

Counting digits of a number using C

#include<stdio.h>
main()
{
int n,count=0;
printf(" Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
n/=10; /* n=n/10 */ ++count; }
printf("\n Number of digits: %d \n",count);

Checking whether character entered is Alphabet or not using C

#include<stdio.h>
main()
{
char c;
printf("\n Enter a character: ");
scanf("%c",&c);
if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
printf(" %c is an alphabet.\n",c);
else
printf(" %c is not an alphabet.\n",c);

Determining whether a number is positive/negative using C

#include<stdio.h>
main()
{
int num;
printf("Enter any number: ");
scanf("%d",num);
if(num>0)
printf("\n The Number you entered is a +ve integer \n");
else if(num<0)
printf("\n The Number you entered is a -ve integer \n");
else
printf("\n You have entered zero on the console \n");
}

Finding ASCII value of a character using C

#include<stdio.h>
main()
{
char c;
printf("Enter the character whose ASCII value is to be known: ");
scanf("%c",&c);
printf("\n The ASCII value of %c is %d \n",c,c);
}

Reversing an Array using C

#include <stdio.h>
 main()
{
   int n, c, d, a[100], b[100];
   printf("Enter the number of elements in array\n");
   scanf("%d", &n);
   printf("Enter the array elements\n");

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

   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");
   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);
}

Displaying Multiplication table of any number using C

#include<stdio.h>
main()
{
int num,i;
printf("Enter the number for which multiplication table is to be displayed:\t");
scanf("%d",&num);
printf("Multiplication table for %d is as follows:\n",num);
for(i=1;i<=10;i++)
{
printf("%d * %d = %d \n",num,i,num*i);
}
}

Binary Search using C

#include<stdio.h>
main()
{
int i,high,low,mid,size,search,array[20];
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("\n Welcome to Binary Search program\n");
printf("\n How many elements you want to enter in this array\n");
scanf("%d",&size);
printf("\n Enter %d elements \n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
printf("\n Enter the element to be searched\n");
scanf("%d",&search);
high=size-1;
low=0;
mid=(high+low)/2;
while(low<=high)
{
if(array[mid]<search)
low=mid+1;
else if(array[mid]==search)
{
printf("\n Element(%d) found at position %d\n",search,mid+1);
break;
}
else
high=mid-1;
mid=(high+low)/2;
}
if(low>high)
printf("\n Element not found !!!\n");
}

Linear Search using C

#include<stdio.h>
main()
{
int array[15],search,loc=1,size,i;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("\n Welcome to Linear Search Program\n");
printf("\n How many elements you want to enetr in this Array:: ");
scanf("%d",&size);
printf("\n Start entering %d elements \n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
printf(" Enter the element to be searched: ");
scanf("%d",&search);
printf("\n Search proces has started !\n");
for(i=0;i<size;i++)
{
if(array[i]==search)
{
printf("\n The element(%d) you searched for was found at %d position\n",search,loc+i);
break;
}
}
if(i==size)
printf("\n Element not found !!!\n");
printf("\n Linear Search completed :)\n");
}

Printing the Fibonacci Series using C

#include<stdio.h>
 main()
{
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   scanf("%d",&n);
   printf("First %d terms of Fibonacci series are :-\n",n);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
}

Finding Maximum and Minimum element of an array using C

#include<stdio.h>
main()
{
int array[10],num,i,max,min,maxloc=1,minloc=1,size;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("\nEnter the numbers of elements you want in this array\n");
scanf("%d",&size);
printf("\nEnter %d elements of this array\n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
max=array[0];
min=array[0];
for(i=1;i<size;i++)
{
if(array[i]>max)
{
max=array[i];
maxloc=i+1;
}
if(array[i]<min)
{
min=array[i];
minloc=i+1;
}
}
printf("\n%d is the greatest element in the Array at %d position\n",max,maxloc);
printf("\n%d is the smallest element in the Array at %d position\n",min,minloc);
}

Addition using pointers in C

#include<stdio.h>
main()
{
int num1,num2,*p,*q,sum;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("\n::Enter the numbers:: ");
scanf("%d%d",&num1,&num2);
p=&num1;
q=&num2;
sum=*p+*q;
printf("\n::Sum of the numbers using pointers is::%d\n",sum);
}

Printing Pascal triangle using C

#include <stdio.h>

long factorial(int);
main()
{
   int i, n, c;
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");

      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

Printing '*' pattern in the shape of Reverse Pyramid using C

#include <stdio.h>
 main()
{
   int row, col, n, temp;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   printf("Enter the number of rows in Reverse pyramid of stars you wish to see: ");
   scanf("%d",&n);
   temp = 1;
  for (row = 1; row <= n; row++,temp++)
  {
    for (col = 1; col < temp; col++)
      printf(" ");
    for (col = 1 ; col <= 2*(n-row)-1; col++)
      printf("*");
    printf("\n");
  }
}

Printing '*' pattern in the shape of Pyramid using C

#include <stdio.h>
 main()
{
   int row, col, n, temp;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   printf("Enter the number of rows in pyramid of stars you wish to see: ");
   scanf("%d",&n);
   temp = n;
   for ( row = 1 ; row <= n ; row++,temp-- )
   {
      for ( col = 1 ; col < temp ; col++ )
      {
         printf(" ");
      }
      for ( col = 1 ; col <= 2*row - 1 ; col++ )
      {
         printf("*");
      }
        printf("\n");
   }
}

Printing Floyd triangle using C

#include <stdio.h>
 main()
{
  int n, r,  c, a = 1;
  printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
  for (r = 1; r <= n; r++)
  {
    for (c = 1; c <= r; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
 }

Printing '*' pattern in the shape of a Diamond using C

#include <stdio.h>
 main()
{
   int row, col, n, temp;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   printf("Enter the number of rows in Diamond of stars you wish to see: ");
   scanf("%d",&n);
   temp = n-1;
   for ( row = 1 ; row <= n ; row++,temp-- )
   {
      for ( col = 1 ; col <= temp ; col++ )
      {
         printf(" ");
      }
      for ( col = 1 ; col <= 2*row - 1 ; col++ )
      {
         printf("*");
      }
        printf("\n");
   }
 
   temp = 1;
  for (row = 1; row <= n; row++,temp++)
  {
    for (col = 1; col <= temp; col++)
    {
      printf(" ");
   }
    for (col = 1 ; col <= 2*(n-row)-1; col++)
    {
      printf("*");
    }
printf("\n");
  }
}

Printing '*' pattern2 using C

#include <stdio.h>
 main()
{
    int n, r, c,temp;
    printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter number of rows\n");
    scanf("%d",&n);
    temp=n;
    for ( r = 1 ; r <= n ; r++ )
    {
        for( c = 1 ; c <=temp ; c++ )
            printf("*");
            temp--;
        printf("\n");
    }
}

Printing '*' pattern1 using C

#include <stdio.h>
 main()
{
    int n, c, k;
    printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter number of rows\n");
    scanf("%d",&n);
    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");
        printf("\n");
    }
}

Generating an Armstrong Number using C

#include<stdio.h>
#include<math.h>
main()
{
long num,i,sum=0,temp,rem;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the Number until which Armstrong numbers need to found::");
scanf("%ld",&num);
printf("The following Armstrog Numbers occur between 1 to %ld",num);
for (i=1;i<num;i++)
{
temp=num;
while(temp!=0)
{
rem=num%10;
sum=sum+pow(rem,3);
num=num/10;
}
if(sum==temp)
printf("%ld",sum);
sum=0;
}
}

Determining whether a number is Armstrong number or not using C

#include<stdio.h>
#include<math.h>
main()
{
int num,rem,sum=0,temp;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number:\t");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
sum=sum+pow(rem,3);
num=num/10;
}
if(temp==sum)
printf("Given number(%d) is an Armstrong Number \n",temp);
else
printf("Given number(%d) is not an Armstrong Number \n",temp);
}

Finding Permutation and Combination of numbers using C

#include<stdio.h>
long factorial(int);
long combination(int, int);
long permutation(int, int);
main()
{
   int n, r;
   long ncr, npr;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   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 combination(int n, int r)
{
   long result;
   result = factorial(n)/(factorial(r)*factorial(n-r));
   return result;
}

long permutation(int n, int r)
{
   long result;
   result = factorial(n)/factorial(n-r);
   return result;
}

long factorial(int n)
{
   int c;
   long result = 1;
   for( c = 1 ; c <= n ; c++ )
      result = result*c;
   return ( result );
}

Determining whether a given number is Palindrome or not using C

#include<stdio.h>
main()
{
int num,rem,rev=0,temp;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number:\t");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(temp==rev)
printf("Given number(%d) is a palindrome\n",temp);
else
printf("Given number(%d) is not a palindrome\n",temp);
}

Finding Reverse of a Number using C

#include<stdio.h>
main()
{
int num,rem,rev=0;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number:\t");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("Reverse of the given number is %d \n",rev);
}

Swapping two numbers using C

#include <stdio.h>
main()
{
   int x, y, temp;
   printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temp = x;
   x    = y;
   y    = temp;
   printf("After Swapping \n x = %d \n y = %d \n",x,y);
 }

Adding digits of a number using C

#include<stdio.h>
main()
{
int num,rem,sum=0,temp;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number: ");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("Sum of the digits of %d is %d\n",temp,sum);
}

Decimal to binary conversion using C

#include <stdio.h>
main()
{
  int n, c, k;
  printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
  printf("%d in binary number system is:\n", n);
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
  printf("\n");
}

Finding HCF and LCM of a number using C

#include <stdio.h>
 main()
{
  int a, b, x, y, t, gcd, lcm;
  printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);
  a = x;
  b = y;
  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
  gcd = a;
  lcm = (x*y)/gcd;
  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
}

Adding 'n' numbers using C

#include<stdio.h>
main()
{
int n,i,sum=0,value;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number of integers you want to add\n");
scanf("%d",&n);
printf("Enter %d Integers\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&value);
sum=sum+value;
}
printf("\n Sum of given %d integers is %d \n",n,sum);
}

Finding Factorial of a number using C

#include<stdio.h>
main()
{
int n,i,fact=1;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the no for which factorial is to be determined\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d is %d\n",n,fact);
}

A given year is leap year or not using C

#include <stdio.h>
 main()
{
  int year;
  printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter a year to check if it is a leap year:");
  scanf("%d", &year);
  if ( year%4 == 0 )
    printf("\n%d is a leap year.\n", year);
  else
    printf("\n%d is not a leap year.\n", year);
}

Determining whether a character is vowel or not using C

#include <stdio.h>
 main()
{
  char ch;
  printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
  printf("Enter a character\n");
  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 not a vowel.\n", ch);
}

Finding a number is odd/even using C

#include<stdio.h>
main()
{
int num;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0)
printf("number(%d) entered is an even number\n",num);
else
printf("number(%d) entered is an odd number\n",num);
}

Arithmetic Operations using C

#include<stdio.h>
main()
{
int a,b,add,sub,mul,div,mod;
printf("\n(There is) none worthy of worship except Allah(Subhanahu wa ta'ala).Muhammad(Sallallahu alayhi wasallam) is Messenger of ALLAH(Subhanahu wa ta'ala).\n");
printf("Enter the numbers: ");
scanf("%d%d",&a,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
printf("Result of Addition of %d and %d is %d\n",a,b,add);
printf("Result of Subtraction of %d and %d is %d\n",a,b,sub);
printf("Result of Multiplication of %d and %d is %d\n",a,b,mul);
printf("Result of Division of %d and %d is %d\n",a,b,div);
printf("Result of Modular of %d and %d is %d\n",a,b,mod);
}

Printing an Integer in C

/* Printing an Integer in C */


#include <stdio.h>
 main()
{
  int number; 
  printf("Enter an integer:\n");
  scanf("%d", &number);
  printf("Integer that you have entered is %d\n", number);
}

"Hello World" in C

/* Simple 'Hello World' program in C */

#include <stdio.h>
 main()
{
       printf("Hello world :) This is Khan \n");
}