C initialize an array

  • Array is collection of similar data items.
  • An array can hold multiple values of same type of data
  • In c programming language an array can be declared as datatype arrayName [ arraySize ]; 
  • In c , while declaring an array we need to specify type of array , name of array and size of the array.
  • One can directly initialize an array while declaring itself with some values
  • int arrayVariable[2]=[10,20];
  • int count[]=[1,2,3,4,5]
  • directly initialize an array of particular : count[3]=10
  • Let us discuss about how to initialize an array with some default values.
  • Using for loop or while loop we can iterate an array and by accessing each index we can assign values 
  • If we are not initializing an array and try to print all the values then it will print some garbage values.
  • c initialize array of ints

#1Write a C program to declare an array and print default values
  1. #include <stdio.h>
  2. // write a c program to explain how to initialize an array
  3. int main () {

  4.    int number[ 15 ]; 
  5.    int i,k;
  6.    
  7.    
  8.    for (k = 0; k < 15; k++ ) {
  9.       printf("Array[%d] = %d\n", k, number[k] );
  10.    }
  11.  
  12.   getch();
  13. }


Output:
  1. Array[0] = -1551778920
  2. Array[1] = -2
  3. Array[2] = 1971427546
  4. Array[3] = 1971495162
  5. Array[4] = 5189464
  6. Array[5] = 5181048
  7. Array[6] = 2686776
  8. Array[7] = 1971494685
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 2686832
  12. Array[11] = 200
  13. Array[12] = 192
  14. Array[13] = 7161720
  15. Array[14] = 48


#2 write a C Program to initialize an array values to 0 (zeros).

initialize an array in c


Output:
  1. Array[0] = 0
  2. Array[1] = 0
  3. Array[2] = 0
  4. Array[3] = 0
  5. Array[4] = 0
  6. Array[5] = 0
  7. Array[6] = 0
  8. Array[7] = 0
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 0
  12. Array[11] = 0
  13. Array[12] = 0
  14. Array[13] = 0
  15. Array[14] = 0

C program to check whether a character is vowel or consonant

  • In this  program we will discuss about how to check whether a character is vowel or consonant.
  • In English language will be having 5 vowels and 21 consonants.
  • To check entered character is vowel or consonant, first Read input character from user using scanf.
  • There are only 5 vowels(A,E,I,O,U), so we need to check entered character is there in those 5 characters.
  • If it is present then we can say its an vowel, otherwise it is a consonant.
  • While checking we need to consider case sensitivity. So we need to check both uppercase vowels and lower case vowels.
  • Lets see an example program to check whether entered character is vowel or not.

check vowel or consonant in c


Write a program to determine whether the input character is a vowel or consonant or not an alphabet

  1. #include <stdio.h>
  2. //write a program to determine whether the input character is a vowel or consonant or not an alphabet
  3. //c program to check whether a character is vowel or consonant
  4. //www.InstanceofJava.com
  5. int main() {
  6.     char character;
  7.     int lowercaseVowel, uppercaseVowel;
  8.     printf("Enter a character  ");
  9.     scanf("%c", &character);

  10.     // assigns 1
  11.     lowercaseVowel = (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u');

  12.     // evaluates to 1 if variable c is a uppercase vowel
  13.     uppercaseVowel = (character == 'A' || character == 'E' || character == 'I' || character == 'O' || character == 'U');

  14.     // evaluates to 1 (true) if c is a vowel
  15.     if (lowercaseVowel || uppercaseVowel)
  16.         printf("%c is a vowel.", character);
  17.     else
  18.         printf("%c is a consonant.", character);
  19.    getch();
  20. }


Output:
  1. Enter a character
  2. I
  3. I is an vowel


Print prime numbers from 1 to 100 in c

  • Lets discuss a program on how to print prime numbers from 1 to 100.
  • Here by default we need to print all prime numbers from 1 to 100.
  • No need to ask user to enter a number because we need to end loop at 100.
  • Using for loop iterate from 1 to 100
  • Take one more loop and check each number is divisible by 1 and itself or not.
  • Program to print prime number between 1 to 100 in c

#1.C program to print prime numbers from 1 to 100

  1. #include <stdio.h>

  2. // c program to print prime numbers from 1 to 100
  3. // www.instanceofjava.com
  4. int main()
  5. {
  6.    int n, i,k, count = 0;
  7.  
  8.  printf("Prime numbers from 1 to 100\n");
  9. for(i=2;i<=100;i++)
  10.  {
  11.   for(k=2;k<i;k++)
  12.   {
  13.    if(i%k==0)
  14.    break;
  15.    else if(i==k+1)
  16.    printf("%d\n",i);
  17. }
  18. }
  19.  
  20.     getch();
  21.     
  22. }
  23.     



prime numbers from 1 to 100



Sum of two integers in c

  •  Check here for printing an integer in c 
  • We have 4 basic data types in c programming language
  • int, char, float and double
  • For storing numbers or integers we will use int data type.
  • Lets see an example program sum of two integers in c programming language.
  • To add two integers we need two int type variables and third variable to store result of sum of two integers.
  • Its a basic level example program for beginners to understand integer data types

sum of two integers in c


Write a C program to add two integers / find sum of two integers in c

  1. #include<stdio.h>
  2. // c program to print integer using %d
  3. // c print integer
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.             //declare an integer variable 
  8. int number1, number2, sumOfTwoIntegers;
  9.             // read input from user using scanf
  10. printf ("Enter any two integers: ");
  11. scanf (" %d%d", &number1,&number2);
  12. //sum of two integers in c
  13. sumOfTwoIntegers=number1+number2;
  14.    //print the same number using %d
  15. printf("Sum of two integers : %d", sumOfTwoIntegers);
  16. getch();
  17. }
Output:
  1. Enter any two integers:
  2. 10
  3. 20
  4. Sum of two integers : 30

Print an integer in c language

  • For declaring numbers we use int data type in c.
  • In order to read using scanf we use %d.
  • using printf we can print same variable. 

print interger in c


#Write a C Program to print an integer 

  1. #include<stdio.h>
  2. // c program to print integer using %d
  3. // c print integer
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.             //declare an integer variable 
  8. int number;
  9.             // read input from user using scanf
  10. printf ("Enter an Integer: ");
  11. scanf (" %d", &number);
  12.    //print the same number using %d
  13. printf("You Entered : %d", number);
  14. getch();
  15. }

Output:
  1. Enter an Integer: 3
  2. You Entered : 3

Sum of even numbers in c

  • Please check this below program which prints even numbers from 1 to n.
  • Print even numbers in c 
  • To print sum of even numbers from 1 to n first we need to get all the even numbers and add them.
  • Using for or while loop iterate from 1 to n number and get all even numbers
  • sum=sum+evenNumber
  • C program to find sum of even numbers using while loop

sum of even numbers in c

C program to find sum of even numbers using while loop

  1. #include<stdio.h> 
  2. //write a program to print even numbers in c using while loop
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i,sum=0;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.      i=1;
  13.        
  14.         while(i<=number){
  15.                 
  16.         if(i%2 ==0){
  17.         
  18.           printf("%d\n" ,i);
  19.            sum=sum+i;    
  20.         }
  21.           i++  ;  
  22.         }
  23.          printf("sum of all even numbers = %d",sum);
  24.          
  25.    getch();
  26. }


Output:
  1. Enter a number:8
  2. 2
  3. 4
  4. 6
  5. 8
  6. sum of all even numbers= 20


Print even numbers in c

  • Lets see how to write a c program to print even numbers using c programming.\
  • Its a basic example to print only even numbers 
  • Use for loop and iterate from 1 to n.
  • In each iteration check if number if divisible by 2 (n%2 == 0). if yes then its a even number otherwise it is a odd number.
  • C program to print even numbers from 1 to n. 
print even numbers in c


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c 
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.   
  13.     for (i=1; i<=number; i++){
  14.         
  15.         if(i%2 ==0){
  16.           printf("%d\n" ,i);
  17.                
  18.         }
  19.               
  20.         }
  21.         
  22.    getch();
  23. }


Output:
  1. Enter a number: 12
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12


#2 print even numbers in c using while loop

  • Now we will try to print even numbers using while loop
  • Only difference is replace for with while loop
  • Variable initialization will be done before loop
  • condition will be inside while(condition)
  • increment or decrement will be last line of while block 
  • Lets print even numbers using while loop from 1 to n.


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c using while loop
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.      i=1;
  13.        
  14.         while(i<=number){
  15.                 
  16.         if(i%2 ==0){
  17.           printf("%d\n" ,i);
  18.                
  19.         }
  20.           i++  ;  
  21.         }
  22.         
  23.    getch();
  24. }


Output:
  1. Enter a number: 20
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12
  8. 14
  9. 16
  10. 18
  11. 20


Sum of digits of a number in c

  • To find sum of digits of a number in c , we need to get individual numbers and add those numbers.
  • First of all read a number from user using scanf.
  • Using while loop get individual number 
  • Divide the number with 10 so we will get last digit of that number as remainder , once we get that last digit then add it to one variable and divide that number with 10 so it will become one digit less.
  • like this we can get all digits and add all those numbers to get sum of digits 
  • Sum of individual digits in c program
  • c program to find sum of digits of a 3 digit number
sum of digits in c

correction: here n =number .

# program to find sum of digits of a number in c

  1. #include<stdio.h> // include stdio.h
  2. //c program to find sum of digits of a number using while loop
  3. //write a program to find sum of digits of a number
  4. //www.instanceofjava.com
  5. int main()
  6. {
  7.     // declare variables 
  8.     int number, remainder, sum = 0;
  9.     //read input from user 
  10.     printf("Enter a number: ");
  11.     scanf("%d", &number);

  12.    //iterate until number not equal to zero
  13.    // get individual digits and add all those digits to get sum
  14.     while(number != 0)
  15.     {
  16.         remainder = number % 10;
  17.         sum += remainder;
  18.         number = number / 10;
  19.     }
  20.   //print sum of digits in c
  21.     printf("sum = %d", sum);

  22.     getch();
  23. }


Output:
  1. Enter a number
  2. 123
  3. sum = 6


C Program to find sum of two numbers

 Lets discuss a basic c program

  • Sum of two integers using c. This might be the first practice program for all users.
  • To check sum of two integers , read input from user and store in two corresponding integer variables.
  • Also take one more variable to store result of sum of two numbers
  • c= a+b
  • Print result using printf

sum of two numbers in c


  1. #include <stdio.h>
  2. // program to check sum of two numbers in c
  3. // write a c program to find sum of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int x, y, sum;
  8.     // read input from user
  9.     printf(" Please enter any two numbers: ");
  10.     scanf("%d %d", &x, &y);
  11.     // sum of two numbers
  12.     sum=x+y;
  13.     
  14.     printf("Sum of %d and %d is %d", x, y, sum);

  15.     getch();
  16. }
Output:

  1. Please enter any two numbers
  2. 10
  3. 20
  4. Sum of 10 and 20 is 30

Gcd of two numbers in c

  • To check GCD of two numbers first we need to read input from user. i.e ask user to enter two numbers
  • Store those two numbers in two integer variables
  • Check if number is factor of given two numbers by iterating in for loop.

gcd of two numbers in c


  1. #include <stdio.h>
  2. // program to check gcd of two numbers in c 
  3. // write a c program to find gcd of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int number1, number2, i, gcd;
  8.     // read input from user
  9.     printf(" please enter any two numbers: ");
  10.     scanf("%d %d", &number1, &number2);

  11.     for(i=1; i <= number1 && i <= number2; ++i)
  12.     {
  13.         // checking  if i is divisible by both numbers / factor of both numbers
  14.         if(number1%i==0 && number2%i==0)
  15.             gcd = i;
  16.     }

  17.     printf("GCD of %d and %d is %d", number1, number2, gcd);

  18.     getch();
  19. }


Output:

gcd of two integers in c




  1. #include <stdio.h>
  2. // Function to compute Greatest Common Divisor using Euclidean Algorithm
  3. int gcd(int a, int b) {
  4.     while (b != 0) {
  5.         int temp = b;
  6.         b = a % b;
  7.         a = temp;
  8.     }
  9.     return a;
  10. }

  11. int main() {
  12.     int num1, num2;

  13.     // enter  two numbers from the user
  14.     printf("Enter two numbers to find their GCD: ");
  15.     scanf("%d %d", &num1, &num2);

  16.     // Ensure numbers are positive
  17.     if (num1 < 0) num1 = -num1;
  18.     if (num2 < 0) num2 = -num2;

  19.     // Calculate and display the GCD
  20.     int result = gcd(num1, num2);
  21.     printf("The GCD of %d and %d is: %d\n", num1, num2, result);

  22.     return 0;
  23. }


Explanation:

  1. Input:
    • The user enters two numbers.
  2. Logic:
    • The findGCD function uses the Euclidean algorithm:
      • Replace aa with bb and bb with a%ba \% b until b=0b = 0.
      • The final value of aa is the GCD.
  3. Output:
    • The program prints the GCD of the two input numbers.



C program to check whether the triangle is right angled triangle

 c program to check whether the triangle is right angled triangle

  • To check right angle triangle or not first we need to check sum of all three angles 
  • If sum of all three angles is 180 then we it should satisfy second condition also
  • Second condition is on of the angle should be 90.
  • if both the conditions are passed then we can say it is a right angle triangle.
  • Now lets write a c program to check triangle is right angle triangle or not.
right angle triangle in c


  1. #include<stdio.h>
  2. // write a c program too check traingle is right angle triasngle or not
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.      int angle1,angle2,angle3;
  7.      printf("Enter Three Angles of Triangle");
  8.      printf("\n-------------------------------\n");
  9.      printf("Enter Angle1  : ");
  10.      scanf("%d", &angle1);
  11.      printf("\nEnter  Angle2 : ");
  12.      scanf("%d",&angle2);
  13.      printf("\nEnter  Angle2  : ");
  14.      scanf("%d",&angle3);
  15.      printf("--------------------------------\n");
  16.      if(angle1+angle2+angle3==180) 
  17.      {
  18.          
  19.           if(angle1==90 || angle2==90 || angle3==90)
  20.           {
  21.                printf("\nTriangle is Right Angle Triangle\n"); //
  22.           }
  23.          
  24.      }
  25.      else
  26.      {
  27.           printf("\nIts Not a Triangle ");
  28.      }
  29.     getch();
  30. }


Write a c program to check a number is palindrome or not:


check palindrome number in c

  • If reverse number of a number is same then we can say its a palindrome number
  • we already know logic to reverse a number using remainder logic
  • read input from the user and store it in a variable
  • check reverse number of given number 
  • compare original number and reverse number if both are same then we can say it's a palindrome number 
  • C Program to Check Whether a Number is Palindrome or Not


Write a c program to check a number is palindrome or not:

  1. #include <stdio.h>
  2. int main() {
  3.     int n, revnumber = 0, ramindernum, number;
  4.     printf("Enter number: ");
  5.     scanf("%d", &n);
  6.     number = n;

  7.     // reversed integer is stored in reversedN
  8.     while (n != 0) {
  9.         ramindernum = n % 10;
  10.         revnumber = revnumber * 10 + ramindernum;
  11.         n /= 10;
  12.     }

  13.     //  if number  and revnumber are same then its a palindrome
  14.     if (number == revnumber)
  15.         printf("%d is a palindrome.", number);
  16.     else
  17.         printf("%d is not a palindrome.", number);

  18.     getch();
  19. }


Output:

palindrome in c



C program to print pyramid pattern of numbers

 #1: C program to print pyramid pattern of n stars

  1. #include <stdio.h>
  2. //c program to print pyramid pattern of numbers
  3. // www.instanceofjava.com
  4. int main(){
  5.    int number,k,m;

  6.     printf("Number of lines of star pattern  ");
  7.     scanf("%d", &number);

  8.     printf("\n");

  9.     for(  k = 1; k <= number; k++ )
  10.     {
  11.         for( m = 1; m <= k; m++)
  12.         {
  13.             printf("*");
  14.         }
  15.         printf("\n");
  16.     }

  17.    getch();
  18. }

Output:
pattern program in c language



#2: C program to print pyramid pattern of n Numbers

  1. #include <stdio.h>
  2. //c program to print pyramid pattern of numbers
  3. // www.instanceofjava.com
  4. //pyramid pattern in c
  5. //write a c
  6.  program to print following pattern
  7. int main(){
  8.    int number,k,m;

  9.     printf("Number of lines of star pattern  ");
  10.     scanf("%d", &number);

  11.     printf("\n");

  12.     for(  k = 1; k <= number; k++ )
  13.     {
  14.         for( m = 1; m <= k; m++)
  15.         {
  16.            printf("%-5d ", m);
  17.         }
  18.         printf("\n");
  19.     }

  20.    getch();
  21. }

Output:

pyramid pattern in c


Switch case in c example program

  • switch case in c programming questions
  • switch case with do you want to continue in c
  • A switch case is a control statement in C that allows you to choose between several different options based on the value of a particular expression. 
  • C has a built-in multi-way decision statement known as a switch. It is a keyword we can select a single option from multiple options. The switch  is having expression; each and every option is known as case and must be followed by colon.
  • Each and every case ends with break statement.
  • The last case is default even that will have break.
  •  The break is a keyword. Each and every case ends with break statement

 SYNTAX:-

 switch(expression)
 {
    case value-1 :block-1 break;
    case value-2 :block-2 break;
    case value-3 :block-3 break;
    .........
    .........
    .........
    .........
    default :default-block break;
 }

Here is an example of how to use a switch case in a C program:

#1; switch case in c programming questions

  1. # include <stdio.h>
  2. void main() {
  3.     char operator;
  4.     double a,b;
  5.     printf("Enter an operator (+, -, *,): ");
  6.     scanf("%c", &operator);
  7.     printf("Enter two numbers ");
  8.     scanf("%lf %lf",&a, &b);
  9.     switch(operator)
  10.     {
  11.         case '+':
  12.             printf("%.1lf + %.1lf = %.1lf",a, b, a + b);
  13.             break;
  14.         case '-':
  15.             printf("%.1lf - %.1lf = %.1lf",a, b, a - b);
  16.             break;
  17.         case '*':
  18.             printf("%.1lf * %.1lf = %.1lf",a, b, a * b);
  19.             break;
  20.         case '/':
  21.             printf("%.1lf / %.1lf = %.1lf",a, b, a / b);
  22.             break;
  23.         default:
  24.             printf("Error! operator is not correct");
  25.     }
  26. }

Output:
  1. Enter an operator (+, -, *,): +
  2. Enter two numbers 12
  3. 12
  4. 12.0 + 12.0 = 24.0
  5. Process returned 18 (0x12)   execution time : 7.392 s
  6. Press any key to continue.



  • In the below  example, the program prompts the user to enter a number and then uses a switch case to determine what to do based on the value of the number. If the user enters 1, the program will print "You entered 1.
  • " If the user enters 2, the program will print "You entered 2," and so on. If the user enters a value that is not covered by any of the cases, the default case will be executed.



C program to insert an element in an array

  • Program to insert an element in an array at a given position
  • C program to insert an element in an array without using function



Program #1: Write a c program to insert an element in array without using function


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int insarray[100], pos, c, n, value;
  7.  
  8.   printf("Enter number of elements of array\n");
  9.    scanf("%d", &n);
  10.  
  11.    printf("Enter all %d elements\n", n);
  12.  
  13.    for (c = 0; c < n; c++)
  14.       scanf("%d", &insarray[c]);
  15.  
  16.    printf("Enter the specific position where you wish to insert an element in array\n");
  17.    scanf("%d", &pos);
  18.  
  19.    printf("Enter the value to insert\n");
  20.    scanf("%d", &value);
  21.  
  22.    for (c = n - 1; c >= pos - 1; c--)
  23.       insarray[c+1] = insarray[c];
  24.  
  25.    insarray[pos-1] = value;
  26.  
  27.    printf("Resultant array is\n");
  28.  
  29.    for (c = 0; c <= n; c++)
  30.       printf("%d\n", insarray[c]);
  31.  
  32. getch();
  33. }
Output:

insert element in array c

C program to delete an element in an array

  • Write a c program to insert and delete an element in array
  • Write a c program to delete an element in an array
  • C Program to Delete the Specified Integer from an Array without using function



Program #1: Write a c program to insert and delete an element in array


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   
  7.      int array[10];
  8.  
  9.     int i, n, position, element, exist = 0;
  10.  
  11.  
  12.     printf("Enter number of elements \n");
  13.  
  14.     scanf("%d", &n);
  15.  
  16.     printf("Enter the elements\n");
  17.  
  18.     for (i = 0; i < n; i++)
  19.  
  20.     {
  21.  
  22.         scanf("%d", &array[i]);
  23.  
  24.     }
  25.  
  26.     printf("Input array elements are\n");
  27.  
  28.     for (i = 0; i < n; i++)
  29.  
  30.     {
  31.  
  32.         printf("%d\n", array[i]);
  33.  
  34.     }
  35.  
  36.     printf("Enter the element to be deleted from array\n");
  37.  
  38.     scanf("%d", &element);
  39.  
  40.     for (i = 0; i < n; i++)
  41.  
  42.     {
  43.  
  44.         if (array[i] == element)
  45.  
  46.         {
  47.             exist = 1;
  48.  
  49.             position = i;
  50.  
  51.             break;
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     if (exist == 1)
  58.  
  59.     {
  60.  
  61.         for (i = position; i <  n - 1; i++)
  62.  
  63.         {
  64.  
  65.             array[i] = array[i + 1];
  66.  
  67.         }
  68.  
  69.         printf("array after deletion  \n");
  70.  
  71.         for (i = 0; i < n - 1; i++)
  72.  
  73.         {
  74.  
  75.             printf("%d\n", array[i]);
  76.  
  77.         }
  78.  
  79.     }
  80.  
  81.     else
  82.  
  83.         printf("Given element %d is not exist in the array \n", element);
  84. getch();
  85. }
Output:


delete array element c

C programming power function example program

  • C program to find power of a number using function.
  • Write a c program to find power of a number using function recursion



 Program #1 : Write a c program to find power of a number without using function recursion

  1.  #include<stdio.h>
  2. int main(){
  3.   int power,num,i=1;
  4.   long int sum=1;
  5.   printf("Enter a number:\n ");
  6.   scanf("%d",&num);
  7.   printf("\nEnter power:\n ");
  8.   scanf("%d",&power);
  9.   while(i<=power){
  10.             sum=sum*num;
  11.             i++;
  12.   }
  13.   printf("\n%d to the power %d is: %ld",num,power,sum);
  14.   getch();
  15. }

  Output:


power of number in c



  Program #2 : Write a c program to find power of a number using function recursion


  1.  #include<stdio.h>
     
  2. void powerofnumber(int power, int num);
  3. int main(){
  4.   int power,num,i=1;
  5.   long int sum=1;
  6.   printf("Enter a number:\n ");
  7.   scanf("%d",&num);
  8.   printf("\nEnter power:\n ");
  9.   scanf("%d",&power);
  10.   powerofnumber(power,num);
  11.   return 0;
  12. }
  13.  
  14. void powerofnumber(int power, int num){
  15.      int i=1,sum=1;
  16.      while(i<=power){
  17.             sum=sum*num;
  18.             i++;
  19.   }
  20.   printf("\n%d to the power %d is: %ld",num,power,sum);
  21.  
  22. }
 

Output:


  1. Enter a number:
  2.  23
  3.  
  4. Enter power:
  5.  3
  6.  
  7. 23 to the power 3 is: 12167

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