Coding Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Write a program in c to calculate area and perimeter of a circle using

function.
#include <stdio.h>

void Area_Perimeter();

int main() {

Area_Perimeter();

void Area_Perimeter() {

/* Variable Declaration. */

float radius, perimeter, area;

float PI=3.14;

/* Taking input of the radius of the circle from the user */

printf("Enter radius of the Circle:\n");

scanf("%f", &radius);

/* Calculating perimeter of the circle */

perimeter = 2 * PI * radius;

printf("Perimeter of the circle: %0.4f\n", perimeter);

/* Calculating area of the circle */

area = PI * radius * radius;

printf("Area of circle: %0.4f\n", area);

}
Write a C program to make a simple calculator that performs arithmetic operations using
functions.

#include <stdio.h>

// Function to perform addition

float add(float a, float b) {

return a + b;

// Function to perform subtraction

float subtract(float a, float b) {

return a - b;

// Function to perform multiplication

float multiply(float a, float b) {

return a * b;

// Function to perform division

float divide(float a, float b) {

if (b != 0) {

return a / b;

} else {

printf("Error: Division by zero is not allowed.\n");

return 0; // Return 0 in case of division by zero

}
int main() {

float num1, num2, result;

char operator;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter second number: ");

scanf("%f", &num2);

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &operator);

switch (operator) {

case '+':

result = add(num1, num2);

break;

case '-':

result = subtract(num1, num2);

break;

case '*':

result = multiply(num1, num2);

break;

case '/':

result = divide(num1, num2);


break;

default:

printf("Error: Invalid operator.\n");

return 1; // Exit with an error code

printf("Result: %0.2f\n", result);

return 0;

}
Write a program in c to swap the values of two variables using call by value.
#include<stdio.h>

void swap (int a, int b) {

int temp;

temp = a;

a = b;

b = temp;

printf("After swapping 1st number is %d and 2nd number is %d", a ,b);

int main(void) {

int first, second;

printf("Enter two numbers : \n");

scanf("%d,%d",&first,&second);

swap(first,second);

//printf("\n After swap function called 1st number is %d and 2nd number is %d", first ,second);

return 0;

}
Write a program in c to calculate factorial of a number using recursion.

#include<stdio.h>

long factorial(int n)

if (n == 0)

return 1;

else

return(n * factorial(n-1));

void main()

int number;

long fact;

printf("Enter a number: ");

scanf("%d", &number);

fact = factorial(number);

printf("Factorial of %d is %ld\n", number, fact);

return 0;

}
Write a program in c to print Fibonacci series using recursion.

#include <stdio.h>

int fibonacci(int i) {

if(i == 0) {

return 0;

if(i == 1) {

return 1;

return fibonacci(i-1) + fibonacci(i-2);

int main() {

int i;

for (i = 0; i < 10; i++) {

printf("%d\t\n", fibonacci(i));

return 0;

You might also like