Posts

C-Codes using Recursion

  1. Write a program to calculate the factorial of the number using recursion #include<stdio.h> long factorial(int a); int main() { int n; long Fact; printf("Enter the number:"); scanf("%d",&n); Fact=factorial(n); printf("The factorial is %ld",Fact); } long factorial(int n) { long f; if(n==0) { return 1; } else { f=n*factorial(n-1); return f; } } 2. Write a program to calculate the fibonacci sequence using recursion #include<stdio.h> int fibonacci(int); int main() { int n,i=1; printf("Enter the number of terms:"); scanf("%d",&n); while(i<=n) { printf("\t%d",fibonacci(i)); i++; } return 0; } int fibonacci(int n) { if(n==1) return 0; else if (n==2) return 1; else return(fibonacci(n-1)+fibonacci(n-2)); } 3. Write a program to read a number n from user and calculate sum of first n natural numbers using Recursion #include<stdio.h> int sum(int); int main() { int a,Sum; printf("Enter the number:...

C Control Statements: For Loop Codes

1. Write a program to find the factorial of a given number #include <stdio.h> int main() {     int n, i;     long fact = 1;          printf("Enter the number: ");     scanf("%d", &n);          for (i = 1; i <= n; i++) {         fact = fact * i;     }          printf("The factorial of %d is %ld", n, fact);          return 0; } 2. Write a program to find the sum of first n natural number #include <stdio.h> int main() {     int n, sum = 0, i;          printf("Enter the number: ");     scanf("%d", &n);          for (i = 1; i <= n; i++) {         sum = sum + i;     }          printf("The sum of natural numbers up to %d = %d", n, sum);          return 0; } 3...

C Control Statements: While, and Do-While Loop Codes

Image
1. WAP to read a number from the user and whether a given number is prime or composite. #include <stdio.h> int main() {     int n, i = 2;     printf("Enter the number: ");     scanf("%d", &n);     while (i < n) {         if (n % i == 0) {             printf("Composite Number");             break;         }         i++;     }     if (i == n)         printf("Prime Number");     return 0; } 2. Write a program to read two numbers from the user (n1<n2) and display the natural numbers between them #include <stdio.h> int main() {     int n1, n2, i = 1;     printf("Enter the two numbers: ");     scanf("%d%d", &n1, &n2);          while (i <= (n2 - n1 - 1)) {         printf("The numbe...

C- Control Statement: if-else ladder Codes

Image
 1.Write a program to find the given number is positive or negative #include <stdio.h> int main() {     int a;     printf("Enter the number, a: ");     scanf("%d", &a);          if (a > 0) {         printf("Positive Number");     } else {         printf("Negative Number");     }          return 0; } 2. Write a program to find the given number is odd or even #include <stdio.h> int main() {     int a, R;     printf("Enter the number, a: ");     scanf("%d", &a);          R = a % 2;          if (R == 0) {         printf("Even Number");     } else {         printf("Odd Number");     }          return 0; } 3. Write a program to check whether a first number is divisi...

C- Control Statement: Switch Codes

 1. Write a program to find the arithmetic operation using the switch statement #include <stdio.h> int main() {     float a, b, R;     char x;     printf("Enter the numbers: ");     scanf("%f%f", &a, &b);     printf("Enter the operation (+, -, *, /): ");     scanf(" %c", &x);          switch (x) {         case '+':             R = a + b;             printf("The sum is %.2f", R);             break;         case '-':             R = a - b;             printf("The difference is %.2f", R);             break;         case '*':             R = a * b;             printf("The product is %....

C-User Defined Function Codes ( 24 Questions Solved)

 1. Write a program to find the factorial of a given number using function. #include<stdio.h> long factorial(int a); int main() { int n; long Fact; printf("Enter the number:"); scanf("%d",&n); Fact=factorial(n); printf("The factorial of the %d is %ld",n,Fact); } long factorial(int a) { int i; long fact=1; for(i=1; i<=a; i++) { fact=fact*i; } return fact; } 2. Write a program to find the combination using function #include<stdio.h> long factorial(int); int main() { int n,r; long f1,f2,f3,C; printf("Enter the total number,n and number being chosen,r:"); scanf("%d%d",&n,&r); f1=factorial(n); f2=factorial(n-r); f3=factorial(r); C=f1/(f2*f3); printf("The combination is %ld",C); return 0; } long factorial(int n) { int i; long Fact=1; for(i=1; i<=n; i++) { Fact=Fact*i; } return Fact; } 3. Write a program to find the s...