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.
rnQ1. Write a program to find factorial of the given number.
rnAnswer:
rn#include <stdio.h>
rnint main()
rn{
rnint n, i;
rnunsigned long long factorial = 1;
rnprintf(“Enter an integer: “);
rnscanf(“%d”,&n);
rn// show error if the user enters a negative integer
rnif (n < 0)
rnprintf(“Error! Factorial of a negative number doesn’t exist.”);
rnelse
rn{
rnfor(i=1; i<=n; ++i)
rn{
rnfactorial *= i; // factorial = factorial*i;
rn}
rnprintf(“Factorial of %d = %llu”, n, factorial);
rn}
rnreturn 0;
rn}
rnQ2. Write a program to check whether the given number is even or odd.
rnAnswer:
rn#include <stdio.h>
rnint main()
rn{
rnint number;
rnprintf(“Enter an integer: “);
rnscanf(“%d”, &number);
rn// True if the number is perfectly divisible by 2
rnif(number % 2 == 0)
rnprintf(“%d is even.”, number);
rnelse
rnprintf(“%d is odd.”, number);
rnreturn 0;
rn}
rnQ3. Write a program to swap two numbers using a temporary variable.
rnAnswer:
rn#include <stdio.h>
rnint main()
rn{
rndouble firstNumber, secondNumber, temporaryVariable;
rnprintf(“Enter first number: “);
rnscanf(“%lf”, &firstNumber);
rnprintf(“Enter second number: “);
rnscanf(“%lf”,&secondNumber);
rn// Value of firstNumber is assigned to temporaryVariable
rntemporaryVariable = firstNumber;
rn// Value of secondNumber is assigned to firstNumber
rnfirstNumber = secondNumber;
rn// Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
rnsecondNumber = temporaryVariable;
rnprintf(“nAfter swapping, firstNumber = %.2lfn”, firstNumber);
rnprintf(“After swapping, secondNumber = %.2lf”, secondNumber);
rnreturn 0;
rn}
rnQ4. Write a program to swap two numbers without using a temporary variable.
rnAnswer:
rn/*
rn* C++ program to swap two numbers without using a temporary variable
rn*/
rn#include<iostream>
rnusing namespace std;
rn/* Function for swapping the values */
rnvoid swap(int &a, int &b)
rn{
rnb = a + b;
rna = b – a;
rnb = b – a;
rn}
rnint main()
rn{
rnint a, b;
rncout << “Enter two numbers to be swapped : “;
rncin >> a >> b;
rnswap(a, b);
rncout << “The two numbers after swapping become :” << endl;
rncout << “Value of a : ” << a << endl;
rncout << “Value of b : ” << b << endl;
rn}
rnQ5. Write a program to swap two numbers using bitwise operators.
rnAnswer:
rn/*
rn* C Program to swap two Numbers using Bitwise operators
rn*/
rn#include <stdio.h>
rn#include <string.h>
rnrn
/* Function Prototype */
rnvoid swap(int*, int *);
rnvoid main()
rn{
rnint num1, num2;
rnprintf(“nEnter two numbers:”);
rnscanf(“%d %d”, &num1, &num2);
rnprintf(“nThe numbers before swapping are Number1= %d Number2 = %d”, num1, num2);
rnswap(&num1, &num2); /* Call by Reference to function swap */
rnprintf(“nThe numbers after swapping are Number1= %d Number2 = %d”, num1, num2);
rn}
rn/* Code to swap two numbers using bitwise operator */
rnvoid swap(int *x, int *y)
rn{
rn*x = *x ^ *y;
rn*y = *x ^ *y;
rn*x = *x ^ *y;
rn}
rnLet us know if you found it useful and we will post more!