Coding Bot: Program Codes

Like Us On Facebook

Follow Us On Twitter


Monday, 2 January 2017

Armstrong Number C Code

Armstrong number is a number that is equal to the sum of the digits raised to the power of the number of digits. So for example:

371 = 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371


There are other such numbers as well: 150, 370, 407, 8208, 1741725, etc.


According to Wikipedia Narcissistic Number or Armstrong Number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b = 10 for the decimal system or b = 2 for the binary system.



C Code to check Armstrong Number:

/* Double-Click To Select Code */
#include <stdio.h>
#include <conio.h> 
int power(int, int);
 
void main()
{
   int n, sum = 0, temp, remainder, digits = 0;
 
   printf("Input an integer\n");
   scanf("%d", &n);
 
   temp = n;
   // Count number of digits
   while (temp != 0) {
      digits++;
      temp = temp/10;
   }
 
   temp = n;
 
   while (temp != 0) {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
   }
 
   if (n == sum)
      printf("%d is an Armstrong number.\n", n);
   else
      printf("%d is not an Armstrong number.\n", n);
}
 
int power(int n, int r) {
   int c, p = 1;
 
   for (c = 1; c <= r; c++) 
      p = p*n;
 
   return p;   
}

Output:

Armstrong Number C Code

Please share if this post helped you :)

Quick Sort Algorithm And C++ Code

Quicksort, or partition-exchange sort, is a sorting algorithm that, on average, makes O(n log n) comparisons to sort n items.  It was developed by Tony Hoare. Quicksort is faster in practice than other O(n log n) algorithms such as Bubble sort or Insertion Sort. Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be done with only O(log n) additional space.
Quicksort is a comparison sort and is not a stable sort.

Its complexity is as follows:
Best Case - O(n log n)
Worst Case - O(n^2) 
Average Case - O(n log n)

Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.
The steps are:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
The base case of the recursion are lists of size zero or one, which never need to be sorted.

Algorithm/Pseudo-code For Quicksort:


/* Double-Click To Select Code */

  function quicksort('array')
      if length('array') ≤ 1
         return 'array'  // an array of zero or one elements is already sorted
      select and remove a pivot value 'pivot' from 'array'
      create empty lists 'less' and 'greater'
      for each 'x' in 'array'
         if 'x' ≤ 'pivot' then append 'x' to 'less'
         else append 'x' to 'greater'
      return concatenate(quicksort('less'), 'pivot', quicksort('greater')) 
// two recursive calls



Here is an Image depicting Quick Sort:


Quick Sort
In-place partition in action on a small list. The boxed element is the pivot element, blue elements are less or equal, and red elements are larger.

If you want to see the C Code for Quick Sort, you can refer to my post here.

C++ Program For Quick Sort:


/* Double-Click To Select Code */
#include<iostream.h>
#include<conio.h>

using namespace std;


int part(int low,int high,int *a)
{
//Function for partitioning array
     int i,h=high,l=low,p,t;  //p==pivot
     p=a[low];
     while(low<high)
     {
                    while(a[l]<p)
                    {
                                   l++;
                    }
                    while(a[h]>p)
                    {
                                   h--;
                    }
                    if(l<h)
                    {
                                t=a[l];
                                a[l]=a[h];
                                a[h]=t;
                    }
                    else
                    {
                        t=p;
                        p=a[l];
                        a[l]=t;
                        break;
                    }
     }
     return h;    
}

void quick(int l,int h,int *a)
{
  int index,i;
  if(l<h)
  {
          index=part(l,h,a);
          quick(l,index-1,a);
          quick(index+1,h,a);
  }
}

void main()
{
      int a[100],n,l,h,i;
      cout<<"Enter number of elements:";
      cin>>n;
      cout<<"Enter the elements (Use Space As A Separator):";
      for(i=0;i<n;i++)
      cin>>a[i];
      cout<<"\nInitial Array:\n";
      for(i=0;i<n;i++)
      {
                      cout<<a[i]<<"\t";
      }   
      h=n-1;
      l=0;
      quick(l,h,a);
      cout<<"\nAfter Sorting:\n";
      for(i=0;i<n;i++)
      {
                cout<<a[i]<<"\t";
      }
      getch();
}

Output of Program:


Quick Sort Algorithm And C++ Code


Please Comment If You Liked This Post !! 

This article uses material from the Wikipedia article Quick Sort which is released under the Creative Commons Attribution-Share-Alike License 3.0

Tuesday, 27 December 2016

Find Number Of Occurrences Of a Character In a String C Program

This program will find out the number of occurrences of each character in a string. So for example, if we have a string "I AM" then here the frequency of occurrence of each of 'I','A','M' will be 1. The program will print the same. This code can be easily modified to account for uppercase characters separately. 


Here is the C Code to find the number of occurrences of a character in a string:


/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char string[100];
   int c = 0, count[26] = {0};
 
   printf("Enter a string\n");
   gets(string);
 
   while (string[c] != '\0')
   {
      if (string[c] >= 'a' && string[c] <= 'z') 
         count[string[c]-'a']++; 
      c++;
   }
 
   for (c = 0; c < 26; c++)
   { 
      if (count[c] != 0)
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }
}

Output:


Find Number Of Occurrences Of a Character In a String C Program