Write a C program to print given numbers as a pyramid. In this program we use the simple for statements to produce the patterns. This program prints the following output, 1 232 34543 4567654 567898765 Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
–>
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
Write a C Program to convert number to words.
Example:
Input: 4562
Output: four thousand five hundred sixty two
Solution
The program takes a number between 0 and 99999 as input. First, we count the total number of digits in the number. This is done by successively dividing the number by 10 until we reach 0. Next we go through each digit in the number and decide what to print based on the value of digit at that position. Digit at a particular position can be obtained by dividing the number by a divider. Divider for a given position is pow(10, (position-1)). We start from the left most position and move right as we identify digit at each position and print relevant words to screen.
In the above example, we first divide the number(4562) by 1000 and get 4. We print four thousand and move on to next digit. The next number to work with can be obtained by num % divider. In this case 4562 % 1000 = 562. We divide this number by 100 (divider for position 3) to get 5. So, we print five hundred and move on to next digit. So forth and so on.
Special case handling
A value of 1 at position 2 and 5 needs special attention. Take these examples. In 23, when we encounter 2 at position 2, we can be print ‘twenty’ without knowing next digit. It will be taken care of next. But in case of 15, when we encounter 1 in position 2, we can’t print anything yet! It could be any number between eleven and nineteen. So, we have to wait till we see the next digit. To keep track of this, we maintain a flag variable which is set to 1 to indicate such a case.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
int main(void) { int toss = 0; int call = 0; srand(time(NULL));
toss = rand() % 2;
printf("Say head or tail! press 0 for head and 1 for tail:nn"); scanf("%d", &call); if(call==0 || call==1) { if(toss == call) { if(toss==1) printf("You called it correctly ... it is tailn"); else printf("You called it correctly ... it is headn"); } else { if(toss==1) printf("No way ...it is head !n"); else printf("No way ... it is tail!n"); } } else printf("Invalid call!!!!!nn"); return 0; }
C Strings: Write a C program to Encryption and Decryption of password. In this program we encrypt the given string by subtracting the hex value from it. Password encryption is required for the security reason, You can use so many functions like hash or other keys to encrypt. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
C Strings: Write a c program to check whether two strings are anagrams of each other.
Two strings are said to be anagrams, if the characters in the strings are same in terms of numbers and value ,only arrangement or order of characters are may be different.
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include<stdio.h>
#include<string.h>
# define NO_OF_CHARS 256
int Anagram(char *str1, char *str2)
{
// Create two count arrays and initialize all values as 0
int count1[NO_OF_CHARS] = {0};
int count2[NO_OF_CHARS] = {0};
int i;
// For each character in input strings, increment count in
// the corresponding count array
for (i = 0; str1[i] && str2[i]; i++)
{
count1[str1[i]]++;
count2[str2[i]]++;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1[i] || str2[i])
return 0;
// Compare count arrays
for (i = 0; i < NO_OF_CHARS; i++)
if (count1[i] != count2[i])
return 0;
return 1;
}
main()
{
char str[100], str1[100];
int flag = 0;
printf("Enter first stringn");
gets(str);
printf("Enter second stringn");
gets(str1);
flag=Anagram(str, str1);
if (flag==1)
printf(""%s" and "%s" are anagrams.n", str, str1);
else
printf(""%s" and "%s" are not anagrams.n", str, str1);
return 0;
}
Read more c programs
C BasicC Strings
You can easily select the code by double clicking on the code area above.
Write a C Program to delete a file. To delete a file in c, We use the remove macro, which is defined in stdio.h. remove macro takes filename with extension as its argument and deletes the file, if it is in the directory and returns the zero, if deleted successfully. Note that deleted file does not goes to the recycle bin, it is going to delete permanently. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
#include<stdio.h> #include<conio.h> #include<process.h> main() { FILE *fp; char filename[20]; int status; clrscr(); printf("nEnter the file name:nn"); scanf("%s",filename);
fp = fopen(filename, r);
if(fp == NULL) { printf("Error: file not found! check the directory!!nn"); exit(0); } fclose(fp); status = remove(file_name);
if( status == 0 ) printf("%s file deleted successfully.n",file_name); else { printf("Unable to delete the filen"); perror("Error"); }
Write a C Program to demonstrate strspn function. The strspn() function returns the index of the first character in string1 that doesn’t match any character in string2. If all the characters of the string2 matched with the string1, strspn returns the length of the string1. Read more about C Programming Language .
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
#include<stdio.h> #include<string.h> #include<conio.h> int main() { int spn; char str1[] = "1234hjbcde09i"; char str2[] = "12345"; char str3[] = "a12d3456fd"; char char_list[] = "1234567890"; clrscr(); //It returns 4 spn = strspn (str1,char_list); printf ("nnThe length of the initial numbers for str1 is %d.n",spn);
//It returns 5 spn = strspn (str2,char_list); printf ("nThe length of the initial numbers for str2 is %d.n",spn);
//It returns 0 spn = strspn (string3,char_list); printf ("nThe length of the initial numbers for string3 is %d.n",spn); return 0; }
C Strings: Write a C program to replace the sub string in a string with another string. In this program, We replace string with another string, if the entered string is the sub string of main string, otherwise function replace_str returns the original string as it is. Read more about C Programming Language .
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
int main(void) { char str[100],str1[50],str2[50]; printf("Enter a one line string..n"); gets(str); printf("Enter the sub string to be replaced..n"); gets(str1); printf("Enter the replacing string....n"); gets(str2); puts(replace_str(str, str1, str2));
Write a c program to find all the permutations of a given string. Example: If the given string is sam, output is, sam sma msa mas asm ams. Read more about C Programming Language .
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/
C Program to demonstrate how the escape characters work.Escape character is the single character, and it is compiled as the character constant. Escape characters are used to solve the c format problems. An escape sequence is denoted by using the backslash () as an escape character, followed by a single character indicating the nonprintable character desired. Read more about C Programming Language .
/*********************************************************** * You can use all the programs on www.c-program-example.com * for personal and learning purposes. For permissions to use the * programs for commercial purposes, * contact [email protected] * To find more C programs, do visit www.c-program-example.com * and browse! * * Happy Coding ***********************************************************/