Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

C Programming for Problem Solving Dept.

of CSE, AIT-Bangalore

C PROGRAMMING LABORATORY
[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2019-20]

Semester : I/II I.A. Marks : 40


Course Code : 18CPL17/27 Exam Marks : 60
Total Hours / Week (L : T : P) : 0:0:2 Exam Hours : 03

Course Learning Objectives:


This course (18CPL17/27) will enable students to:
 Write flowcharts, algorithms and programs.
 Familiarize the processes of debugging and execution.
 Implement basics of C programming language.
 Illustrate solutions to the laboratory programs.

Descriptions (if any):


 The laboratory should be preceded or followed by a tutorial to explain the approach or algorithm being
implemented or implemented for the problems given.
 Note that experiment 1 is mandatory and written in the journal.
 Questions related with experiment 1, need to be asked during viva-voce for all experiments.
 Every experiment should have algorithm and flowchart be written before writing the program.
 Code should be traced using minimum two test cases which should be recorded.
 It is preferred to implement using Linux and GCC.

Laboratory Programs:
1. Familiarization with computer hardware and programming environment, concept
of naming the program files, storing, compilation, execution and debugging, taking any
simple C- code.
PART A
2. 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)
3. Develop a program to compute the roots of a quadratic equation by accepting the
coefficients. Print appropriate messages.

© Copyright Acharya Institutes Page 1


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

4. Develop a program to find the reverse of a positive integer and check for palindrome or
not. Display appropriate messages.
5. 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.
6. Introduce 1D Array manipulation and implement Binary search.
7. Implement using functions to check whether the given number is prime and display
appropriate messages. (No built-in math function)

PART B
8. Develop a program to introduce 2D Array manipulation and implement Matrix
multiplication and ensure the rules of multiplication are checked.
9. 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.
10. Write functions to implement string operations such as compare, concatenate, string
length. Convince the parameter passing techniques.
11. Develop a program to sort the given set of N numbers using Bubble sort.
12. 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).
13. Implement structures to read, write and compute average- marks and the students scoring
above and below the average marks for a class of N students.
14. Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
15. Implement Recursive functions for Binary to Decimal Conversion.

© Copyright Acharya Institutes Page 2


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Laboratory Outcomes:
The student should be able to:
 Write algorithms, flowcharts and program for simple problems.
 Correct syntax and logical errors to execute a program.
 Write iterative and wherever possible recursive programs.
 Demonstrate use of functions, arrays, strings, structures and pointers in problem solving.

Conduct of Practical Examination:


 All laboratory experiments, excluding the first, are to be included for practical
examination.
 Experiment distribution
o For questions having only one part: Students are allowed to pick one experiment
from the lot and are given equal opportunity.
o For questions having part A and B: Students are allowed to pick one experiment
from part A and one experiment from part B and are given equal opportunity.
 Strictly follow the instructions as printed on the cover page of answer script for breakup of
marks
 Change of experiment is allowed only once and marks allotted for procedure part to be
made zero.
 Marks Distribution (Subjected to change in accordance with university regulations)
a) For questions having only one part -
Procedure + Execution + Viva-Voce: 15+70+15 =100 Marks
b) For questions having part A and B
i. Part A - Procedure + Execution + Viva = 4 + 21 +5 = 30 Marks
ii. Part B - Procedure + Execution + Viva =10 + 49+ 11= 70 Marks

© Copyright Acharya Institutes Page 3


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

PART-A

2. 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)

#include<stdio.h>

void main()

char op;

float a, b, res;

printf("Enter the operator \n");

scanf("%c",&op);

printf("Enter the two operands \n");

scanf("%f%f",&a,&b);

switch(op)

case '+' : res = a + b;

break;

case '-' : res = a - b;

break;

case '*' : res = a * b;

break;

case '/' : if( b != 0)

res = a / b;

else

© Copyright Acharya Institutes Page 4


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

printf("Division not possible\n");

exit(0);

break;

default : printf("invalid operator\n");

exit(0);

printf(" Result = %f \n", res);

Output:
1. Enter an Operator :
+
Enter two Operands:
20
23
Result =43.000000

2. Enter an Operator:
-
Enter two Operands:
23
20
Result = 3.000000

3. Enter an Operator:
*
Enter two Operands:
3
2
Result =6.000000

4. Enter an Operator:
/
Enter two Operands:
6
2

© Copyright Acharya Institutes Page 5


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Result = 3.0000
5. Enter an Operator :
!
Enter two Operands:
6
2
Invalid Operator
6. Enter an Operator :
/
Enter two Operands:
2
0
Division not possible

© Copyright Acharya Institutes Page 6


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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


coefficients. Print appropriate messages.
#include<stdio.h>
#include<math.h>
void main( )
{
float a, b, c, disc, root1, root2, x, y;
printf("Enter the coefficients a,b and c of the quadratic equation \n");
scanf("%f%f%f",&a, &b, &c);
disc = b * b - 4 * a * c;
if(disc == 0)
{
printf(" Roots are real and Equal\n");
root1 = root2 = - b / (2 * a);
printf(" Root1 = %f and Root2 = %f\n", root1, root2);
}
else if(disc > 0)
{
printf(" Roots are real and Distinct\n");
root1 = ( - b + sqrt(disc) ) / (2 * a);
root2 = ( - b - sqrt(disc) ) / (2 * a);
printf(" Root1 = %f and Root2 = %f\n", root1, root2);
}
else if(disc < 0)
{
printf(" Roots are Complex and Distinct\n");
x = - b / (2 * a);
y = sqrt(fabs(disc) ) / (2 * a);
printf(" Root1 = %f + i %f \n", x, y);
printf(" Root2 = %f - i %f \n", x, y);
}
return 0;
}

© Copyright Acharya Institutes Page 7


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Output
1. Enter the coefficients a,b and c of the quadratic equation
111
Roots are Complex and Distinct
Root1=0.5000000+i0.866025
Root2=0.5000000-i0.866025

2. Enter the coefficients a,b and c of the quadratic equation


121
Roots are Real and Equal
Root1=-1.000000 and Root2=-1.000000

3. Enter the coefficients a,b and c of the quadratic equation


142
Roots are Real and distinct
Root1=-0.585786 and Root2=-3.414214

© Copyright Acharya Institutes Page 8


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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

#include<stdio.h>
void main( )
{
int n, num, rev, digit,

printf("Enter the number\n");


scanf("%d", &n);

num = n;
rev=0;

while(num!=0)
{
digit = num%10;
rev = rev*10 + digit;
num = num /10;
}

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

if(n == rev)
printf("%d is a palindrome\n", n);
else
printf("%d is not a palindrome\n", n);
return 0;
}

Output
1.Enter the number:
1221
The reverse of 1221 is 1221
1221 is a palindrome
2. Enter the number:
123
The reverse of 123 is 321
123 is not a palindrome

© Copyright Acharya Institutes Page 9


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

5. 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.

#include<stdio.h>
void main( )
{
char name[20];
int units;
float charges, subcharge;
printf("Enter the name of the customer:\n");
scanf("%s", name);
printf("Enter the total number of units consumed:\n");
scanf("%d", &units);
if(units < 0)
{
printf("Invalid input\n");
}
else if(units == 0)
{
charges = 100;
}
else if(units > 0 and units <=200)
{
charges = 100 + units*0.80;
}
else if(units > 200 and units <=300)
{
charges = 100 + 200 * 0.80 + (units - 200) * 0.90;
}
© Copyright Acharya Institutes Page 10
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

else if(units > 300)


{
charges = 100 + 200 * 0.80 + 100 * 0.90 + (units - 300) * 1.00;
}

if(charges > 400)


{
subcharge = charges * 0.15;
charges = charges + subcharge;
}
printf("User %s need to pay %f rupees for electricity Bill”, name, charges );
return 0;
}
Output:
1.Enter the name of the customer:
Rani
Enter the total number of units consumed:
0
User Rani need to pay 100.000000 rupees for electricity Bill

2.Enter the name of the customer :


Ayush
Enter the total number of units consumed:
200
User Ayush need to pay 260.000000 rupees for electricity Bill

3.Enter the name of the customer:


Raju
Enter the total number of units consumed:
300
User Raju need to pay 350.000000 rupees for electricity Bill

4.Enter the name of the customer


Manju
Enter the total number of units consumed:
400
User Manju need to pay 517.500000 rupees for electricity Bill

5.Enter the name of the customer


Suraj

© Copyright Acharya Institutes Page 11


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Enter the total number of units consumed:


425
User Suraj need to pay 546.250000 rupees for electricity Bill

© Copyright Acharya Institutes Page 12


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

6. Introduce 1D Array manipulation and implement Binary search.

#include<stdio.h>
#include<stdlib.h>
void main( )
{
int n, a[20], key, low, high, mid ;
printf(“Enter the size of an array:\n”);
scanf(“%d”, &n);
printf(“Enter the elements to an array:\n”);
for( i = 0; i < n ; i ++ )
{
scanf(“%d”, &a[i] );
}
printf(“Enter the key element to be searched:\n”);
scanf(“%d”, &key);
low = 0;
high = n-1;
while( low <= high )
{
mid = (low + high) / 2;
if(key == a[mid])
{
printf(“Element found at location: %d \n”, mid + 1 );
exit(0);
}
else if(key < a[mid])
high = mid – 1;
else
low = mid +1;
}
printf(“Key not found \n” );
}

Output:
1.Entre the size of array: 5

Enter the elements of an array: 10 20 30 40 50

Enter the key element to be searched: 30

Element found at location 3

2. Entre the size of array: 5

© Copyright Acharya Institutes Page 13


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Enter the elements of an array: 10 20 30 40 50

Enter the key element to be searched: 60

Key not found

© Copyright Acharya Institutes Page 14


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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

#include<stdio.h>
void checkprime( int n )
{
int i;

if( n==0 || n==1)


{
printf("Number is neither prime nor composite\n");
exit(0);
}

for(i=2; i<=n/2; i++)


{
if( n % i == 0 )
{
printf("Number is not prime\n");
exit(0);
}
}
printf("Number is prime\n");

void main( )
{
int n;

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


scanf("%d", &n);

checkprime( n );
}

Output:
1.Enter the value of n: 5

Number is prime

2. Enter the value of n: 4

Number is not prime

© Copyright Acharya Institutes Page 15


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

8. Develop a program to introduce 2D Array manipulation and implement Matrix


multiplication and ensure the rules of multiplication are checked.

void main( )
{
int m, n, p, q, i, j, a[20] [20], b[20][20], c[20][20];

printf(“Enter the number of rows and columns of matrix A:\n”);


scanf(“%d %d”, &m,&n);

printf(“Enter the number of rows and columns of matrix B:\n”);


scanf(“%d%d”, &p,&q);

if(n!=p)
{
printf("Matrix multiplication is not possible\n");
exit(0);
}

printf(“Enter the elements of matrix A:\n”);


for( i = 0; i < m ; i ++ )
{

for( j = 0; j < n ; j ++ )
{
scanf(“%d”, &a[i][j] );
}
}

printf(“Enter the elements of matrix B:\n”);


for( i = 0; i < p ; i ++ )
{
for( j = 0; j < q ; j ++ )
{
scanf(“%d”, &b[i][j] );
}
}

printf(“The elements of matrix A are:\n”);


for( i = 0; i < m ; i ++ )
{
for( j = 0; j < b ; j ++ )
{
printf(“%d \t”,a[i][j] );
}
printf(“\n”);

© Copyright Acharya Institutes Page 16


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

printf(“The elements of matrix B are:\n”);


for( i = 0; i < p ; i ++ )
{
for( j = 0; j < q ; j ++ )
{
printf(“%d \t”,b[i][j] );
}
printf(“\n”);
}

// To multilpy the two matrices


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];
}
}
}

printf(“The resultant matrix is\n”);


for( i = 0; i < m ; i ++ )
{
for( j = 0; j < q ; j ++ )
{
printf(“%d \t”,c[i][j] );
}
printf(“\n”);
}

Output:
1.Enter the number of rows and columns of matrix A:

23

Enter the number of rows and columns of matrix B:

22

Matrix multiplication is not possible

© Copyright Acharya Institutes Page 17


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

2. Enter the number of rows and columns of matrix A:

22

Enter the number of rows and columns of matrix B:

22

Enter the elements of matrix A:

1111

Enter the elements of matrix B:

1111

The elements of matrix A are:

11

11

The elements of matrix B are:

11

11

The resultant matrix is:

22

22

© Copyright Acharya Institutes Page 18


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

9. 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.

#include<stdio.h>
#define PI 3.142
void main( )

{
int i, degree;

float x, sum=0, term, num, deno;


printf(“Enter the value in degrees:\n”);
scanf(“%d”, &degree);
x=degree * (PI / 180);
num= x;
deno=1;
i = 2;

do
{
term=num/deno;

num=-num*x*x;

sum=sum+term;

deno = deno*i*(i+1));

i = i + 2;

} while(fabs(term)>=0.00001);

printf(“The sine of %d is: %f\n”, degree, sum);

printf(“The sine function of %d is: %f\n” , degree, sin(x));

© Copyright Acharya Institutes Page 19


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Output:
1. Enter the values in degrees:90

The sine of 90 is 1.000000

The sin function of 90 is 1.000000

2. Enter the values in degrees:45

The sine of 45 is 0.706825

The sin function of 45 is 0.706825

© Copyright Acharya Institutes Page 20


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void strcopy(char str1[30],char tr2[30]);
void strcomp(char str1[30],char str2[30]);
void strcon(char str1[30],char str2[30]);
void strlength(char str1[30]);
void main()
{
int ch;
char str1[30,str2[30];
printf(“Enter your choice:\n1: For string copy\n2:For string compare\n3:For
string concatenation\n4:For string length:\n”);
scanf(“%d”,&ch);

switch(ch)
{
case 1: strcopy(str1,str2);

break;
case 2: strcomp(str1,str2);
break;
case 3: strcon(str1,str1);
break;
case 4: strlength(str1); break;
default: printf(“Invalid choice, try with valid input”);
exit(0);
}
}
void strcopy(char str1[], char str2[])
{
int i;

© Copyright Acharya Institutes Page 21


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

printf(“Enter the string to copy:\n”);

scanf(“%s”,str2);

i=0;
while(str2[i]!=’\0’)
{
str1[i]=str2[i];

i++;
}
str1[i]=’\0’;
printf(“After copying string1 and string1 are %s %s”, str1,str2);
}
void strcomp(char str1[], char str2[])
{
int i;
printf(“Enter the string1:\n”);

scanf(“%s”,str1);

printf(“Enter the string2:\n”);

scanf(“%s”,str2);

for(i=0;str1[i]!=’\0’;i++);
{
if(str1[i]!=str2[i])
{
printf(“Strings are not equal\n”); exit(0);
}
}
printf(“The string are equal\n”);
}
void strcon(char str1[], char str2[])
{
int i,len=0;
printf(“Enter the string1:\n”);
© Copyright Acharya Institutes Page 22
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

scanf(“%s”,str1);

printf(“Enter the string2:\n”);

scanf(“%s”,str2);

len=strlen(str1);

for(i=0;str2[i]!=’\0’;i++)

str1[len+i]=str2[i];

str1[len+i]=’\0’;
printf(“After concatenation String 2 to string 1 is: %s\n”,str1);
}
void strlength(char str1[])
{
int i,len;
printf(“Enter the string1:\n”);
scanf(“%s”,str1);
for(i=0;str1[i]!=’\0’;i++)
len++;
printf(“The length of the given string is: %d\n”,len);
}

Output:
1.Enter your choice:

For string copy:


For string compare:
For string concatenation:
For string length:
1
Enter the string to copy: Raju
After copying string1 and string1 are Raju Raju

© Copyright Acharya Institutes Page 23


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

2.Enter your choice:


For string copy:
For string compare:
For string concatenation:

For string length:


2
Enter the string1: Raju
Enter the string2: Raju
Strings are equal

3.Enter your choice:


For string copy:
For string compare:
For string concatenation:

For string length:


3
Enter the string1:
Acharya
Enter the string2:
Institute

After concatenation String 2 to string 1 is:


AcharyaInstitute

4.Enter your choice:


For string copy:
For string compare:
For string concatenation:

© Copyright Acharya Institutes Page 24


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

For string length:


4
Enter the string1: Acharya
The length of the given string is: 7

© Copyright Acharya Institutes Page 25


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

11. Develop a program to sort the given set of N numbers using Bubble sort.

#include<stdio.h>
void main( )
{
int n, i, j, temp, a[100];
printf("Enter the value for n:\n");
scanf("%d",&n);
printf("Enter the array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("The array elements before sorting are: \n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);

for(j=1; j<n; j++)


{
for(i=0 ; i< n-j ; i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}

printf("The elements after sorting are: \n");


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

© Copyright Acharya Institutes Page 26


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

printf("%d\n",a[i]);
return 0;
}

Output:
Enter the value for n: 5

Enter the array elements:

10 90 40 60 20

The array elements before sorting are:

10 90 40 60 20

The elements after sorting are:

10 20 40 60 90

© Copyright Acharya Institutes Page 27


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

12. 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).

#include<stdio.h>
void main( )
{
float n, s, d;

printf(“Enter the value of n:\n”);


scanf(“%f”, &n);

s=1;
while(s*s<=1)
{
s=s+1;
}
s = s – 1;
d=0.0001;
while(s*s<=1)
{
s=s+d;
}
s = s – d;

printf(“Square root of a given number %f is: %f\”, n, s);


printf(“Using Built in function Square root of a given number %f is: %f\”, n, sqrt(n));

© Copyright Acharya Institutes Page 28


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

OR

#include<stdio.h>
main( )
{
float n, x1, x2;

printf("Enter the number\n");


scanf("%f", &n);

x2 = 0;
x1 = (n + (n/n)) / 2;

while(x1 != x2)
{
x2 = (x1 + (n/x1)) / 2;
x1 = (x2 + (n/x2)) / 2;
}

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


printf(“Using Built in function Square root of a given number %f is %f\”, n, sqrt(n));

return 0;
}

Output:
Enter the value of n: 16

Square root of a given number 16 is: 4

Using Built in function Square root of a given number 16 is: 4

© Copyright Acharya Institutes Page 29


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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

#include<stdio.h>

struct student
{
char name[20];
int rollno;
int m1, m2, m3;
int avg;
};

void main( )
{
struct student s[100];
int n, i, sum=0, class_avg =0;

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


scanf("%d", &n);
// To read the details of 'n' students
printf("Enter the student details\n");
for(i=0; i<n; i++)
{
printf("Enter the name\n");
scanf("%s", s[i]. name);
printf("Enter the rollno\n");
scanf("%d", &s[i]. rollno);
printf("Enter the marks in three tests\n");
scanf("%d%d%d", &s[i].m1, &s[i].m2, &s[i].m3);
}
// To compute the average marks of each student
for(i=0; i<n; i++)
{
s[i].avg = ( s[i].m1 + s[i].m2 + s[i].m3) / 3;
}

// To compute the average marks of class


for(i=0; i<n; i++)
{
sum = sum + s[i].avg;
}

© Copyright Acharya Institutes Page 30


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

class_avg = sum / n;

printf("The average marks of class is %d\n", class_avg);

// To print the names of students scoring above average


printf("Above average students\n");
for(i=0; i<n; i++)
{
if( s[i].avg >= class_avg)
{
printf("%d\t", s[i].rollno);
printf("%s\n", s[i].name);
}
}

// To print the names of students scoring below average


printf("Below average students\n");
for(i=0; i<n; i++)
{
if( s[i].avg < class_avg)
{
printf("%d\t", s[i].rollno);
printf("%s\n", s[i].name);
}
}
}

Output:
1. Enter the number of students: 3

Enter the student details

Enter the name

Raju

Enter the roll no

Enter the marks in three tests

20 30 40

© Copyright Acharya Institutes Page 31


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

Enter the name

Rani

Enter the roll no

Enter the marks in three tests

67 89 45

Enter the name

Bhoomi

Enter the roll no

Enter the marks in three tests

90 45 66

The average marks of the class is 54

Above average students

2 Rani

3 Bhoomi

Below average students

1 Raju

© Copyright Acharya Institutes Page 32


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

14. Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of 'n' real numbers.

#include<stdio.h>

void main( )
{
int n, i,temp;
float a[20], sum, mean, var, sd;
float *p;

printf(“Enter the total number of elements:\n”);


scanf(“%d”, &n);

printf(“Enter the elements to an array:\n”);


for( i = 0; i < n ; i ++ )
{
scanf(“%f”, &a[i] );
}

p = a; // p = &a[0];

// To find sum and mean


sum=0.0;
for( i = 0; i < n ; i ++ )
{
sum = sum + *(p+i);
}
mean = sum/n;

// To find variance
temp=0.0;
for( i = 0; i < n ; i ++ )
{
temp = temp + (*(p+i) - mean) * (*(p+i) - mean);
}

© Copyright Acharya Institutes Page 33


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

var = temp/n;

// To find standard deviation


sd = sqrt(var);

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


printf("Mean = %f\n", mean);
printf("Variance = %f\n", var);
printf("Standard deviation = %f\n", sd);
return 0;
}

Output:
Enter the total number of elements:5

Enter the elements to an array:

12345

Sum=15.000000

Mean=3.000000

Variance=2.000000

Standard deviation=1.414214

© Copyright Acharya Institutes Page 34


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

15. Implement Recursive functions for Binary to Decimal Conversion.

#include<stdio.h>
int binarytodecimal(int n)

{
if(n == 0)
return 0;
else
return( n%10 + binarytodecimal (n/10) * 2 );
}

main( )
{
int decnum, binnum;

printf(“enter the binary number : only 0s and 1s:”);

scanf(“%d”,&binnum);

decnum= binarytodecimal (binnum);

printf(“The decimal equivalent is:%d”, decnum);

return 0;
}

Output:
1. Enter the binary number: only 0s and 1s:

1010

The decimal equivalent is: 10

2. Enter the binary number: only 0s and 1s:

0011

The decimal equivalent is: 3

© Copyright Acharya Institutes Page 35


C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

© Copyright Acharya Institutes Page 36

You might also like