Write a program to find sum of elements of 1-D Array using Function.
#include <stdio.h>#include <conio.h>intmain(){int a[1000], i, n, sum = 0;printf("Enter size of the array : ");scanf("%d", &n);printf("Enter elements in array : ");for (i = 0; i < n; i++){scanf("%d", &a[i]);}for (i = 0; i < n; i++){sum += a[i];}printf("sum of array is : %d", sum);return 0;}
Write a program that use user defined function swap() to interchange the value of two variable.
#include <stdio.h>void swap(int *, int *);int main(){int n1, n2;printf("\n\n Function : swap two numbers using function :\n");printf("------------------------------------------------\n");printf("Input 1st number : ");scanf("%d", &n1);printf("Input 2nd number : ");scanf("%d", &n2);printf("Before swapping: n1 = %d, n2 = %d ", n1, n2);// pass the address of both variables to the function.swap(&n1, &n2);printf("\nAfter swapping: n1 = %d, n2 = %d \n\n", n1, n2);return 0;}void swap(int *p, int *q){// p=&n1 so p store the address of n1, so *p store the value of n1// q=&n2 so q store the address of n2, so *q store the value of n2int tmp;tmp = *p; // tmp store the value of n1*p = *q; // *p store the value of *q that is value of n2*q = tmp; // *q store the value of tmp that is the value of n1}
Write a program to find factorial of a number using recursion.
#include <stdio.h>long int multiplyNumbers(int n);int main(){int n;printf("Enter a positive integer: ");scanf("%d", &n);printf("Factorial of %d = %ld", n, multiplyNumbers(n));return 0;}long int multiplyNumbers(int n){if (n >= 1)return n * multiplyNumbers(n - 1);elsereturn 1;}
Write a program to generate Fibonacci series using recursion
#include <stdio.h>int main(){int n1 = 0, n2 = 1, n3, i, number;printf("Enter the number of elements:");scanf("%d", &number);printf("\n%d %d", n1, n2); // printing 0 and 1for (i = 2; i < number; ++i) // loop starts from 2 because 0 and 1 are already printed{n3 = n1 + n2;printf(" %d", n3);n1 = n2;n2 = n3;}return 0;}
0 Comments