C program to print given number in words

  • Write a program to input a digit and print it in words
  • C program to print number in words using switch cas.
  • Now we will see how to convert number /digits in to words in C programming.
  • Get the input from the user using Scanf() function.
  • Using while loop and % operator and get the each number and use switch case to print corresponding word for the current number.



Program #1: write a program in c that reads the digits and then converts them into words

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.  int number,i=0,j,digit;
  6.     char * word[1000];
  7.  
  8.     printf("Enter any integer to print that in words: ");
  9.     scanf("%d",&number);
  10.  
  11.     while(number){
  12.  
  13.     digit = number %10;
  14.     number = number /10;
  15.  
  16.          switch(digit){
  17.              case 0: word[i++] = "zero"; break;
  18.              case 1: word[i++] = "one"; break;
  19.              case 2: word[i++] = "two"; break;
  20.              case 3: word[i++] = "three"; break;
  21.              case 4: word[i++] = "four"; break;
  22.              case 5: word[i++] = "five"; break;
  23.              case 6: word[i++] = "six"; break;
  24.              case 7: word[i++] = "seven"; break;
  25.              case 8: word[i++] = "eight"; break;
  26.              case 9: word[i++] = "nine"; break;
  27.  
  28.         }
  29.     }
  30.    
  31.     for(j=i-1;j>=0;j--){
  32.          printf("%s ",word[j]);
  33.     }
  34.  return 0;
  35. }
  
Output:


print numbers in words c program

C program for swapping of two strings

  • C program for swapping of two strings.
  • C program to swap two strings without using library functions.
  • Now we will swap two strings without using library functions and by using arrays in java.
  • Now we will write a C program on how to swap two strings.



Program #1 Write a C program to swap two strings


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.  int i=0,j=0,k=0;
  6.   char str1[20],str2[20],temp[20];
  7.   puts("Enter first string");
  8.   gets(str1);
  9.   puts("Enter second string");
  10.   gets(str2);
  11.   printf("Before swapping the strings \n");
  12.   puts(str1);
  13.   puts(str2);
  14.   while(str1[i]!='\0'){
  15.              temp[j++]=str1[i++];
  16.   }
  17.   temp[j]='\0';
  18.   i=0,j=0;
  19.   while(str2[i]!='\0'){
  20.               str1[j++]=str2[i++];
  21.   }
  22.   str1[j]='\0';
  23.   i=0,j=0;
  24.   while(temp[i]!='\0'){
  25.               str2[j++]=temp[i++];
  26.   }
  27.   str2[j]='\0';
  28.   printf("After swapping the strings are\n");
  29.   puts(str1);
  30.   puts(str2);
  31.   getch();
  32. }

  
Output:


swap strings in c program

Write a C program to swap two integer arrays

  • C program to interchange the elements of an array.
  • Now we will see how to swap two integer arrays in C programming by using third array.



Program #1: Write a C program to swap two integer arrays

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a[5],b[5],c[5],i;
  7.   printf("Enter First array 5 elemnets \n");
  8.   for(i=0;i<5;i++)
  9.   scanf("%d",&a[i]);
  10.   printf("\nEnter Second array 5 elements\n");
  11.   for(i=0;i<5;i++)
  12.             scanf("%d",&b[i]);
  13.   printf("Arrays before swapping\n");
  14.   printf("First array \n");
  15.   for(i=0;i<5;i++){
  16.             printf("%d ",a[i]);
  17.   }
  18.   printf("\nSecond array\n");
  19.   for(i=0;i<5;i++){
  20.             printf("%d ",b[i]);
  21.   }
  22.   for(i=0;i<5;i++){
  23.             c[i]=a[i];
  24.             a[i]=b[i];
  25.             b[i]=c[i];
  26.   }
  27.   printf("\nArrays after swapping\n");
  28.   printf("First array elements \n");
  29.   for(i=0;i<5;i++){
  30.             printf("%d ",a[i]);
  31.   }
  32.   printf("\nSecond array elements \n");
  33.   for(i=0;i<5;i++){
  34.             printf("%d ",b[i]);
  35.   }
  36.  return 0;
  37. }

Output:


swap arrays in c


C Program to swap two numbers without using third variable

  • C program to swap two numbers without using third variable.
  • Yes we can swap two variables without using third variable.
  • We have four different ways to swap two numbers without using third variable.
  • Lets see an example C program on how to swap two numbers without using third variable and without using function.



Program #1: Write a simple C program to swap two numbers without using third variable

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   
  13.     a=b+a;
  14.     b=a-b;
  15.     a=a-b;
  16.     
  17.   printf("\nafter swapping\n");
  18.   printf("a=%d\n",a); 
  19.   printf("b=%d",b); 
  20.   getch();
  21.   
  22. }

Output:


swap without third variable c program



Program #2: Write a simple C program to swap two numbers without using third variable


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.  
  13.   a=a+b-(b=a); 
  14.     
  15.   printf("\nafter swapping\n");
  16.   printf("a=%d\n",a); 
  17.   printf("b=%d",b); 
  18.   getch();
  19.   
  20. }

Output:

  1. Please enter two integers numbers to swap
  2. 30
  3. 40
  4. before swapping
  5. a=30
  6. b=40
  7. after swapping
  8. a=40
  9. b=30

Program #3: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.     a=a^b;
  8.     b=a^b;
  9.     a=b^a;
  10.    printf("a=%d\n",a); 
  11.  printf("b=%d",b); 
  12.   getch();
  13.   
  14. }

  Output:

  1. a=13
  2. b=12

Program #4: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.      a=b-~a-1;
  8.     b=a+~b+1;
  9.      a=a+~b+1;
  10.    printf("a=%d\n",a); 
  11.    printf("b=%d",b); 
  12.  
  13.  return 0;
  14.   
  15. }

  Output:

  1. a=13
  2. b=12

C program to swap two numbers without using third variable and using functions

  • C program to swap two numbers using functions temporary variable.
  • We can swap two numbers by using temporary variable.
  •  Lets see an example C program to swap two numbers without using any function.
  • C program to swap two numbers using third variable


Program #1: Write a C program to swap two numbers using temporary variable without using any function and by using third variable.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b, temp;
  7.   printf("Please enter two integer number to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   temp=a;
  13.   a=b;
  14.   b=temp;
  15.   printf("\nafter swapping\n");
  16.   printf("a=%d\n",a); 
  17.   printf("b=%d",b);        
  18.   
  19.  return 0;
  20.   
  21. }
     

 Output:


swap numbers in c.png


Program #2: Write a C program to swap two numbers using temporary variable with using  function and using third variable.

  • C program to swap two numbers using functions call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void swap(int a, int b);
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   
  13.   swap(a,b);   
  14.     
  15.   return
  16.   
  17. }
  18.  
  19. void swap(int a, int b){
  20.     
  21.   int temp=a;
  22.   a=b;
  23.   b=temp;
  24.   printf("\nafter swapping\n");
  25.   printf("a=%d\n",a); 
  26.   printf("b=%d",b); 
  27.     
  28. }

Output:

  1. Please enter two integer numbers to swap
  2. 10
  3. 20
  4. before swapping
  5. a=10
  6. b=20
  7. after swapping
  8. a=20
  9. b=10

C program to print patterns of numbers,alphabets and stars in a pyramid shape

  • We can print *'s  in pyramid pattern using C programming language.
  • We can also print  patterns of numbers and alphabets using C program.
  • Now Check below program that how to print pyramid pattern of * using C program using for loop.
  • C program to print patterns of numbers and stars in a pyramid shape



Program #1 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows: \n");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("* ");
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    getch();
  21.     
  22. }
   
Output:


print pyramid pattern c program

 Program #2 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows to print pyramid numbers pattern \n ");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("%d ",j);
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    return 0;
  21.     
  22. }

Output:

  1. Enter number of rows to print pyramid numbers pattern
  2.  10
  3. 1
  4. 1 2
  5. 1 2 3
  6. 1 2 3 4
  7. 1 2 3 4 5
  8. 1 2 3 4 5 6
  9. 1 2 3 4 5 6 7
  10. 1 2 3 4 5 6 7 8
  11. 1 2 3 4 5 6 7 8 9
  12. 1 2 3 4 5 6 7 8 9 10
  
Program #3 : Write a c program to print patterns of numbers and alphabets

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j;
  7.     char input, alphabet = 'A';
  8.  
  9.     printf("Enter the uppercase character you want to print in last row: ");
  10.     scanf("%c",&input);
  11.  
  12.     for(i=1; i <= (input-'A'+1); ++i)
  13.     {
  14.         for(j=1;j<=i;++j)
  15.         {
  16.             printf("%c", alphabet);
  17.         }
  18.         ++alphabet;
  19.  
  20.         printf("\n");
  21.     }
  22.     
  23.     getch();
  24.     
  25. }

Output:

  1. Enter the uppercase character you want to print in last row:
  2.  I
  3. A
  4. BB
  5. CCC
  6. DDDD
  7. EEEEE
  8. FFFFFF
  9. GGGGGGG
  10. HHHHHHHH
  11. IIIIIIIII
Select Menu