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

Select Menu