C program to display characters from a to z using loop

  • C program to display characters from a to z using loop.
  • We can print all alphabets starting from A to Z using for loop.
  • Take a char variable and using for loop iterate it from A to Z.
  • Let us see an Example C program to print A to Z using for loop.



Program #1: Write a C program to print or display characters from A to Z using for loop.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char c;
  5.  
  6.     for(c = 'A'; c <= 'Z'; ++c)
  7.       printf("%c ", c);
  8.     
  9.     return 0;
  10. }

   Output:

  1. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Program #2: Write a C Program to Display English Alphabets in Uppercase and Lowercase

  1. #include <stdio.h>
  2. int main()
  3. {
  4.      char c;
  5.  
  6.     printf("Enter u to display alphabets in uppercase. And enter l to display alphabets in
  7. lowercase: ");
  8.     scanf("%c", &c);
  9.  
  10.     if(c== 'U' || c== 'u')
  11.     {
  12.        for(c = 'A'; c <= 'Z'; ++c)
  13.          printf("%c ", c);
  14.     }
  15.     else if (c == 'L' || c == 'l')
  16.     {
  17.         for(c = 'a'; c <= 'z'; ++c)
  18.          printf("%c ", c);
  19.     }
  20.     else
  21.        printf("Error! You entered invalid character.");
  22.     getch();
  23. }
     

  Output:

print a to z c program

Convert string to integer c programming

  • We can convert string to integer in c Programming by using atoi() function.
  • atoi() is a library function from C. 
  • int atoi(const char *str) ;
  • Now we will write a C program to convert String format number to integer.
  • Read input from user as string.
  • Convert string to integer using atoi() function.

Program #1: Write a C program to convert String to integer in c Programming.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main() {
  5.    int number;
  6.    char number_string[3];
  7.  
  8.    printf("Please Enter a number : ");
  9.    scanf("%s", number_string);
  10.  
  11.    number = atoi(number_string);
  12.    printf("\n number_string : %d", number);
  13.  
  14.    getch();
  15. }


 Output:


convert String to int in c program

Bubble sort algorithm in c with explanation

  • Bubble sort is one of the example of sorting algorithm.
  • Bubble sort in data structure
  • Bubble sort is a procedure of sorting elements in ascending or descending order.
  • If we want to sort an array of numbers [4,2,3,1]  using bubble sort it will return [1,2,3,4] after sorting in ascending order.


 Bubble sort in c with explanation

  • Bubble sort algorithm of ascending order will move higher valued elements to right and lower value elements to left.
  • For this we need to compare each adjacent elements in an array and check they are in order or not if not swap those two elements.
  • After completion of first iteration highest value in the array will be moved to last or final position.
  • In the worst case all the highest elements in array like [9 8 7 5 4] completely in reverse order so we need to iterate all of the elements in N times.
  • Repeat this process N- 1 time in worst case to sort set of elements.
  • Worse case complexity : O(n2
  • In the best case scenario all the elements are already in sorted order so one iteration with no swaps required.
  • Best case complexity: O(n)
  • Let us see the graphical representation of explanation of bubble sort algorithm in c programming language.

Bubble sort algorithm with example diagram

bubble sort algorithm in c

Bubble sort algorithm pseudocode:

Program #1 : Write a C program to sort list of elements using bubble sort algorithm.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Bubble sort algorithm in c
  4. //www.instanceofjava.com
  5. int main(int argc, char *argv[])
  6. {
  7.    int array[100], n,i,j,k, swap;
  8.  
  9.   printf("Enter number of elements to sort\n");
  10.   scanf("%d", &n);
  11.  
  12.   printf("Enter %d elements \n", n);
  13.   for (i = 0; i< n; i++)
  14.     scanf("%d", &array[i]);
  15.  
  16.   printf("\nBefore sorting \n ");
  17.    for (k = 0; k < n; k++) {
  18.       printf("%5d", array[k]);
  19.    }
  20.  
  21.    for (i = 1; i < n; i++) {
  22.       for (j = 0; j < n - 1; j++) {
  23.          if (array[j] > array[j + 1]) {
  24.             swap = array[j];
  25.             array[j] = array[j + 1];
  26.             array[j + 1] = swap;
  27.          }
  28.       }
  29.       
  30.       }
  31.  
  32.  printf("\nAfter Bubble sort elements are:\n");
  33.    for (k = 0; k < n; k++) {
  34.       printf("%5d", array[k]);
  35.    }
  36.  
  37.   return 0;
  38. }

 Output:


bubble sort algorithm in c program recursive

  • Now we will see program on recursive bubble sort algorithm in c
  • Bubble sort using recursive function in c

Program #2: Write a c program for bubble sort using recursion


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Bubble sort algorithm in c
  4. //www.instanceofjava.com
  5. void bubblesort(int array[], int n);
  6. int main(int argc, char *argv[])
  7. {
  8.    int array[10], n,i,k;
  9.  
  10.   printf("Enter number of elements to sort\n");
  11.   scanf("%d", &n);
  12.  
  13.   printf("Enter %d elements \n", n);
  14.   for (i = 0; i< n; i++)
  15.     scanf("%d", &array[i]);
  16.  
  17.   printf("\nBefore sorting \n ");
  18.    for (k = 0; k < n; k++) {
  19.       printf("%5d", array[k]);
  20.    }
  21.  bubblesort(array,n);
  22.    
  23.   getch();
  24. }
  25.  
  26. void bubblesort(int array[], int n){
  27.       int  i,j,k, swap;
  28.   for (i = 1; i < n; i++) {
  29.       for (j = 0; j < n - 1; j++) {
  30.          if (array[j] > array[j + 1]) {
  31.             swap = array[j];
  32.             array[j] = array[j + 1];
  33.             array[j + 1] = swap;
  34.          }
  35.       }
  36.       
  37.       }
  38.  
  39.  printf("\nAfter Bubble sort elements are:\n");
  40.    for (k = 0; k < n; k++) {
  41.       printf("%5d", array[k]);
  42.    }    
  43. }

Output:

  1. Enter number of elements to sort
  2. 6
  3. Enter 6 elements
  4. 9 8 2 3 5 1
  5.  
  6. Before sorting
  7.      9    8    2    3    5    1
  8. After Bubble sort elements are:
  9.     1    2    3    5    8    9

C program to find ASCII value of a string or character

  • Convert string to ascii c programming.
  • C program to find ascii value of a string or character.
  • C program to print ascii value of alphabets.
  • Now we will write a C program to print ASCII value of each character of a String.



Program #1: Write a C programming code to print ASCII value of String.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     char str[100];
  7.     int i=0;
  8.     printf("Enter any string to get ASCII Value of each Character \n");
  9.     scanf("%s",str);
  10.  
  11.     printf("ASCII values of each characters of given string:\n ");
  12.     while(str[i])
  13.          printf("%d \n",str[i++]);
  14.         
  15.     getch();
  16. }


Output:


Print Ascii value of string character c program

C program to print 1 to 100 without using loop

  • Print 1 to 100 without using loop in c
  • We print 1 to n numbers without using any loop.
  • By using recursive function in C Programming we can print 1 to 100 or 1 to n numbers.
  • Now We will write a C program to print one to 100 without using any loops.
  • Print 1 to 100 without loop and recursion



 Program #1: Write a C program to print 1 to 100 without loop and using recursive function.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int print(int num);
  4. int main(int argc, char *argv[])
  5. {
  6.     int number = 1;
  7.  
  8.     print(number);
  9.  
  10.    return 0;
  11. }
  12. int print(int number){
  13.     if(number<=100){
  14.          printf("%d ",number);
  15.          print(number+1);
  16.     }
  17. }
   
Output:


print 1 to 100 in c

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
Select Menu