Write a c program to sort strings in descending order
#include
#include
int main()
{
int sort[10],i,j,t;
printf("\n\nEnter 10 Elements In Array To Sort In Descending Order :\n\n");
for(i=0;i<10;i++) i="0;i<10;i++)" j="i+1;j<10;j++)">sort[j])
{
t=sort[i];
sort[i]=sort[j];
sort[j]=t;
}
}
}
printf("Array Elements in Descending Order Are :");
for(i=9;i>=0;i--)
{
printf("%d\n",sort[i]);
}
getch();
return 0;
}
Output
Enter 10 Elements In Array To Sort In Descending Order :
20
36
5
2
7
9
13
52
45
63
Array Elements in Descending Order Are :63
52
45
36
20
13
9
7
5
2
C program use all string functions
#include
#include
#include
void main()
{
int l,comp,cmp;
char str[10], s1[10], s2[10], cat[20];
clrscr();
printf("Enter 1st String : ");
gets(str);
l=strlen(str);
printf("\nThe length of the String : %d",l);
strupr(str);
printf("\nIn UPPERCASE : %s",str);
strlwr(str);
printf("\nIn lowercase : %s",str);
strrev(str);
printf("\nIn Reverse : %s",str);
printf("\n\nEnter two strings : ");
scanf("%s %s",&s1,&s2);
cmp=strcmp(s1,s2);
if(cmp==0)
printf("\nStrings are equal");
else
printf("\nStrings are not equal");
strcat(s1,s2);
printf("\n\nString Concatenation : %s",s1);
strcpy(s1,s2);
printf("\nString2 copied into String1 : %s",s1);
getch();
}
Output:
Enter 1st String : yahoo
The length of the String : 5
In UPPERCASE : YAHOO
In lowercase : yahoo
In Reverse : oohay
Enter two strings : yahoo
yahoo
Strings are equal
String Concatenation : yahooyahoo
String2 copied into String1 : yahoo
Wite a C program to Print the status of a character.
#include
#include
void main()
{
char a;
clrscr();
printf("\nEnter any character : ");
scanf("%c",&a);
if(a>=65 && a<=90) printf("\n%c is an Uppercase Character",a); else if(a>=97 && a<=122) printf("\n%c is a lower character",a); else if(a>=48 && a<=57) printf("\n%c is either a Special Character or Numeric ",a); else printf("\n%c is a Special character",a); getch(); } /* Output for 3 conditions are given below: Enter any character : a a is a lower character Enter any character : A A is an Uppercase Character Enter any character : 1 1 is either a Special Character or Numeric */ Posted by IT Career at 10:29 AM 1 comments Labels: C Programs Sunday, October 14, 2007 C programs Write a program to sort the elements of an array in descending order. /*Write a program to sort the elements of an array in descending order. */ # include # include void main() { int a[20],n,i,j,t; clrscr(); printf("Enter the number of elements in an array:"); scanf("%d",&n); for(i=0;i { printf("\nEnter the %d value:",i+1); scanf("%d",&a[i]); } for(i=0;i { for(j=i;j { if(a[i] { t=a[i]; a[i]=a[j]; a[j]=t; } } } for(i=0;i printf("\n%d",a[i]); getch(); } /* Enter the number of elements in an array:3 Enter the 1 value:6 Enter the 2 value:5 Enter the 3 value:8 8 6 5 */ Posted by IT Career at 7:09 AM 0 comments Labels: C Programs C programs Write a function to find the GCD of 2 nos recursively. /*Write a function to find the GCD of 2 nos recursively. Also write the main() function to use it. */ #include #include int gcd(int a,int b); void main() { int r,g,a,b; clrscr(); printf("Enter two numbers:"); scanf("%d %d",&a,&b); g=gcd(a,b); printf("GCD : %d",g); getch(); } int gcd (int a,int b) { if(b>a)
return gcd(b,a);
if(b==0)
return a;
else
return gcd(b,a%b);
}
Output:
Enter two numbers:2
4
GCD : 2
C program Program to concatenate two strings. (don’t use inbuilt function strcat()
/*Program to concatenate two strings. (don’t use inbuilt function strcat() ) */
# include
# include
void main()
{
int i,j,k=0;
char str1[50],str2[50],str3[50];
clrscr();
printf("\nEnter a string: ");
scanf("%s",str1);
printf("\nEnter a string: ");
scanf("%s",str2);
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
for(j=i;str2[k]!='\0';j++)
{
str3[j]=str2[k];
k++;
}
for(i=0;i
printf("%c",str3[i]);
getch();
}
Output
Enter a string: TOM
Enter a string: JERRY
TOMJERRY
C programs Write a program to read a string and copy the alphabets into another string.
/*Write a program to read a string and copy the alphabets into another string.
For example “NEW DELHI- 110 066” is to be copied as “ NEW DELHI”.
Program to copy the alphabets to another string*/
# include
# include
void main()
{
char str[30],str1[30];
int i= 0,j=0,k=0;
clrscr();
printf("Enter the string: ");
gets(str);
while(str[i]!='\0')
{
j=str[i];
if ((j>=65 && j<=90) || (j>=97 && j<=122)) { str1[k]=str[i]; k++; } i++; } for(i=0;i { printf("%c",str1[i]); } getch(); } /* Output Enter the string: Google 45 yahoo 477 Googleyahoo */ Posted by IT Career at 7:07 AM 0 comments Labels: C Programs C Programs Write a C Program to swap 2 strings /*Write a C Program to swap 2 strings */ #include #include #include void main() { char s1[20], s2[20], temp[20]; clrscr(); printf("Enter 1st String : "); gets(s1); printf("Enter 2nd String : "); gets(s2); strcpy(temp,s1); strcpy(s1,s2); strcpy(s2,temp); printf("\nString after Swapping :); printf("\nString1 : %s",s1); printf("\nString2 : %s",s2); getch(); } /*Output Enter 1st String : Microsoft OS Enter 2nd String : Intel Processor Strings after Swapping : String1 : Intel Processor String2 : Microsoft OS */ Posted by IT Career at 7:04 AM 0 comments Labels: C Programs C Programs Write a C Program to swap 2 strings /*Write a C Program to swap 2 strings */ #include #include #include void main() { char s1[20], s2[20], temp[20]; clrscr(); printf("Enter 1st String : "); gets(s1); printf("Enter 2nd String : "); gets(s2); strcpy(temp,s1); strcpy(s1,s2); strcpy(s2,temp); printf("\nString after Swapping :); printf("\nString1 : %s",s1); printf("\nString2 : %s",s2); getch(); } /*Output Enter 1st String : Microsoft OS Enter 2nd String : Intel Processor Strings after Swapping : String1 : Intel Processor String2 : Microsoft OS */ Posted by IT Career at 7:04 AM 0 comments C Program Print the status of a character. - UpperCase or Lowecase /* Print the status of a character. - UpperCase or Lowecase*/ #include #include void main() { char a; clrscr(); printf("\nEnter any character : "); scanf("%c",&a); if(a>=65 && a<=90) printf("\n%c is an Uppercase Character",a); else if(a>=97 && a<=122) printf("\n%c is a lower character",a); else if(a>=48 && a<=57) printf("\n%c is either a Special Character or Numeric ",a); else printf("\n%c is a Special character",a); getch(); } /* Output for 3 conditions are given below: Enter any character : a a is a lower character Enter any character : A A is an Uppercase Character Enter any character : 1 1 is either a Special Character or Numeric */ Posted by IT Career at 7:03 AM 0 comments Labels: C Programs C Program Print the ascii chart //Print the ascii chart. #include #include void main() { char a; int i; clrscr(); printf("ASCII CHART"); for(i=97;i<=125;i++) { a=i; printf("\t%c = %d",a,i); } getch(); } /* Output ASCII CHART a = 97 b = 98 c = 99 d = 100 e = 101 f = 102 g = 103 h = 104 i = 105 j = 106 k = 107 l = 108 m = 109 n = 110 o = 111 p = 112 q = 113 r = 114 s = 115 t = 116 u = 117 v = 118 w = 119 x = 120 y = 121 z = 122 { = 123 | = 124 } = 125 */ Posted by IT Career at 7:03 AM 0 comments Labels: C Programs C programs Print the factorial of a all numbers from 1 to 15. /*Print the factorial of a all numbers from 1 to 15.*/ #include #include void main() { float i,f=1; clrscr(); for(i=1;i<=15;i++) { f=f*i; printf("\nFactorial of %0.0f :%0.0f",i,f); } getch(); } /* Output Factorial of 1 :1 Factorial of 2 :2 Factorial of 3 :6 Factorial of 4 :24 Factorial of 5 :120 Factorial of 6 :720 Factorial of 7 :5040 Factorial of 8 :40320 Factorial of 9 :362880 Factorial of 10 :3628800 Factorial of 11 :39916800 Factorial of 12 :479001600 Factorial of 13 :6227020800 Factorial of 14 :87178289152 Factorial of 15 :1307674279936 */ Posted by IT Career at 7:01 AM 0 comments Labels: C Programs C Program to Find the sum of digits of a numbers /* Find the sum of digits of a number.*/ # include # include void main() { int no,sum=0,rem=0; clrscr(); printf("\nEnter a Number: "); scanf("%d",&no); while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\nSum of digits of a number: %d",sum);
getch();
}
Output
Enter a Number: 26
Sum of digits of a number: 8
C Program for Palindrome
/* Program to find if a given number is Palindrome or not */
#include
#include
void main()
{
int num,rev=0,rem=0,temp;
clrscr();
printf("\n Enter a number:");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
rev=rem+rev*10;
num=num/10;
}
if(rev==temp)
{
printf("\n This Number is a Palindrome");
}
else
printf("\n This Number is not a Palindrome");
getch();
}
Output
Enter a number: 454
This Number is a Palindrome
C Programs Fibonacci numbers
/*Print first ‘M’ Fibonacci numbers.*/
#include
#include
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("\nEnter the number: ");
scanf("%d",&n);
printf("%d %d",a,b);
for(i=3;i<=n;i++) { c=a+b; a=b; b=c; printf(" %d",b); } getch(); } Output
Enter the number: 5
0 1 1 2 3
C Programs Multipliction table
/*Enter a number n, and print out its multiplication table from 1 to m.*/
#include
#include
void main()
{
int num,i,mul,no;
clrscr();
printf("\nEnter the number: ");
scanf("%d",&num);
printf("Enter the number of times: ");
scanf("%d",&no);
for (i=1;i<=no;i++) { mul=num*i; printf("\n%d * %d = %d",num,i,mul); } getch(); } Output
Enter the number: 4
Enter the number of times: 5
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
C Program to print asterix graph
//Program to print asterix graph
# include
# include
void main()
{
int no,i,j;
clrscr();
printf("Enter a Number: ");
scanf("%d",&no);
for(i=1;i<=no;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } getch(); } Output
Enter a Number: 4
*
**
***
****
C Programs Average of a Number
//Enter 3 numbers and print their average.
# include
# include
void main()
{
int a,b,c;
float total,avg;
clrscr();
printf("\nEnter 3 numbers:\n");
scanf("\n%d %d %d",&a,&b,&c);
total=a+b+c;
avg=total/3;
printf("\nAverage = %0.02f",avg);
getch();
}
Output
Enter 3 numbers:
43
69
52
Average = 54..67
Saturday, May 17, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment