BHARAT RATNA PURATCHI THALAIVAR
Dr. MGR GOVT. ARTS & SCIENCE COLLEGE,
PALACODE - 636808
DEPARTMENT OF COMPUTER SCIENCE
ALLIED PRACTICAL II - PROGRAMMING IN C
Name : ……………………………………………...
Reg. No : ………………………………....…………...
Class : II – B.Sc. ELECTRONICS AND COMMUNICATION
BHARAT RATNA PURATCHI THALAIVAR
Dr.M.G.R GOVT. ARTS & SCIENCE COLLEGE,
PALACODE – 636808
DEPARTMENT OF COMPUTER SCIENCE
BONAFIDE CERTIFICATE
This is to certificate that this Record work done by ____________________________
Reg. No. _______________________of II - B.Sc (Electronics and Communication) in
III - Semester for “ALLIED PRACTICAL II - PROGRAMMING IN C” during the year
2020-2021 at the Bharat Ratna Puratchi Thalaivar Dr. MGR Govt. Arts & Science College,
Palacode – 636808.
Staff-in-charge Head of the Department
Submitted for Practical Examination hold on ___________ at Bharat Ratna
Puratchi Thalaivar Dr. MGR Govt. Arts & Science College, Palacode – 636808.
Internal Examiner External Examiner
LIST OF EXPERIMENTS
S. NO DATE PROGRAM NAME PAGE REMARKS
1 Simple Interest and Compound Interest
2 Mean and Standard Deviation
3 The largest of a given set of numbers
4 Finding Prime number or not
5 Calculating sin(x) using series
6 Printing Fibonacci series
7 Generating Pascal triangle
8 Binary to Decimal conversion
9 Calculating binomial co-efficient using
recursion
10 Reverse a string without using string library
function
1. Program to find the Simple Interest and Compound Interest
Aim:
To write a program to find the Simple and Compound interest.
Algorithm:
A. START
B. Declare variables p , n , r ,si and ci .
C. Read the values of variable p ( principal ) , n ( Number of years ) , r ( Rate of Interest
).
D. calculate the values of “ si = ( p * n * r )/100 “ .
E. calculate the values of “ ci = p (1+ r /100 )n “ .
F. Display si ( simple interest ) .
G. Display si ( compound interest ).
H. STOP
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,t;
float r,si,amount,ci;
clrscr();
printf("Please enter principal,time and rate of interest\n");
scanf("%d%d%f",&p,&t,&r);
si=p*t*r/100;
//Simple Interest formula is p*t*r
printf("\nSimple interest = %.3f",si);
//Compound Interest formula is below
amount=p*pow((1 +r/100),t);
ci=amount-p;
printf("\nCompound interest = %.3f",ci);
getch();
}
Output:
Please enter principal, time and rate of interest
10000
2
3
Simple interest = 600.000
Compound interest = 609.000
Result:
Thus, the above program has been executed and the result is verified successfully.
2. Program to find the mean and standard deviation
Aim:
To write a C program to find the mean and standard deviation.
Algorithm:
Step 1: Find the mean.
Step 2: For each data point, find the square of its distance to the mean.
Step 3: Sum the values from Step 2.
Step 4: Divide by the number of data points.
Step 5: Take the square root.
Flowchart:
Source Code:
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 20
int main()
{
int i,n;
float data[MAX],sum=0.0,mean,sd=0.0;
clrscr();
printf("How many elements do you want to enter?\n");
scanf("%d",&n);
printf("Enter %d elements one by one\n",n);
for(i=0;i<n;i++)
scanf("%f",&data[i]);
for(i=0;i<n;i++)
{
sum+=data[i];
}
mean=sum/n;
for(i=0;i<n;i++)
sd+=pow(data[i]-mean,2);
sd=sqrt(sd/n);
printf("\nThe mean of the above %d elements is=%.2f",n,mean);
printf("\nThe Standard Deviation of the above %d elements is=%.2f",n,sd);
getch();
return 0;
}
Output:
How many elements do you want to enter?
7
Enter 7 elements one by one
5
12
4
6
10
8
9
The mean of the above 7 elements is=7.71
The Standard Deviation of the above 7 elements is=2.66
Result:
Thus, the above program has been executed and the result is verified successfully.
3. PROGRAM TO FIND THE LARGEST OF A GIVEN SET OF NUMBERS
Aim:
To write a program to find the largest of a given set of numbers.
Algorithm:
1. look at the set of numbers.
2. for each number, see if it’s bigger than any earlier number.
3. if it is, remember that number.
4. keep going until you’ve looked at them all.
5. the last number you remembered is the largest.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
int c,big;
clrscr();
printf("******Prgram to find the largest number among given number******");
printf("\nEnter the number of elements you wish enter: ");
scanf("%d", &n);
printf("\nEnter %d numbers :", n);
printf("\n\t\t\tElement 1: ");
scanf("%f", &big);
for(i = 2; i <= n; i++)
{
printf("\n\t\t\tElement %d : ", i);
scanf("%f", &c);
if(big < c)
big = c;
}
printf("\n\nThe largest of the %d numbers is %f ", n, big);
getch();
return 0;
}
Output:
******Prgram to find the largest number among given number******
Enter the number of elements you wish enter: 5
Enter 5 numbers:
Element 1: 8
Element 2: 6
Element 3: 7
Element 4: 10
Element 5: 56
The largest of the 5 numbers is 56
Result:
Thus, the above program has been executed and the result is verified successfully.
4. Program to find the given number is Prime or not
Aim:
To write a C program to find the given number is prime or not.
Algorithm:
1. Start
2. Initialize variables num,flag=1, j=2
3. Read num from user
4. If num<=1 // Any number less than 1 is not a prime number
5. Display "num is not a prime number"
6. Goto step 7
7. Repeat the steps until j<[(n/2)+1]
5.1 If remainder of number divide j equals to 0,
8. Set flag=0
9. Goto step 6
5.2 j=j+1
10. If flag==0,
11. Display num+" is not prime number"
12. Else
13. Display num+" n is prime number"
14. Stop.
Flowchart:
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i,flag=0;
clrscr();
printf("Input a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num % i==0){
flag++;
break;
}
}
if(flag==0 && num!= 1)
printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number",num);
getch();
}
Output:
Input a number: 8
8 is not a prime number
Input a number: 5
5 is a prime number.
Result:
Thus, the above program has been executed and the result is verified successfully.
5. Create a program to calculate sin(x) using series
Aim:
To write a C program to calculate sin(x) using series.
Algorithm:
1. First the computer reads the value of x and limit from the user.
2. Now convert x to radian value x=x*(3.1415\180)
3. Then using for loop the value of sin(x) is calculated.
4. Finally, the value of sin(x) is printed.
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
float x, sum, t;
clrscr();
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
/* Loop to calculate the value of Sine */
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
getch();
}
Output:
Enter the value for x : 2
Enter the value for n : 3
The value of Sin(0.034907) = 0.0349
Result:
Thus, the above program has been executed and the result is verified successfully.
6. Create a program to print the Fibonacci series
Aim:
To create a C program to print the Fibonacci Series.
Algorithm:
A. Start
B. Declare variables i, a,b , show
C. Initialize the variables, a=0, b=1, and show =0
D. Enter the number of terms of Fibonacci series to be printed
E. Print First two terms of series
F. Use loop for the following steps
G. show=a+b
H. a=b
I. b=show
J. increase value of i each time by 1
K. print the value of show
L. End
Flowchart:
Source Code:
#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
clrscr();
printf("Fibonacci series\n");
printf("Enter the value for N\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
getch();
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Output:
Fibonacci series
Enter the value for N
5
0
1
1
2
3
Result:
Thus, the above program has been executed and the result is verified successfully.
7. Create a program to generate Pascal triangle
Aim:
To create a C program to generate the Pascal triangle.
Algorithm:
A. Start
B. Declare variables x, y, n, a, z, s
C. Enter the limit
D. Initialize the value of variables, s=n , x=0, y=0 , z=s
E. Do the following operations in loop
i) x = 0 to n
ii) a= 1, x++
iii) z=s to 0
iv) print space
v) z—-
vi) y = o to x
vii) print a
viii) a = a*(x-y)/(y+1)
ix) y= y+1
x) go to next line
F. Repeat the process to n
G. Print the final required triangle
H. Stop
Flowchart:
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int array[30], temp[30], i, j, k, l, num;
clrscr();
printf("Enter the number of lines to be printed: ");
scanf("%d", &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
printf(" ");
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d", array[l]);
temp[l] = array[l];
}
printf("\n");
}
getch();
}
Output:
Enter the number of lines to be printed: 4
1
1 1
1 2 1
1 3 3 1
Result:
Thus, the above program has been executed and the result is verified successfully.
8. Create a program to convert Binary to Decimal
Aim:
To create a C program to convert the binary into decimal.
Algorithm:
A. Start
B. Divide the number by 2 through % (modulus operator) and store the remainder in
array
C. Divide the number by 2 through / (division operator)
D. Repeat the step 2 until number is greater than 0
E. Stop
Flowchart:
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
clrscr();
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
getch();
}
Output:
Enter a binary number(1s and 0s)
1010
The Binary number is = 1010
Its decimal equivalent is = 10
Result:
Thus, the above program has been executed and the result is verified successfully.
9. Create a program to calculate binomial co-efficient using recursion
Aim:
To create a C program to calculate binomial co-efficient using recursion.
Algorithm:
Step 1: Get the two inputs, the positive value of n and the non-positive value of k which
denotes the k-th binomial coefficient in the Binomial Expansion.
Step 2: Allocate the array of size k + 1 with the value of 1 at 0-th index and rest with value 0.
Step 3: Next, generating the sequence of pascal's triangle, with the first row containing single
element valued 1 which was already created in step 2.
Step 4: Further next consecutive rows of pascal's triangle are computed from the previous
row by adding the two consecutive elements, but step 4 is to be carried out upto k-
times, for enclosing n-value times.
Step 5: Stop.
Flowchart:
Source Code:
#include <stdio.h>
#include <conio.h>
int BC(int n, int k);
int main()
{
int n,k;
clrscr();
printf("Enter n and k : ");
scanf("%d%d",&n,&k);
printf("\nBinomial coefficient\n",BC(n,k));
printf("%d\n",BC(n,k));
getch();
return 0;
}
int BC(int n, int k)
{
if(k==0 || k==n)
return 1;
return BC(n-1,k-1) + BC(n-1,k);
}
Output:
Enter n and k: 4
2
Binomial coefficient
6
Result:
Thus, the above program has been executed and the result is verified successfully.
10. Create a program to reverse a string without using string library function
Aim:
To create a C program to reverse a string without using string library function.
Algorithm:
A. Calculate the length (Len) of string.
B. Initialize the indexes of the array.
C. Start = 0, End = Len-1
D. In a loop, swap the value of pszData[Start] with pszData[End].
E. Change the indexes’ of the array as follows.
F. Start = start +1; End = end – 1
Flowchart
Source Code:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main() {
char str[100], temp;
int i, j = 0;
clrscr();
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
getch();
return (0);
}
Output:
Enter the string: welcome
Reverse string is: emoclew
Result:
Thus, the above program has been executed and the result is verified successfully.