C Programs for you – Set 1

C Programs for you – Set 1

C Programs are being asked in various tests now and you should definitely know the most common ones. Go through the programs below, understand the logic and then try to replicate the code.

rn

Q1. Write a program to find factorial of the given number.

rn

Answer:

rn

#include <stdio.h>

rn

int main()

rn

{

rn

    int n, i;

rn

    unsigned long long factorial = 1;

rn

    printf(“Enter an integer: “);

rn

    scanf(“%d”,&n);

rn

    // show error if the user enters a negative integer

rn

    if (n < 0)

rn

        printf(“Error! Factorial of a negative number doesn’t exist.”);

rn

    else

rn

    {

rn

        for(i=1; i<=n; ++i)

rn

        {

rn

            factorial *= i;              // factorial = factorial*i;

rn

        }

rn

        printf(“Factorial of %d = %llu”, n, factorial);

rn

    }

rn

    return 0;

rn

}

rn

Q2. Write a program to check whether the given number is even or odd.

rn

Answer:

rn

#include <stdio.h>

rn

int main()

rn

{

rn

    int number;

rn

    printf(“Enter an integer: “);

rn

    scanf(“%d”, &number);

rn

    // True if the number is perfectly divisible by 2

rn

    if(number % 2 == 0)

rn

        printf(“%d is even.”, number);

rn

    else

rn

        printf(“%d is odd.”, number);

rn

    return 0;

rn

}

rn

Q3. Write a program to swap two numbers using a temporary variable.

rn

Answer:

rn

#include <stdio.h>

rn

int main()

rn

{

rn

      double firstNumber, secondNumber, temporaryVariable;

rn

      printf(“Enter first number: “);

rn

      scanf(“%lf”, &firstNumber);

rn

      printf(“Enter second number: “);

rn

      scanf(“%lf”,&secondNumber);

rn

      // Value of firstNumber is assigned to temporaryVariable

rn

      temporaryVariable = firstNumber;

rn

      // Value of secondNumber is assigned to firstNumber

rn

      firstNumber = secondNumber;

rn

      // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber

rn

      secondNumber = temporaryVariable;

rn

      printf(“nAfter swapping, firstNumber = %.2lfn”, firstNumber);

rn

      printf(“After swapping, secondNumber = %.2lf”, secondNumber);

rn

      return 0;

rn

}

rn

Q4. Write a program to swap two numbers without using a temporary variable.

rn

Answer:

rn

/*

rn

 * C++ program to swap two numbers without using a temporary variable

rn

 */

rn

#include<iostream>

rn

using namespace std;

rn

 /* Function for swapping the values */

rn

void swap(int &a, int &b)

rn

{

rn

    b = a + b;

rn

    a = b – a;

rn

    b = b – a;

rn

}

rn

 int main()

rn

{

rn

    int a, b;

rn

     cout << “Enter two numbers to be swapped : “;

rn

    cin >> a >> b;

rn

    swap(a, b);

rn

    cout << “The two numbers after swapping become :” << endl;

rn

    cout << “Value of a : ” << a << endl;

rn

    cout << “Value of b : ” << b << endl;

rn

}

rn

Q5. Write a program to swap two numbers using bitwise operators.

rn

Answer:

rn

/*

rn

 * C Program to swap two Numbers using Bitwise operators

rn

 */

rn

#include <stdio.h>

rn

#include <string.h>

rn

 

rn

/* Function Prototype */

rn

void swap(int*, int *);

rn

 void main()

rn

{

rn

    int num1, num2;

rn

    printf(“nEnter two numbers:”);

rn

    scanf(“%d %d”, &num1, &num2);

rn

    printf(“nThe numbers before swapping are Number1= %d Number2 = %d”, num1, num2);

rn

    swap(&num1, &num2);        /* Call by Reference to function swap */

rn

    printf(“nThe numbers after swapping are Number1= %d Number2 = %d”, num1, num2);

rn

}

rn

 /* Code to swap two numbers using bitwise operator */

rn

void swap(int *x, int *y)

rn

{

rn

    *x = *x ^ *y;

rn

    *y = *x ^ *y;

rn

    *x = *x ^ *y;

rn

}

rn

Let us know if you found it useful and we will post more!

c