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


Select Menu