Write down the C code for the following questions.a) Write a program in C to check if a given number is even or odd using the function.b) Write a program in C to swap two numbers using a function.c) Write a program in C to find the square of a number using the function.d) Write a program in C to find the factorial of a number using the function.e) Write a program in C to calculate the area of circle using the function.f) Write a C program to compute the sum of the first “N” natural numbers using thefunction.g) Write a program in C to print the Fibonacci series using the function.
Question
Write down the C code for the following questions.a) Write a program in C to check if a given number is even or odd using the function.b) Write a program in C to swap two numbers using a function.c) Write a program in C to find the square of a number using the function.d) Write a program in C to find the factorial of a number using the function.e) Write a program in C to calculate the area of circle using the function.f) Write a C program to compute the sum of the first “N” natural numbers using thefunction.g) Write a program in C to print the Fibonacci series using the function.
Solution
Sure, here are the C code for the questions:
a) Check if a number is even or odd:
#include <stdio.h>
void check(int num) {
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
check(num);
return 0;
}
b) Swap two numbers:
#include <stdio.h>
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
printf("Before swapping: %d, %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: %d, %d", num1, num2);
return 0;
}
c) Find the square of a number:
#include <stdio.h>
int square(int num) {
return num * num;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Square of %d is %d", num, square(num));
return 0;
}
d) Find the factorial of a number:
#include <stdio.h>
int factorial(int num) {
if(num == 0)
return 1;
else
return num * factorial(num - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
e) Calculate the area of a circle:
#include <stdio.h>
#define PI 3.1416
float area(float radius) {
return PI * radius * radius;
}
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Area of the circle is %.2f", area(radius));
return 0;
}
f) Compute the sum of the first “N” natural numbers:
#include <stdio.h>
int sum(int num) {
return num * (num + 1) / 2;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of first %d natural numbers is %d", num, sum(num));
return 0;
}
g) Print the Fibonacci series:
#include <stdio.h>
void fibonacci(int num) {
int t1 = 0, t2 = 1, nextTerm;
for (int i = 1; i <= num; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
}
int main() {
int num;
printf("Enter the number of terms: ");
scanf("%d", &num);
fibonacci(num);
return 0;
}
Similar Questions
Write a C program, using functions where the function calculates the factorial of a given integer, and the main() program does I/O and calling of factorial function.
Explain the structure of C program. Explain type conversion in C. Write a C program to find prime numbers between a range(using function). Explain any 5 string handling functions available in C. Write a C program to print the address of a variable along with its value.
Consider the C program shown below -int sumOdd(int *a, int n){ if(n <= 0) return 0; else if (*a % 2 != 0) return *a + sumOdd (a + 1, n - 1); return sumOdd (a + 1, n - 1);}int main(){ int a[] = {1,2, 3, 4, 5 ,6}; printf("%d\n", sumOdd(a, 6)); return 0;}The output of this program is -912158
#include<stdio.h>int main(){int a;scanf("%d",&a);int B=a%2;int C=a*a*a;int D=a*a;switch(B){case 0:printf("Even\n");printf("%d\n",C);break;case 1:printf("Odd\n");printf("%d\n",D);break;}return 0;}
write an iterative code for factorial in c
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.