Pages

Custom Search
Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Saturday, August 9, 2014

Program to convert string to title case

Program to convert string to title case.
In this program we are going to convert this first letter of every word in the string to UPPER CASE.
and all other letters to lower case.
This is simple but interesting program.

The LOGIC part:
Logic of the program is very simple.
Character by character parse the string from start to end of string.
Check if each letter is in proper case. If not convert it to the proper case.
SOURCE CODE:
 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[50];  
      int i, j, len;  
      printf("\nEnter a string: ");  
      gets(str);  
      printf("\nEntered string: ");  
      puts(str);  
      len=strlen(str);  
      i=0;  
      while(i!=len)                                   //til end of string  
      {  
           if((i==0))                                   //first letter of string  
           {  
                if(str[i]>90)                         //if it is not UPPER case  
                     str[i] = str[i]%65+33;  
                i++;  
           }  
           else if((str[i]!=' ') && (str[i]<=90))          //any other position than first letter of word  
           {  
                str[i]=str[i]%97+32;  
                i++;  
           }  
           else if(str[i]==' ')                    //for first letter of a word  
           {  
                i++;  
                if(str[i]>96)  
                     str[i]=str[i]%65+33;  
                i++;  
           }  
           else  
                i++;                                   //if letter is in correct case, simply increment i  
      }  
      str[i]='\0';  
      printf("\nOutput: ");  
      puts(str);  
      printf("\n\n");  
      return 1;  
 }  


SAMPLE OUTPUT

Friday, March 23, 2012

Quicksort to sort elements in ascending order using the first element as the Pivot.



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:
    -Pick an element, called a pivot, from the list.[Fisrt element in this program.]
    -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.
    -Recursively sort the sub-list of lesser elements and the sub-list of greater elements.



Source Code:




 #include <stdio.h>  
 #include <stdlib.h>  
 int partition(int a[], int l, int r)  
 {  
     int p, i, j, t;  
     p = l;  
     i = l;  
     j = r;  
     while(i<j)  
     {  
          while((a[ i ] <= a[ p ]) && (i<r))  
          {  
             i++;  
          }  
          while(a[ j ] > a[ p ])  
          {  
             j--;  
         }  
         if(i<j)  
         {  
             t=a[ i ];  
             a[ i ] = a[ j ];  
             a[ j ] = t;  
         }  
     }  
     t=a[ p ];  
     a[ p ] = a[ j ];  
     a[ j ] = t;  
     return j;  
 }  
 void quicksort(int a[50], int l, int r)  
 {  
     int s;  
     if(l < r)  
     {  
         s=partition(a,l,r);  
         quicksort(a, l, s-1);  
         quicksort(a, s+1, r);  
     }  
 }  
 int main()  
 {  
     int a[50], l, r, i, n;  
     printf("\nEnter the size of the array: ");  
     scanf("%d", &n);  
     srand(time(NULL));  
     for(i=0;i<n;i++)  
     {  
         a[ i ] = rand()%50 + 1;  
     }  
     printf("\nThe elements of the array before sort are: \n");  
     for(i=0;i<n;i++)  
     {  
         printf("%d ", a[ i ]);  
     }  
     quicksort(a,0,n-1);  
     printf("\nThe elements of the array after sort are: \n");  
     for(i=0;i<n;i++)  
     {  
         printf("%d ", a[ i ]);  
     }  
     getch();  
 }  
Sample Output:





Tuesday, October 11, 2011

Program to generate superprime numbers in a given range.


//Program to generate superprimes in a given range and display the number of superprimes.
//leave space between lower limit and upper limit

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

void superprime();                   //Function declaration

int main()
{
int  sn, en;
printf("\nEnter range of numbers: ");
scanf("%d %d", &sn, &en);

superprime(sn, en);                 //Function call

getch();
}
void superprime(int sn, int en)
{
int a, digit, d=2, flag=1, f=1, t, i, c=0;;

for(i=sn;i<=en;i++)
{
t=i;
flag=1;
d=2;
while(d<t)
{
if((t%d)==0)
{
flag=0;
break;
}
d++;
}
if(flag==1)
{
while(t>0)
{
a=2;
digit=t%10;
f=1;
while(a<digit)
{
if((digit%a)==0)
{
f=0;
break;
}
a++;
}
t=t/10;
if(f==0)
break;
}
if(f==1)
{
printf("%d\t", i);
c++;
}
}
}
printf("\n\nNumber of superprimes: %d\n", c);
}

SAMPLE OUTPUT:



Program to generate armstrong numbers in a given range.


//Generate armstrong number in a given range.
Leave space between the upper and the lower range.

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

void range_arm();                    //Function declaration

int main()
{
        int sn, en;
        printf("\nEnter range of numbers: ");
        scanf("%d %d", &sn, &en);
        range_arm(sn, en);                   //Function call
        getch();
}
void range_arm(int sn, int en)
{
        int t, cube, scube=0, d, i, c=0;

        for(i=sn;i<=en;i++)
        {
                scube=0;
                t=i;
                while(t>0)
                {
                        d=t%10;
                        cube=d*d*d;
                        scube=scube+cube;
                        t=t/10;
                }
                 if(scube==i)
                {
                        printf("%d\t", i);
                        c++;
                }
        }
        if(c==0)
        {
                printf("\nNo armstrong numbers in the given range");
        }
        printf("\n");
}

SAMPLE OUTPUT:


Program to check whether entered number is armstrong or not.


//An armstrong number is an number in which the sum of cubes of the digits of the number equals the number.
For eg: 153 = 1(1's cube) + 125(5's cube) + 27(3's cube)

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

void armstrong();                             //Function declaration

void main()
{
        int num, a;
        printf("\nEnter an integer number: ");
        scanf("%d", &num);
        armstrong(num);                  //Function call
        getch();
}
void armstrong(int p)
{
        int t, cube, scube=0, d;
        t=p;
        while(p>0)
{
                d=p%10;                     //Separating digit's
                cube=d*d*d;               //Cube of separated digit
                scube=scube+cube;     //Adding cube to sum of cube(scube)
                p=p/10;
        }
        if(scube==t)
                printf("\nGiven number is armstrong\n");
        else
                printf("\nGiven number is not armstrong\n");
        return;
}

SAMPLE OUTPUT:


Program to check whether the entered number is superprime or not.


//For a number to be a superprime number the first condition is that it should be a prime number. If the number is a prime number, then for it to become superprime the digits in the prime number should also be prime.
For ex: 19 is a prime number; 1 is a prime but 9 is not a prime. Hence 19 is not a superprime number.
            13 is a prime number; 1 and 3 both are prime numbers. Hence 13 is a superprime number.

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

void superprime(int *p);

int main()
{
        int num;
        printf("\nEnter an integer number: ");
        scanf("%d", &num);
        superprime(&num);
        getch();
}
void superprime(int *p)
{
        int a, digit, d=2, flag=1, f=1, t;
        t=*p;

        while(d<*p)
        {
                if((*p%d)==0)
                {
                        flag=0;
                        break;
                }
                d++;
         }
                if(flag==1)
                {
                        f=1;
                        while(t>0)
                        {
                                a=2;

                                digit=t%10;
                                while(a<digit)
                                {
                                         if((digit%a)==0)
                                         {
                                                f=0;
                                                break;
                                         }
                                         a++;
                                }
                                t=t/10;
                        }
                 }

        if(f==1&&flag==1)
                printf("\nGiven number is superprime\n");
        else if(flag==0)
                printf("\nGiven number is not prime\n");
        else if(f==0&&flag==1)
                printf("\nGiven number is prime but not superprime");
        else
                printf("\nGiven number is not prime\n");
        return;
}

SAMPLE OUTPUT:



Custom Search