0% found this document useful (0 votes)
88 views37 pages

Computer Programming Laboratory 2018-2019

The document describes 6 programs related to computer programming laboratory assignments: 1) A calculator program that allows arithmetic operations on two integers and displays results. 2) A program to compute the roots of a quadratic equation by accepting coefficients and handling different cases. 3) A program to find the reverse of a positive integer and check if it is a palindrome. 4) A program to calculate electricity bill rates based on units consumed and applying different slabs and surcharges. 5) Instructions for a program to implement binary search on a 1D array.

Uploaded by

Radhika Ajadka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views37 pages

Computer Programming Laboratory 2018-2019

The document describes 6 programs related to computer programming laboratory assignments: 1) A calculator program that allows arithmetic operations on two integers and displays results. 2) A program to compute the roots of a quadratic equation by accepting coefficients and handling different cases. 3) A program to find the reverse of a positive integer and check if it is a palindrome. 4) A program to calculate electricity bill rates based on units consumed and applying different slabs and surcharges. 5) Instructions for a program to implement binary search on a 1D array.

Uploaded by

Radhika Ajadka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Computer Programming Laboratory 2018-2019

Program 2: Commercial Calculator


Develop a program to solve simple computational problems using arithmetic expressions
and use of each operator leading to simulation of a commercial calculator. (No built-in
math function)
***************************************************************************

File : Calculator.c

Description: Implementation of commercial calculator

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program to implement a commercial calculator*/

#include<stdio.h>

void main()

int num1,num2,opt;

printf("Enter the first Integer:\n");

scanf("%d",&num1);

printf("Enter the second Integer:\n");

scanf("%d",&num2);

for(;;)

printf("\n\n\n\nEnter the your option:\n");

printf("\n1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");

scanf("%d",&opt);

switch(opt)

Dept. of CSE, SIT, Valachil Page 1


Computer Programming Laboratory 2018-2019

case 1:printf("\nAddition of %d and %d is: %d",num1,num2,num1+num2);

break;

case 2:printf("\nSubstraction of %d and %d is: %d",num1,num2,num1-num2);

break;

case 3:printf("\nMultiplication of %d and %d is: %d",num1,num2,num1*num2);

break;

case 4:

if(num2==0)

printf("Devide by zero\n");

else

printf("\n Division of %d and %d is: %d",num1,num2,num1/num2);

break;

case 5: return 0;

break;

default:printf("\n Enter correct option\n");

break;

Dept. of CSE, SIT, Valachil Page 2


Computer Programming Laboratory 2018-2019

Output 1: Run the following commands in your terminal:


cc calculator.c
./a.out
Enter the first Integer:
10
Enter the second Integer:
5
Enter the your option:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
1
Enter the your option:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
2
Substraction of 10 and 5 is: 5
Enter the your option:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
3
Multiplication of 10 and 5 is: 50
Enter the your option:
1-Addition.
2-Substraction.

Dept. of CSE, SIT, Valachil Page 3


Computer Programming Laboratory 2018-2019

3-Multiplication.
4-Division.
5-Exit.
4
Division of 10 and 5 is: 2
Enter the your option:
1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.
7
Enter correct option
Enter the your option:

1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.

Dept. of CSE, SIT, Valachil Page 4


Computer Programming Laboratory 2018-2019

Program 3: Quadratic Equation


Develop a program to compute the roots of a quadratic equation by accepting the

coefficients. Print appropriate messages.

***************************************************************************

File : quadratic.c

Description: Program to compute roots of a quadratic equation.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/* Program for solving quadratic equation*/


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main ()
{
int a, b, c, d;
float x1, x2, real, img;
printf ("Enter three coefficients\n");
scanf ("%d%d%d", &a, &b, &c); //reading values for a,b,c
if (a * b * c == 0) //checking whether coefficients are zero
{
printf ("roots cannot be calculated\n");
exit (0);
}
d = b * b - 4 * a * c; //calculating discriminant value
if (d == 0) //checking whether discriminant value is equal to zero
{
printf ("roots are real and equal\n");
x1 = -b / (2 * a);
x2 = -b / (2 * a);
printf ("root1=%f\n", x1);
printf ("root2=%f\n", x2);
Dept. of CSE, SIT, Valachil Page 5
Computer Programming Laboratory 2018-2019

}
else if (d > 0) // checking whether discriminant value is positive
{
printf ("roots are real and distinct\n");
x1 = (-b + sqrt (d)) / (2 * a);
x2 = (-b - sqrt (d)) / (2 * a);
printf ("root1=%f\n", x1);
printf ("root2=%f\n", x2);
}
else // if discriminant value is negative
{
printf ("roots are complex and imaginery\n");
real = -b / (2 * a); //calculate real and imaginery roots
img = sqrt (fabs (d)) / (2 * a);
printf ("root1=%f+%fi\n", real, img);
printf ("root2=%f-%fi\n", real, img);
}
}

Output 1: Run the following commands in your terminal:


cc Quadratic.c -lm
./a.out
Enter three coefficients
1
0
1
roots cannot be calculated
Output 2:
cc Quadratic.c -lm
./a.out
Enter three coefficients
1
-4

Dept. of CSE, SIT, Valachil Page 6


Computer Programming Laboratory 2018-2019

4
roots are real and equal
root1=2.000000
root2=2.000000
Output 3:
cc Quadratic.c -lm
./a.out
Enter three coefficients
1
2
3
roots are complex and imaginary
root1=-1.000000+i1.414214
root2=-1.000000-i1.414214
Output 4:
cc Quadratic.c -lm
./a.out
Enter three coefficients
1
3
2
roots are real and distinct
root1=-1.000000
root2=-2.000000

Dept. of CSE, SIT, Valachil Page 7


Computer Programming Laboratory 2018-2019

Program 4: Palindrome
Develop a program to find the reverse of a positive integer and check for palindrome or
not. Display appropriate messages
***************************************************************************

File : palindrome.c

Description: Program to compute roots of a quadratic equation.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program to reverse a number and check whether its palindrome*/

#include<stdio.h>

main ()

int num, rem, temp, rev;

rev = 0; //initializing rev to zero

printf ("Enter the number\n");

scanf ("%d", &num);

temp = num; // assigning original number to temp variable

while (num != 0)

rem = num % 10; // process to reverse a number

rev = rev * 10 + rem;

num = num / 10;

printf ("The reverse of number is %d\n", rev);

if (temp == rev) //comparing the given number with its reverse

Dept. of CSE, SIT, Valachil Page 8


Computer Programming Laboratory 2018-2019

printf ("Number is palindrome\n");

else

printf ("Number is not palindrome\n");

Output 1: Run the following commands in your terminal:

cc Palindrome.c

./a.out

Enter the number

1221

Reverse of number is 1221

Number is palindrome

Output 2:

cc Palindrome.c

./a.out

Enter the number

1234

The reverse of number is 4321

Number is not palindrome

Dept. of CSE, SIT, Valachil Page 9


Computer Programming Laboratory 2018-2019

Program 5: Electricity Board Rate


An electricity board charges the following rates for the use of electricity: for the first 200
units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per
unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount is
more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the name of the user, number of units consumed and print out the
charges.
***************************************************************************
File : electric.c

Description: Program to calculate rates for the use of electricity based on number of units.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program to calculate rates for the use of electricity based on number of units.*/

#include<stdio.h>
#include<math.h>
void main()
{
char name[20];
float unit, charge;
printf("Enter your name and unit Consumed:");
scanf("%s %f",&name,&unit);
if(unit<125)
charge=100;
else if(unit<=200)
charge=unit*.80;
else if(unit<=300)
charge=(unit-200)*0.90+160;
else
if(unit>300)
charge=(unit-300)*1+250;
if(charge>=400)

Dept. of CSE, SIT, Valachil Page 10


Computer Programming Laboratory 2018-2019

charge=charge+charge*0.15;
printf("Name: %s\nCharge: %5.2f",name,charge);
}

Output 1: Run the following commands in your terminal:

cc electric.c

./a.out

Enter your name and unit Consumed: ram


400
Name: ram
Charge: 350.00

Output 2:
Enter your name and unit Consumed:dennis
100
Name: dennis
Charge: 100.00

Dept. of CSE, SIT, Valachil Page 11


Computer Programming Laboratory 2018-2019

Program 6: Binary Search


Introduce 1D Array manipulation and implement Binary search.

***************************************************************************

File : binary.c

Description: Program to search the element using binary search

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/* Program to search the element using binary search */


#include <stdio.h>
main()
{
int i, first, last, middle, n, key, array[100],found=0;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter array integers in ascending order:\n");
for (i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
printf("Enter the key element:\n");
scanf("%d", &key);

first = 0;
last = n - 1;
while (first <= last && !found)
{
middle = (first+last)/2;
if (array[middle] == key)
{

found=1;

Dept. of CSE, SIT, Valachil Page 12


Computer Programming Laboratory 2018-2019

}
else if (array[middle] > key)
{
first = middle + 1;
}

else
{
last = middle - 1;
}
}
if(found == 1)
{
printf("Key %d found at location %d.\n", key, middle+1);
}
else
{
printf("Key Not found!");
}
}

Output 1: Run the following commands in your terminal:

cc binary.c

./a.out

Enter number of elements


5
Enter array integers in ascending order:
10 20 30 40 50
Enter the key element:
30
Key 30 found at location 3.

Dept. of CSE, SIT, Valachil Page 13


Computer Programming Laboratory 2018-2019

Output 2:
Enter number of elements
5
Enter array integers in ascending order:
25 40 56 78 90
Enter the key element:
100
Key Not found!

Dept. of CSE, SIT, Valachil Page 14


Computer Programming Laboratory 2018-2019

Program 7: Prime Number


Implement using functions to check whether the given number is prime and display
appropriate messages. (No built-in math function)
***************************************************************************
File : prime.c

Description: Program on matrix multiplication

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program to check prime number*/


#include<stdio.h>
int isprime (int); //function declaration
main ()
{
int x, y, i, flag = 0;
printf ("enter the range");
scanf ("%d%d", &x, &y);
printf("prime numbers in the range %d and %d\n”, x,y);
for (i = x; i <= y; i++)
{
if (isprime (i)) //function call
{
printf ("%d\t", i);
flag = 1;
}
}
if (flag == 0)
printf ("no prime number in that range");
}
int isprime(int num) //function definition
{
int i;
if(num==0||num==1)
Dept. of CSE, SIT, Valachil Page 15
Computer Programming Laboratory 2018-2019

return 0;
for(i=2;i<num;i++)
{
if(num%i==0) //checking for prime number
return 0;
}
return 1;
}

Output 1: Run the following commands in your terminal:


cc prime.c
./a.out
Enter the range
10
20
prime numbers in the range 10 and 20
11 13 17 19

Output 2:
cc prime.c
./a.out
Enter the range
20
22
no prime number in that range

Dept. of CSE, SIT, Valachil Page 16


Computer Programming Laboratory 2018-2019

Program 8: Matrix Multiplication


Develop a program to introduce 2D Array manipulation and implement Matrix
multiplication and ensure the rules of multiplication are checked.

***************************************************************************
File : matrix.c

Description: Program to check whether the given string is palindrome or not.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program on matrix multiplication*/


#include<stdio.h>
#include<stdlib.h>
main ()
{
int i, j, k, m, n, p, q, a[10][10], b[10][10], c[10][10];
printf ("\nEnter the order of matrix A\n");
scanf ("%d%d", &m, &n);
printf ("\nEnter the order of matrix B\n");
scanf ("%d%d", &p, &q);
if (n != p)
{
printf ("matrix multiplication is not possible\n");
exit (0);
}
printf ("\nEnter the elements of A\n ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf ("%d", &a[i][j]); // reading values for matrix A
}
}

Dept. of CSE, SIT, Valachil Page 17


Computer Programming Laboratory 2018-2019

printf ("\nEnter the elements of B\n ");


for (i = 0; i < p; i++)
{
for (j= 0; j < q; j++)
{
scanf ("%d", &b[i][j]); //reading values for matrix B
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j]; // matrix multiplication
}
}
}
printf ("Matrix A\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf ("%d\t", a[i][j]); //printing matrix A
}
printf ("\n");
}
printf ("Matrix B\n");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{

Dept. of CSE, SIT, Valachil Page 18


Computer Programming Laboratory 2018-2019

printf ("%d\t", b[i][j]); //printing matrix B


}
printf ("\n");
}
printf ("Resultant matrix\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
printf ("%d\t", c[i][j]); //printing matrix C
}
printf ("\n");
}
}

Output 1: Run the following commands in your terminal:


cc matrix.c
./a.out
Enter the order of matrix A
2
2
Enter the order of matrix B
2
2
Enter the elements of A
1
2
3
4
Enter the elements of B
1
2
3

Dept. of CSE, SIT, Valachil Page 19


Computer Programming Laboratory 2018-2019

4
Matrix A:
12
34
Matrix B:
12
34
Resultant matrix :
7 10
15 22

Output 2:
cc matrix.c
./a.out
Enter the order of matrix A
1
2
Enter the order of matrix B
1
2
matrix multiplication is not possible

Dept. of CSE, SIT, Valachil Page 20


Computer Programming Laboratory 2018-2019

Program 9: Taylor Series


Develop a Program to compute Sin(x) using Taylor series approximation. Compare your
result with the built- in Library function. Print both the results with appropriate messages

File : taylor.c

Description: Program to compute sin(x) using taylor series.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************
/*Program to compute sin(x) using taylor series*/

#include<stdio.h>
#include<math.h>
#define PI 3.1426 //defining PI value as 3.1426
main ()
{ int i;
float degree, x, term, sum;
printf ("enter the degree\n");
scanf ("%f", &degree);
x = (degree * PI) / 180; // converting the degree into radian
term = x; //initializing term as x
sum = term;
for (i = 3; i <= 15; i += 2)
{ term = (-term * x * x) / (i * (i - 1)); //calculating the value of each term
sum = sum + term; // addition of terms
}
printf ("sin(%f)=%f\n", degree, sum);
printf ("using library function\n");
printf ("sin(%f)=%f\n", degree, sin (x)); //using sin() library function
}

Dept. of CSE, SIT, Valachil Page 21


Computer Programming Laboratory 2018-2019

Output 1: Run the following commands in your terminal:


cc taylor.c -lm
./a.out
enter the degree : 60
sin(60.000000) =0.866193
using library function
sin(60.000000)=0.866193

Dept. of CSE, SIT, Valachil Page 22


Computer Programming Laboratory 2018-2019

Program 10: Function


Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
***************************************************************************

File : function.c

Description: Program on string function

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

#include<stdio.h>
#include<string.h>
int length = 0,i=0,j=0; // Initialization
void len(char[]); //Function Declaration
void concat(char[],char[]);
void comp(char[],char[]);

int main() {
char str1[100], str2[100];
printf("\nEnter the first String : ");
gets(str1);
printf("\nEnter the Second String : ");
gets(str2);

len(str1); //Function Calling


concat(str1,str2);
comp(str1,str2);
}

void len(char str1[]) //Function Defination


{
while (str1[length] != '\0')
length++;

Dept. of CSE, SIT, Valachil Page 23


Computer Programming Laboratory 2018-2019

printf("\nLength of the String1 is : %d", length);


return(0);
}

void concat(char str1[],char str2[])


{
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}

void comp(char str1[],char str2[])


{
i=0;
while(str1[i]==str2[i]&& str1[i]=='\0')
i++;
if (str1[i] < str2[i])
{
printf("\n String1 is less than String2\n");
}
else if (str1[i] > str2[i])
{
printf("\n String1 is greaterthan String2\n");
}
else

Dept. of CSE, SIT, Valachil Page 24


Computer Programming Laboratory 2018-2019

{
printf("\n String1 is Equal to String2\n");
}
}

Output 1: Run the following commands in your terminal:


cc function.c
./a.out
Enter the first String : dennis

Enter the Second String : ritchie

Length of the String1 is : 6


Concatenated String is dennisritchie
String1 is less than String2

Dept. of CSE, SIT, Valachil Page 25


Computer Programming Laboratory 2018-2019

Program 11: Bubble Sort


Develop a program to sort the given set of N numbers using Bubble sort.
***************************************************************************
File : bubble.c

Description: Program to sort the given number using Bubble Sort

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program to arrange numbers in ascending order using bubble sort*/


#include<stdio.h>
main ()
{
int n, i, temp, j, a[50];
printf ("enter the number \n ");
scanf ("%d", &n);
printf (" enter the elements of an array\n");
for (i = 0; i < n; i++)
{
scanf ("%d", &a[i]); //reading values to array
}
for (i = 0; i < n -1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1]) //comparing the adjacent elements
{
temp = a[j]; //swapping process
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf ("array elements after sorting\n");
Dept. of CSE, SIT, Valachil Page 26
Computer Programming Laboratory 2018-2019

for (i = 0; i < n; i++)


{
printf ("%d\t", a[i]);
}
}

Output: Run the following commands in your terminal:


cc bubble.c
./a.out
enter the number
5
enter the elements of an array
50 40 30 10 90
Array elements after sorting:
10 30 40 50 90

Dept. of CSE, SIT, Valachil Page 27


Computer Programming Laboratory 2018-2019

Program 12: Square Root


Develop a program to find the square root of a given number N and execute for all possible
inputs with appropriate messages. Note: Don’t use library function sqrt(n).
***************************************************************************
File : squareroot.c

Description: Program to find the square root of a given number

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program for calculating the square root of a number*/

#include<stdio.h>

#include<math.h>

void main()

float n,s;

int i;

printf("\n Enter the number to find the square root\n");

scanf("%f",&n);

if(n>0)

s = n/ 2;

for(i = 0; i <n; i++)

s = (s + (n/s))/2;

printf("Square root of %f is %f\n",n,s);

else

printf("\n Not possible to find the square root");

Dept. of CSE, SIT, Valachil Page 28


Computer Programming Laboratory 2018-2019

Output 1: Run the following commands in your terminal:

cc squareroot.c

./a.out

enter a number

square root is 2.000000

Dept. of CSE, SIT, Valachil Page 29


Computer Programming Laboratory 2018-2019

Program 13: Structure


Implement structures to read, write, compute average- marks and the students scoring above and
below the average marks for a class of N students.
***************************************************************************

File : structure.c

Description: Program on structure

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************

/*Program on structure*/

#include<stdio.h>

#include<string.h>

struct student // structure declaration

char name[20];

int mark1, mark2, mark3;

};

main ()

int i, n, found = 0,total_mark,avg[100];

struct student s[100]; //declaring array of structures s

printf ("Enter the number of students details\n");

scanf ("%d", &n);

for (i = 0; i < n; i++)

printf ("Enter student %d details\n", i + 1);

Dept. of CSE, SIT, Valachil Page 30


Computer Programming Laboratory 2018-2019

printf ("Enter the name \n");

scanf ("%s", s[i].name);

printf ("Enter the three subject marks\n");

scanf ("%d%d%d", &s[i].mark1,&s[i].mark2,&s[i].mark3);

printf ("Students details\n");

printf ("Name \t\t\t Marks1\t\t Marks2\t\t Marks3\n");

for (i = 0; i < n; i++)

printf ("%s\t\t\t%d\t\t\t%d\t\t\t%d\n", s[i].name, s[i].mark1,s[i].mark2,s[i].mark3);

for (i = 0; i < n; i++)

total_mark=s[i].mark1+s[i].mark2+s[i].mark3;

avg[i]=total_mark/3;

printf ("Total Marks of student %d is %d\n", i+1,total_mark);

printf ("Average Marks :%d\n", avg[i]);

for (i = 0; i < n; i++)

if (avg[i] <=45)

printf (" Below avg studens are:%s\n",s[i].name);

else

Dept. of CSE, SIT, Valachil Page 31


Computer Programming Laboratory 2018-2019

printf(" Above avg studens are:%s\n",s[i].name);

Output 1: Run the following commands in your terminal:

cc structure.c

./a.out

Enter the number of students details

Enter student 1 details

Enter the name

ram

Enter the three subject marks

11 20 5

Enter student 2 details

Enter the name

raj

Enter the three subject marks

40 50 75

Enter student 3 details

Enter the name

tom

Enter the three subject marks

5 9 15

Dept. of CSE, SIT, Valachil Page 32


Computer Programming Laboratory 2018-2019

Enter student 4 details

Enter the name

ann

Enter the three subject marks

35 65 95

Students details

Name Marks1 Marks2 Marks3

ram 11 20 5

raj 40 50 75

tom 5 9 15

ann 35 65 95

Total Marks of student 1 is 36

Average Marks :12

Total Marks of student 2 is 165

Average Marks :55

Total Marks of student 3 is 29

Average Marks :9

Total Marks of student 4 is 195

Average Marks :65

Below avg studens are:ram

Above avg studens are:raj

Below avg studens are:tom

Dept. of CSE, SIT, Valachil Page 33


Computer Programming Laboratory 2018-2019

Program 14: Pointers


Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
***************************************************************************
File : pointer.c

Description: Program to compute the sum, mean and standard deviation.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************
/*Program to calculate sum,mean and standard deviation*/

#include<stdio.h>

#include<math.h>

main ()

float arr[100], sum = 0, sum1 = 0, mean, var, sd, diff;

float *ptr; // declaring a pointer variable

int n, i;

printf ("Enter the value of n\n");

scanf ("%d", &n);

printf ("Enter the array elements\n");

for (i = 0; i < n; i++)

scanf ("%f", &arr[i]);

ptr = arr; //initializing pointer variable

for (i = 0; i < n; i++)

Dept. of CSE, SIT, Valachil Page 34


Computer Programming Laboratory 2018-2019

sum = sum + *(ptr + i); // sum of all elements in an array

printf ("Sum=%f\n", sum);

mean = sum / n; //calculating mean

printf ("Mean is %f\n", mean);

for (i = 0; i < n; i++)


{
diff = *(ptr + i) - mean;

sum1 = sum1 + pow (diff, 2);


}
var = sum1 / n; //calculating variance

printf ("Var is %f\n", var);

sd = sqrt (var); //calculating standard deviation

printf("Standard deviation is %f\n", sd);

Output1: Run the following commands in your terminal:

cc pointer.c -lm

./a.out

Enter the value of n : 5

Enter the array elements

1 2 3 4 5

Sum=15.000000

Mean is 3.000000

Var is 2.000001

Standard deviation is 1.414214

Dept. of CSE, SIT, Valachil Page 35


Computer Programming Laboratory 2018-2019

Program 15: Binary to Decimal Conversion


Implement Recursive functions for Binary to Decimal Conversion.

***************************************************************************
File : binary.c

Description: Program to convert binary to decimal.

Compiler: cc compiler, Ubuntu 14.04

***************************************************************************
/* Program to implement Binary to decimal */

#include <stdio.h>

#include <math.h>

int convertBinaryToDecimal(long n);

int main()

long n;

printf("Enter a binary number: ");

scanf("%lld", &n);

printf("%lld in binary = %d in decimal", n, convertBinaryToDecimal(n));

return 0;

int convertBinaryToDecimal(long n)

int decimalNumber = 0, i = 0, remainder;

while (n!=0)

remainder = n%10;

Dept. of CSE, SIT, Valachil Page 36


Computer Programming Laboratory 2018-2019

n /= 10;

decimalNumber += remainder*pow(2,i);

++i;

return decimalNumber;

Output1: Run the following commands in your terminal:

cc binary.c

./a.out

Enter a binary number: 1110


1110 in binary = 14 in decimal

Output2:
Enter a binary number: 101
101 in binary = 5 in decimal

Dept. of CSE, SIT, Valachil Page 37

You might also like