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

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

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

C program to find leap year using conditional operator

  • We have already seen how to check leap year or not using C program here
  • Now we need to find out a leap year by using conditional operator using C programming language.
  • expression ? value1 : value2
  • if expression is true then it picks value1 else value2.
  • Let us see an example C program to check a year is leap year or not by using conditional operator.



Program #1: Write a C program to find leap year using conditional operator without using any function and loops.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.     int year;
  7.     printf("Enter a year to check if it is a leap year or not \n");
  8.     scanf("%d", &year);
  9.  
  10.    
  11.     (year%4==0 && year%100!=0) ? printf("Leap Year") :
  12.         (year%400 ==0 ) ? printf("Leap Year") : printf("not a leap year");
  13.         
  14.    getch();
  15.     
  16. }

Output:


  1. Enter a year to check if it is a leap year or not
  2. 2012
  3. Leap Year


leap year in c conditional operator

this program prompts the user to enter a year and then uses the conditional operator to check if the year is a leap year. The condition being checked is whether the year is evenly divisible by 4 and not divisible by 100, or if it is evenly divisible by 400. If the year is a leap year, the program will print "year is a leap year.", otherwise it will print "year is not a leap year."

The conditional operator is represented by the ? symbol. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.

In this example, the condition is (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0), the first expression to execute if the condition is true is printf("%d is a leap year.\n", year) and the second expression if the condition is false is printf("%d is not a leap year.\n", year)

It's important to note that this program doesn't take into account the Julian calendar and leap year rules before the Gregorian calendar.

In summary, this is a simple C program that uses the conditional operator to check if a year is a leap year, by evaluating a condition that checks if the year is evenly divisible by 4 and not divisible by 100, or if it is evenly divisible by 400. The program uses the ternary operator to make the code more concise and readable.

C program to find leap year using if else

  • A year is said to be leap year if it is divisible by 4. 
  • For example 2004 , 2008,2012 and 2016.
  • A year is also a leap year if it is divisible by 100 and  divisible by 400.
  • For example 1600 and 2000 etc.
  • A leap year of February moth will have 29 days.
  • Let us see an example C program to check a year is leap year or not using simple if condition.
  • First we need to check a year is divisible by 4 or not.
  • If yes then again one more condition if it is divisible by 100 and 400. if it is divisible by 100 then it should be divisible by 400 then only it is leap year.
  • c program code to find leap year using if else


Program #1: Write a C program to check a year is leap year or not 


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // Function to check if a year is a leap year

  4. int isLeapYear(int year) {
  5.     // Logic for leap year:
  6.     // A year is a leap year if:
  7.     // 1. It is divisible by 4
  8.     // 2. If divisible by 100, it must also be divisible by 400

  9.     if (year % 4 == 0) {
  10.         if (year % 100 == 0) {
  11.             if (year % 400 == 0) {
  12.                 // Year is divisible by 400

  13.                 return 1; // It is a leap year
  14.             } else {
  15.                 // Year is divisible by 100 but not by 400

  16.                 return 0; // Not a leap year
  17.             }
  18.         } else {
  19.             // Year is divisible by 4 but not by 100

  20.             return 1; // It is a leap year
  21.         }
  22.     } else {
  23.         // Year is not divisible by 4

  24.         return 0; // Not a leap year
  25.     }
  26. }

  27. int main() {

  28.     // Program to check leap year in C programming
  29.     // Input year and output whether it is a leap year or not

  30.     int year; // Variable to store year input by user

  31.     printf("=== Leap Year Checker in C ===\n\n");
  32.     printf("This program checks if a given year is a leap year or not.\n\n");

  33.     // Prompt user for input
  34.     printf("Enter a year to check: ");
  35.     scanf("%d", &year);

  36.     // Call function to check leap year
  37.     if (isLeapYear(year)) {

  38.         printf("%d is a leap year!\n", year);

  39.     } else {

  40.         printf("%d is not a leap year.\n", year);

  41.     }

  42.     // Demonstrating the leap year logic with examples
  43.     printf("\nExamples of leap year logic:\n");
  44.     printf("- 2000: Divisible by 400, so it is a leap year.\n");
  45.     printf("- 1900: Divisible by 100 but not by 400, so not a leap year.\n");
  46.     printf("- 2016: Divisible by 4 but not by 100, so it is a leap year.\n");
  47.     printf("- 2019: Not divisible by 4, so not a leap year.\n\n");

  48.     // Prompt for additional checks
  49.     char choice;
  50.     do {
  51.         printf("Would you like to check another year? (y/n): ");
  52.         scanf(" %c", &choice);

  53.         if (choice == 'y' || choice == 'Y') {
  54.             printf("\nEnter a year to check: ");
  55.             scanf("%d", &year);

  56.             // Call function again for the new input
  57.             if (isLeapYear(year)) {
  58.                 printf("%d is a leap year!\n", year);
  59.             } else {
  60.                 printf("%d is not a leap year.\n", year);
  61.             }
  62.         }
  63.     } while (choice == 'y' || choice == 'Y');

  64.     printf("\nThank you for using the Leap Year Checker in C program!\n");

  65.     return 0;
  66. }

 
Output:

  1. === Leap Year Checker in C ===

  2. This program checks if a given year is a leap year or not.

  3. Enter a year to check: 2000
  4. 2000 is a leap year!

  5. Examples of leap year logic:
  6. - 2000: Divisible by 400, so it is a leap year.
  7. - 1900: Divisible by 100 but not by 400, so not a leap year.
  8. - 2016: Divisible by 4 but not by 100, so it is a leap year.
  9. - 2019: Not divisible by 4, so not a leap year.

  10. Would you like to check another year? (y/n): n

  11. Thank you for using the Leap Year Checker in C program!





Program #2: Write a C program to check a year is leap year or not without using any function.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.     int year;
  7.     printf("Enter a year to check if it is a leap year or not \n");
  8.     scanf("%d", &year);
  9.  
  10.     if ( year%400 == 0)      
  11.     printf("%d is a leap year.\n", year);     
  12.     else if ( year%100 == 0)      
  13.     printf("%d is not a leap year.\n", year); 
  14.          
  15.     else if ( year%4 == 0 )      
  16.     printf("%d is a leap year.\n", year);
  17.     
  18.     else      
  19.     printf("%d is not a leap year.\n", year); 
  20.  
  21.   return 0;
  22.     
  23. }


 Output:


leap year in c program


C program to find sum of n numbers using for loop

  • C program to find sum of n numbers using function
  • This is about to get sum of all natural numbers in C using for loop
  • For example 4: 1+2+3+4=11
  • Let us see an example program to get  sum of natural numbers using while loop.
  • c program to find sum of n numbers using for loop



Program #1 :  Write a C program to get sum of all natural numbers up to n using for loop

  1. // www. instanceofjava.com all rights reserved
  2.  #include<stdio.h> 
  3. #include<math.h>
  4.  
  5. int main()
  6. {
  7.     int n, i, sum = 0;
  8.     
  9.     printf("Enter a positive integer: ");
  10.     scanf("%d",&n);
  11.  
  12.     for(i=1; i <= n; ++i)
  13.     {
  14.         sum += i;   // sum = sum+i;
  15.     }
  16.  
  17.     printf("Sum = %d",sum);
  18.  
  19.    return 0;
  20. }

 Output:


sum of all natural numbers in c

In this program, we first declare three variables: n, i, and sum. We use the printf function to prompt the user to enter the value of n, and the scanf function to read the value entered by the user and store it in the n variable.

The for loop iterates from 1 to n, and for each iteration, the current value of i is added to the sum variable. Once the loop completes, the final value of sum is the sum of the first n natural numbers.

The program then prints the final value of sum using the printf function.

You can try running this program and entering different values for n to see how it works.

Note: This program assumes that the input will be valid. You may want to include error check for non-integer input or negative number input.

C program to reverse a number using while loop and for loop

  • Finding reverse number means for example take 123 then we need to get 321 is its reverse number.
  • In c programming we can do it by using for loop and while loop.
  • Lets us see an example C program to get reverse number of a number by using while loop and for loop.
  • while(n != 0)
  •    {
  •         rem = n%10;
  •         reverse_Number = reverse_Number*10 + rem;
  •         n /= 10;
  •     }



Program #1 : Write a c program to reverse  number using for loop.


  1. #include <stdio.h>
  2. // www. instanceofjava.com all rights reserved
  3. int main()
  4. {
  5.     int n, reverse_Number = 0, rem,Original_number=0;
  6.  
  7.     printf("Enter a number to get reverse number ");
  8.     scanf("%d", &n);
  9.  Original_number=n;
  10.     while(n != 0)
  11.     {
  12.         rem = n%10;
  13.         reverse_Number = reverse_Number*10 + rem;
  14.         n /= 10;
  15.     }
  16.  
  17.     printf("Reversed Number of %d is = %d",Original_number=0;,reverse_Number);
  18.     getch();
  19. }

 Output:


reverse number in c program


Program #2 :  Write a C program to reverse a number using for loop
 

  1. #include <stdio.h>
  2. // www. instanceofjava.com all rights reserved
  3. int main()
  4. {
  5.     int n, reverse_Number = 0, rem,Original_number=0;
  6.  
  7.     printf("Enter a number to get reverse number ");
  8.     scanf("%d", &n);
  9.     Original_number=n;
  10.     for(;n != 0;)
  11.     {
  12.         rem = n%10;
  13.         reverse_Number = reverse_Number*10 + rem;
  14.         n /= 10;
  15.     }
  16.  
  17.     printf("Reversed Number of %d is = %d",Original_number,reverse_Number);
  18.  
  19.     getch();
  20. }

 Output:

  1. Enter a number to get reverse number
  2. 123987
  3. Reversed Number of 123987 is = 789321 

C program to convert binary to decimal using for loop

  • c program to convert binary to decimal using array and for loop.
  • Lets us see an example program on c to convert binary format number to decimal format.
  • Write a function which accepts a number as binary.
  •       rem = n%10;
  •         n /= 10;
  •         decimal += rem*pow(2,i);
  • This is the logic to get decimal format of the number.




Program #1 : write a c program to convert binary to decimal using while loop function

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     while (n!=0)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.         ++i;
  23.     }
  24.     return decimal;
  25. }

 Output:


Binary to decimal in c program


Program #2 : write a c program to convert binary to decimal using while loop function

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     for (i=0; n!=0;i++)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.       
  23.     }
  24.     return decimal;
  25. }

Output:

  1. Enter a binary number:
  2. 1011
  3. 1011 binary format= 11 decimal format

You Might Like:
  1. Write a c program to generate fibonacci series without recursion  
  2. Write a c program to generate fibonacci series using recursion
  3. Matrix multiplication in c program with explanation
  4. Factorial program in c without recursion
  5. C program to check number is armstrong number or not
  6. Check Armstrong number in c using recursive function  
  7. C program for even or odd using for loop 
  8. C program to check odd or even without using modulus operator and division operator
  9. Prime number program in c using for loop and while loop 
  10. C program to print prime numbers from 1 to n
  11. C program to print multiplication table using while loop and for loop
  12. C program to convert binary to decimal using for loop 
  13. C program to reverse a number using while loop and for loop
  14. C program to find sum of n numbers using for loop 
  15. C program to find leap year using if else  
  16. c program to check leap year using conditional operator  
  17. C program to print patterns of numbers,alphabets and stars in a pyramid shape
  18. C program to swap two numbers without using third variable and using functions 
  19. C Program to swap two numbers without using third variable 
  20. Write a C program to swap two integer arrays  
  21. C program for swapping of two strings  
  22. C program to print given number in words 
  23. C program to print 1 to 100 without using loop
  24. C program to find ASCII value of a string  
  25. Convert string to integer c programming  
  26. C program to display characters from a to z using loop 
  27. C programming power function example program 
  28. C program for addition subtraction multiplication and division using switch case  
  29. C program to delete an element in an array
  30. C program to insert an element in an array
  31. Switch case in c example program
  32. Prime number program in c using for loop
  33. Fibonacci series in c without recursion

C program to print multiplication table using while loop and for loop

C Program to Print Multiplication Table Using While Loop and For Loop
In the programming world, loops are the most basic. It is only by mastering the rules of a loop, and then replacing them with a loop when necessary, that we can program something that is reproducible. This article explores how to print a multiplication table in C using two types of loops: the `for` loop and the `while` loop. In fact, both approaches are often used—not only in commercial programming languages such as Basic but also in later ones like C. Both are also very simple and straightforward, helping you to consolidate your programming skills.

 What is a Multiplication Table?

A multiplication table is a table used to define the multiplication operation for an algebraic system. Multiplying 5 by 1, 5 by 2, and so on—all ten answers to these multiplication problems make up the multiplication table of 5. One of the simplest forms is the multiplication table of 1—since 1 multiplied by any number always equals that same number. The product of a number and any of its factors is in this way called a "multiple." For example, multiply a circular disk by its diameter to get the circumference; the product of 3.14 and a disk with a diameter of 1 is 3.14.
  • Its is very easy to print multiplication table using c program
  • We need one for loop which iterates from 1 to 10
  • Inside for loop just multiply numbers and print result in each iteration.
  • Lets us see an example C program on how to print multiplication table using for loop.
  • multiplication table program in c using for loop
  • write a c program to input a number from user and print multiplication table of the given number using for loop. how to print multiplication table of a given number in c programming.
Program #1 : Write a c program to print multiplication table using for loop.Take input from user.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i;
  5.  
  6.     printf("Enter a Number ");
  7.     scanf("%d",&n);
  8.  
  9.     for(i=1; i<=10; ++i)
  10.     {
  11.         printf("%d * %d = %d \n", n, i, n*i);
  12.     }
  13.      
  14.     getch();
  15.     
  16. }
  
Output:


multiplication table in c program

Program #2: Write a c program to print multiplication table using while loop
write a c program to print multiplication table of any number using for loop


  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i;
  5.  
  6.     printf("Enter a Number ");
  7.     scanf("%d",&n);
  8.     i=1;
  9.     while(i<=10){
  10.                 
  11.         printf("%d * %d = %d \n", n, i, n*i);
  12.         ++i;
  13.     }
  14.      
  15.     getch();
  16.     
  17. }
  
Output:

  1. Enter a Number 
  2. 2
  3. 2 * 1 = 2
  4. 2 * 2 = 4
  5. 2 * 3 = 6
  6. 2 * 4 = 8
  7. 2 * 5 = 10
  8. 2 * 6 = 12
  9. 2 * 7 = 14
  10. 2 * 8 = 16
  11. 2 * 9 = 18
  12. 2 * 10 = 20
     

Using a `while` Loop to Print a Multiplication Table

The `while` loop is a control flow statement that allows code to be executed repeatedly based on a given condition. If you want to print the multiplication table of a given number using a `while` loop, here is how you can do it in C:

  1. #include <stdio.h>

  2. int main() {
  3.     int number, i = 1;

  4.     // Input the number for which you want to print the multiplication table
  5.     printf("Enter a number: ");
  6.     scanf("%d", &number);

  7.     // Use a while loop to print the multiplication table
  8.     printf("Multiplication Table of %d using while loop:\n", number);
  9.     while (i <= 10) {
  10.         printf("%d x %d = %d\n", number, i, number * i);
  11.         i++;
  12.     }

  13.     return 0;
  14. }

Explanation:
  • Input the Number: The program first asks the user to input a number for which the multiplication table will be generated.
  • Initialize `i`: The variable `i` is initialized to 1, which will act as the multiplicand.
  • While Loop Condition: As long as `i` is less than or equal to 10, the loop continues.
  • Print the Result: Within the loop, the program prints the result of `number * i`.
  • increment `i`: After each iteration, `i` is incremented by 1 to prepare for the next run.

Output Example:

  1. Enter a number: 7
  2. Multiplication Table of 7 using while loop:
  3. 7 x 1 = 7
  4. 7 x 2 = 14
  5. 7 x 3 = 21
  6. 7 x 4 = 28
  7. 7 x 5 = 35
  8. 7 x 6 = 42
  9. 7 x 7 = 49
  10. 7 x 8 = 56
  11. 7 x 9 = 63
  12. 7 x 10 = 70


Using a `for` Loop to Print a Multiplication Table

The `for` loop is another control flow statement providing a compact way to write loops. If you want to print the multiplication table of a number using a `for` loop, here is how you can do it in C:

  1. #include <stdio.h>

  2. int main() {
  3.     int number;

  4.     // Input the number for which you want to print the multiplication table
  5.     printf("Enter a number: ");
  6.     scanf("%d", &number);

  7.     // Use a for loop to print the multiplication table
  8.     printf("Multiplication Table of %d using for loop:\n", number);
  9.     for (int i = 1; i <= 10; i++) {
  10.         printf("%d x %d = %d\n", number, i, number * i);
  11.     }

  12.     return 0;
  13. }


Explanation:
1. input the Number: Just as in the example with the `while` loop, this program first asks the user to input a number.
2. For Loop Initialization: The `for` loop initializes `i` to 1, checks the condition `i <= 10`, and increments `i` on each iteration.
3. Print the Result: The program prints the result of `number * i` inside the loop.


Output:

  1. Enter a number: 7
  2. Multiplication Table of 7 using for loop:
  3. 7 x 1 = 7
  4. 7 x 2 = 14
  5. 7 x 3 = 21
  6. 7 x 4 = 28
  7. 7 x 5 = 35
  8. 7 x 6 = 42
  9. 7 x 7 = 49
  10. 7 x 8 = 56
  11. 7 x 9 = 63
  12. 7 x 10 = 70

Key Differences Between a `while` Loop and a `for` Loop

Syntax: The syntax of the `for` loop is more succinct than that of the `while` loop. With the `for` loop, we can combine initialization, condition testing, and incrementation all into one line; whereas, in the `while` loop, these steps must be executed separately.
When to Use: Use a `while` loop when you don't know how many times the statement execution will be repeated. Use a `for` loop when you know the number of iterations in advance.


Both loops are powerful tools in the C language for handling repetitive work. By understanding how to use them to print a multiplication table, you can apply these methods to solve more complex problems in the future. Whether you choose a `while` loop or a `for` loop totally depends on your actual use case and coding style. I hope you have fun playing around with the code to understand the range of the multiplication table generated, or even combining the two loops under one program to enhance your understanding of them all. Happy programming!

You Might Like:

  1. Write a c program to generate fibonacci series without recursion  
  2. Write a c program to generate fibonacci series using recursion
  3. Matrix multiplication in c program with explanation
  4. Factorial program in c without recursion
  5. C program to check number is armstrong number or not
  6. Check Armstrong number in c using recursive function  
  7. C program for even or odd using for loop 
  8. C program to check odd or even without using modulus operator and division operator
  9. Prime number program in c using for loop and while loop 
  10. C program to print prime numbers from 1 to n
  11. C program to print multiplication table using while loop and for loop
  12. C program to convert binary to decimal using for loop 
  13. C program to reverse a number using while loop and for loop
  14. C program to find sum of n numbers using for loop 
  15. C program to find leap year using if else  
  16. c program to check leap year using conditional operator  
  17. C program to print patterns of numbers,alphabets and stars in a pyramid shape
  18. C program to swap two numbers without using third variable and using functions 
  19. C Program to swap two numbers without using third variable 
  20. Write a C program to swap two integer arrays  
  21. C program for swapping of two strings  
  22. C program to print given number in words 
  23. C program to print 1 to 100 without using loop
  24. C program to find ASCII value of a string  
  25. Convert string to integer c programming  
  26. C program to display characters from a to z using loop 
  27. C programming power function example program 
  28. C program for addition subtraction multiplication and division using switch case  
  29. C program to delete an element in an array
  30. C program to insert an element in an array
  31. Switch case in c example program
  32. Prime number program in c using for loop
  33. Fibonacci series in c without recursion

Select Menu