Saturday, January 8, 2011

C Program to Sorting a characters in the String

Coding:


#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
void main()
{
char a[200],temp;
int n=0,j,i;
clrscr();
textcolor(23);
cprintf("\nEnter the string: ");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
textcolor(23);
cprintf("\nThe string in alphabetical order:");
for(i=0;i<n;i++)
{
textcolor(10);
cprintf("%c",a[i]);
}
getch();
}

Output:



C Program for Prime Numbers upto n Number

Coding:


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,j,c;
clrscr();
textcolor(10);
 cprintf("\nEnter Number Of Terms::");
scanf("%d",&n);
textcolor(12);
cprintf("\nPrime Numbers Are Follwing:\r\n\n");
while(i<=n)
{
   c=0;
   for(j=1;j<=i;j++)
   {
     if(i%j==0)
c++;
   }
  if(c==2)
  printf("%28d\n",i);
  i++;
}
getch();
}

Output:



C Program to convert Numbers into Words

Coding:


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

void pw(long,char[]);
char *one[]={" ",
    " One"," Two"," Three"," Four",
    " Five"," Six"," Seven"," Eight",
    " Nine"," Ten"," Eleven"," Twelve",
    " Thirteen"," Fourteen"," Fifteen",
    " Sixteen"," Seventeen"," Eighteen",
    " Nineteen"
    };

char *ten[]={ " "," "," Twenty"," Thirty"," Forty",
     " Fifty"," Sixty","Seventy"," Eighty",
     " Ninety"
    };


void main()
{
 long n;
 clrscr();
 printf("\n\n Enter any 9 digit no: ");
 scanf("%ld",&n);
  printf("\n");
 if(n<=0)
printf("Enter numbers greater than 0");
 else
 {

 pw((n/10000000),"Crore");
 pw(((n/100000)%100),"Lakh");
 pw(((n/1000)%100),"Thousand");
 pw(((n/100)%10),"Hundred");
 pw((n%100)," ");
 }
 getch();
}


void pw(long n,char ch[])
{
 textcolor(YELLOW);
 (n>19)?cprintf("%s %s ",ten[n/10],one[n%10]):cprintf("%s ",one[n]);
 if(n)
 cprintf("%s ",ch);
}

Output:



C Program to find Factorial with and without using Recursion Function

Coding:


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

long fact(long);
void main()
{
int i;long f;

/* Initialization for Factorial without using Recursion
int i,n;
long f=1;   */

clrscr();
printf("\nEnter the number: ");
scanf("%d",&i);
f=fact(i);

/* Factorial without using Recursion
for(n=1;n<=i;n++)
f*=n;  */

printf("\nFactorial of %d is %ld",i,f);
getch();
}

long fact(long k)
{
if(k==0)
return 1;
else
return k*(fact(k-1));

}

Output:


C Program to find Largest number of 'n' numbers

Coding:

#include<stdio.h> 
#include<conio.h> 
void main()
{
int n,m,i,max;
clrscr();
printf("\nHow many numbers are you going to enter:");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
textcolor(YELLOW);
scanf("%d",&m);
max=m;
for(i=2;i<=n;i++)
{
scanf("%d",&m);
if(m>max)
max=m;
}
cprintf("\nThe Largest Number is ");
textcolor(GREEN);
cprintf("%d",max);
getch();
}

Output: