For B.Sc.(Hons.)Maths Pt-II/BCA/MCA/B.Tech. (CS)
Practical with Computer Programming in C Language.
I N D E X
S.N |
Name of the Experiment Group –“A” |
Page No. |
1. |
C Program printing n terms of Fibonacci sequence/ series. |
|
2. |
C program to finding factorial value of given integer number. (n!) |
|
3. |
C program to find the sum and display n terms of natural numbers. Σ n |
|
4. |
C program to find the sum and display n terms of Square Natural n Numbers. Σ n2 |
|
5. |
C program to Defining a function and finding sum of n terms of a series / sequence whose general term is given (e.g. an = (n2 + 3)/ (n+1) ) |
|
6. |
C program to Printing Pascal’s triangle. |
|
7. |
C program to finding GCD and LCM of two number by Euclid’s algorithm. |
|
8. |
C program to checking prime/composite number |
|
9. |
C program to finding numbers of prime less than n, n Є Z. |
|
10. |
C program to finding mean. |
|
11. |
C program to finding standard deviation. |
|
12. |
C program to finding nPr, nCr for different n and r. |
|
Solved Program
1. |
C Program printing n terms of Fibonacci sequence/ series. 0 1 1 2 3 5 8 …. n terms
|
|
Source Code: /* display Fibonacci series: 0 1 1 2 3 5 8 …. n terms */ /*using for loop statement */ #include<stdio.h> int main() { int prev=0, curr=1, term, n, i; printf(“Enter how many terms to be display \n”); scanf(“%d”,&n); printf(“\nFibonacci series is: \n\n”); printf(“%5d%5d”, prev,curr); for(i=3;i<=n;i++) { term = prev+curr; printf(“%5d”, term); prev = curr; curr = term; } return 0; }
|
|
Output/Run 1. Enter how many terms to be display 10 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34
2. Enter how many terms to be display 11 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55
|
|
Flowchart
|
2. |
C program to finding factorial value of given integer number. (n!) n! = n*(n-1)* (n-2) *(n-3)….1 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
|
|
Source Code: /* read an integer value and calculate factorial value */ #include <stdio.h> int main() { int i, n; long int f=1; printf(“Enter an integer number :”); scanf(“%d”, &n); /* factorial calculation */ for( i=n; i>=1; i–) { f= f*i; } printf(“\n Factorial value %d! = %ld\n”, n, f); return 0; }
|
|
Output/Run
1. Enter an integer number : 5 Factorial value 5! = 120
2. Enter an integer number : 7 Factorial value 7! = 5040
|
|
Flowchart
|
3. |
C program to find the sum and display n terms of natural numbers. Σ n 1 2 3 4 5 6 7…… n terms
|
|
Source Code:
#include <stdio.h> int main() { int n, i, sum=0; printf(“Enter How many terms to be display\n”); scanf(“%d”,&n); /* display series and find sum */ for(i=1; i<=n; i++) { printf(“%4d”,i); sum = sum + i; } printf(“\n Sum of natural number = %d \n”, sum); return 0; }
|
|
Output/Run
Enter How many terms to be display 8 1 2 3 4 5 6 7 8 Sum of natural number = 36
Enter How many terms to be display 10 1 2 3 4 5 6 7 8 9 10 Sum of natural number = 55
|
|
Flowchart
|
4. |
C program to find the sum and display n terms of Square Natural n Numbers. Σ n2 1 4 9 16 25 36 49 . . . n terms
|
|
Source Code:
/* Exp No 4: display Square natural number and find sum 1 4 9 16 …. n terms */ #include <stdio.h> int main() { int n, i, sum=0; printf(“Enter How many terms to be display\n”); scanf(“%d”,&n); /* display series and find sum */ for(i=1; i<=n; i++) { printf(“%4d”,i*i); sum = sum + i*i; } printf(“\n Sum of natural number = %d \n”, sum); return 0; }
|
|
Output/Run Enter the value of n : 10 1 4 9 16 25 36 49 64 81 100 Sum of the series = 385
Enter the value of n : 5 1 4 9 16 25 Sum of the series = 55
|
|
Flowchart
|
5. |
C program to Defining a function and finding sum of n terms of a series /sequence whose general term is given (E.g an = (n2 + 3)/ (n+1) )
|
|
Source Code:
|
|
Output/Run
|
|
Flowchart
|
6. |
C program to Printing Pascal’s triangle. Pascal’s triangle is triangular array of the binomial coefficients
|
|
Source Code:
/* Program :Printing Pascal’s triangle */ #include <stdio.h> int main() { int a[15][15], i, j, rows, p=25, k; printf(“\nEnter the number of rows display : “); scanf(“%d”, &rows); for(i=0;i<rows;i++) { for(j=p-2*i;j>=0;j–) printf(” “); for(j=0;j<=i; j++) { if(j==0||i==j) a[i][j] =1; else a[i][j] = a[i-1][j-1]+a[i-1][j]; printf(“%4d”,a[i][j]); } printf(“\n”); } return 0; }
|
|
Output/Run
|
|
Flowchart
|
7. |
C program to finding GCD and LCM of two number by Euclid’s algorithm.
n1 = 24 GCD (Greatest Common Divisor ) : 8 n2 = 40 or HCF (Highest Common Factor ) LCM (Lowest Common Multiple) : 120 |
|
Source Code:
/* Finding GCD and LCM by Euclid’s algorithms */ #include <stdio.h> int gcd(int n1, int n2); /* function prototype */ int main() { int n1, n2; printf(“Enter two positive integers: “); scanf(“%d%d”, &n1, &n2); printf(“\nG.C.D of %d and %d is %d.”, n1, n2, gcd(n1,n2)); printf(“\nL.C.M of %d and %d is %d\n”, n1, n2, (n1*n2)/gcd(n1,n2)); return 0; } /* function define as recursion */ int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1%n2); else return n1; }
|
|
Output/Run Enter two positive integers: 20 30 G.C.D of 20 and 30 is 10 L.C.M of 20 and 30 is 60 |
|
Flowchart
|
8. |
C program to checking prime/composite number
INPUT : 17 OUTPUT : 17 IS A PRIME NUMBER INPUT : 25 OUTPUT : 25 NOT A PRIME NUMBER IT IS A COMPOSITE NUMBER
|
|
Source Code:
/* Program : Checking Prime/Composite Number */ #include <stdio.h> #include<math.h> #include <stdlib.h>
int main() { int n, flag, s, i; printf(“\n\nEnter an integer number : “); scanf(“%d”, &n); if(n==1) { printf(“The number %d is neither prime nor composite\n”,n); return 0; exit(1); } s=sqrt(n); flag=0; for(i=2; i<=s; i++) { if(n%i==0) { printf(“The Number %d is not a Prime Number(Composite Number)\n”, n); flag=1; break; } } if(flag==0) printf(“The Number %d is a Prime Number \n”,n); return 0; }
|
|
Output/Run Enter an integer number : 47 The Number 47 is a Prime Number Enter an integer number : 65 The Number 65 is not a Prime Number(Composite Number) |
|
Flowchart
|
9. |
C program to finding numbers of prime less than n, n Є Z.
INPUT : 20 OUTPUT : 2 3 5 7 11 13 17
|
|
Source Code:
/* Display all Prime Number less than n */ #include<stdio.h> #include<math.h>
int prime(int x); /* function prototype declaration */ int main() { int t, n, flag; printf(“Enter the value of t (upper limit) : \n”); scanf(“%d”, &t); printf(“All prime numbers less than %d \n”, t); for(n=2; n<t; n++) { flag = prime(n); /* function calling */ if(flag ==0) printf(“%5d”,n); } return 0; } /* function definition */ int prime(int x) { int i, s; s=sqrt(x); for(i=2;i<=s; i++) if (x%i == 0) return(1); return(0); } |
|
Output/Run Enter the value of t (upper limit) : 30 All prime numbers less than 30 2 3 5 7 11 13 17 19 23 29
|
|
Flowchart
|
10. |
C program to finding mean. |
|
Source Code:
/* Calculate Arithmetic mean */
#include <stdio.h> #include <math.h>
int main() { float x[50], sum, mean; int n, i; printf(“How many numbers ? : “); scanf(“%d”, &n); printf (“Enter %d numbers:\n”, n); for(i = 0; i<n; i++) scanf(“%f”, &x[i]); /* calculate mean */ sum= 0.0; for(i = 0; i < n; i++) sum= sum + x[i]; mean = sum / n; printf(“Mean = %6.3f\n”, mean); return 0; } |
|
Output/Run How many numbers ? : 7 Enter 7 numbers: 4 7 8 5 4 3 9 Mean = 5.714 |
|
Flowchart
|
11. |
C program to finding standard deviation. |
|
Source Code: /* Calculate mean and standard deviation */
#include <stdio.h> #include <math.h>
int main() { float x[50], sum, mean, sd; int n, i; printf(“How many numbers ? : “); scanf(“%d”, &n); printf (“Enter %d numbers:\n”, n); for(i = 0; i<n; i++) scanf(“%f”, &x[i]); /* calculate mean */ sum= 0.0; for(i = 0; i < n; i++) sum= sum + x[i]; mean = sum / n;
/* calculate standard deviation */ sum= 0.0; for(i = 0; i < n; i++) sum = sum+ pow((x[i] – mean),2); sd = sqrt(sum / n); printf(“Mean = %6.3f\n”, mean); printf(“Standard Deviation = %6.3f\n”, sd); return 0; } |
|
Output/Run How many numbers ? : 7 Enter 7 numbers: 4 7 8 5 4 3 9 Mean = 5.714 Standard Deviation = 2.119 |
|
Flowchart
|
|
|
12. |
C program to finding nPr, nCr for different n and r. nPr = n!/(n-r)! nCr = n!/(r! * (n-r)!)
|
|
Source Code: /* Program Calculate npr and ncr */ #include<stdio.h>
long int fact(int x); /* function prototype */ int main() { printf(“enter the value of n & r\n”); scanf(“%d%d”,&n, &r); npr = fact(n)/fact(n-r); ncr = fact(n)/(fact(r)*fact(n-r)); printf(“nPr = %d\n”,npr); printf(“nCr = %d\n”,ncr); return 0; } /* function definition */ long int fact(int x) { int i; long int f =1; for(i=1;i<=x; i++) f= f*i; return(f); } |
|
Output/Run enter the value of n & r 6 3 nPr = 120 nCr = 20 |
|
Flowchart
|