0% found this document useful (0 votes)
8 views

Principles of Programming Using C Laboratory

Uploaded by

kushalnataraj05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Principles of Programming Using C Laboratory

Uploaded by

kushalnataraj05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

C Programming for Problem Solving Dept.

of CSE, AIT-Bangalore

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

COMPUTER PROGRAMMING
LABORATORY
Course Code 22POP13/2 CIE Marks 50
3

Teaching Hours/Week (L: T:P: S) 0:0:2:0 SEE Marks 50


Total Hours of Pedagogy -- Total Marks 100
Credits 01 Exam Hours 03
Course Objectives:
1. Explain problem statements and identify appropriate solutions
2. Demonstrate the use of IDE, C Compiler, and identify and rectify
the syntax andsyntactic errors during programming.
3. Development of algorithms and programs using constructs of C
programming language
4. Reporting the observations
Sl. Practice Programs
No
1 Calculation of Simple Interest,
2 Check whether the given number is even or
3 odd Convert string case
4 Check for the palindrome, prime number, perfect
5 square.Development of linear search algorithm Etc…
PART A-List of problems for which students should develop the program and execute
inthe
Laboratory
1 Simulation of a Simple Calculator.
2 Compute the roots of a quadratic equation by accepting the coefficients.
Printappropriate messages.
3 An electricity board charges the following rates for the use of electricity: for the first
200units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units
Rs 1 perunit. 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 the total
amount is charged.
Write a program to read the name of the user,
the number of units consumed, and print out the charges.
4 Implement Binary Search on Integers / Names.
5 Implement Matrix multiplication and validate the rules of multiplication.

© Copyright Acharya Institutes Page 1


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

6[As per
Compute
Choice sin(x)/cos(x) using (CBCS)
Based Credit System Taylor series
schemeapproximation. Compare
- Effective from the your
academic year 2021-2022]
resultwith the built-in library function. Print both the results with

© Copyright Acharya Institutes Page 2


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
appropriate inferences.

7 Sort the given set of N numbers using Bubble sort.


8 Write functions to implement string operations such as compare,
concatenate,string length. Convince the parameter passing techniques.
9 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.
10 Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers.
11 Implement Recursive functions for Binary to Decimal Conversion.
PART B – Practical Based Learning
A problem statement for each batch is to be generated in consultation with the
co-examiner and the student should develop an algorithm, program and
Execute the program for the given problem with appropriate outputs.

Course Objectives:
1. Explain problem statements and identify appropriate solutions
2. Demonstrate the use of IDE, C Compiler, and identify and rectify the
syntax andsyntactic errors during programming.
3. Development of algorithms and programs using constructs of C programming language
4. Reporting the observations

Course Outcomes:
The student should be able to:
At the end of the course the student will be able to:

CO1: Write the program for the given specifications.


CO2: Simulate the experiments with the given specification.
CO3: Tabulate, Validate the readings and infer the results mathematically.
CO4: Interpret the concepts and results both orally and written.

© Copyright Acharya Institutes Page 3


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
Conduct of Practical Examination:
The practical examinations are to be conducted as per the timetable of the University in batch-
wise with the strength of students not more than 10-15 per batch.

All laboratory experiments (Part A) are to be included for practical examination.

Students can pick one experiment from the questions lot of PART A with equal choice to all
the students in a batch.

For PART B examiners should frame a question for each batch, the student should develop
an algorithm, program, execute and demonstrate the results with appropriate output for the
given problem.

Weightage of marks for PART A is 80% and for PART B is 20% 5.

Change of experiment is allowed only once for part A and 15% Marks allotted to the procedure
part to be made zero. However, PART B question is mandatory (change of question is not
allowed).

Marks distribution: procedure (15%) + execution (70%) + viva voce (15%) PART A
(12+56+12 = 80) AND FOR PART B (3+14+3 = 20)

© Copyright Acharya Institutes Page 4


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
PART-A

1. Simulation of a Simple Calculator.


#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
{
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:
© Copyright Acharya Institutes Page 5
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

20 Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per

© Copyright Acharya Institutes Page 6


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
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
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 7


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

2. 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, real, img;
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");
real = - b / (2 * a);
img = sqrt(fabs(disc) ) / (2 * a);
printf(" Root1 = %f + i %f \n", real, img);
printf(" Root2 = %f - i %f \n", real, img);
}
}

Output
1. Enter the coefficients a,b and c of the quadratic
equation 1 1 1
Roots are Complex and Distinct

© Copyright Acharya Institutes Page 8


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

Root1=0.5000000+i0.866025
Root2=0.5000000-i0.866025

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


equation 1 2 1
Roots are Real and Equal
Root1=-1.000000 and Root2=-1.000000

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


equation 1 4 2
Roots are Real and distinct
Root1=-0.585786 and Root2=-3.414214

3. 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 and units <=200)
{
charges = 100 + units*0.80;
}
else if(units > 200 and units <=300)
{
charges = 100 + 200 * 0.80 + (units - 200) * 0.90;
}
else if(units > 300)

© Copyright Acharya Institutes Page 9


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

{ Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
[As per

© Copyright Acharya Institutes Page


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

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

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
Enter the total number of units consumed:

© Copyright Acharya Institutes Page 11


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

425
User Suraj need to pay 546.250000 rupees for electricity Bill

4. Implement Binary Search on Integers / Names.


#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

© Copyright Acharya Institutes Page 12


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

Enter the key element to be searched: 30


Element found at location 3

2. Entre the size of array: 5


Enter the elements of an array: 10 20 30 40 50
Enter the key element to be searched: 60
Key not found

5. Implement Matrix multiplication and validate the rules of multiplication.


#include<stdio.h>
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] );
}
}

© Copyright Acharya Institutes Page 13


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

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”);
}
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”);
}

© Copyright Acharya Institutes Page 14


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
}

Output:
1. Enter the number of rows and columns of matrix A:
2 3
Enter the number of rows and columns of matrix B:
2 2
Matrix multiplication is not possible
2. Enter the number of rows and columns of matrix A:
2 2
Enter the number of rows and columns of matrix B:
2 2
Enter the elements of matrix A:
1111
Enter the elements of matrix B:
1111
The elements of matrix A are:
1 1
1 1
The elements of matrix B are:
1 1
1 1
The resultant matrix is:
2 2
2 2

6. Compute sin(x)/cos(x) using Taylor series approximation. Compare your resultwith the
built-in library function. Print both the results with appropriate inferences.
#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;

© Copyright Acharya Institutes Page 15


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
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));
}

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

7. 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++)
© Copyright Acharya Institutes Page 16
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

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

© Copyright Acharya Institutes Page 17


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
for(i=0; i<n-1; i++)
{
for(j=0 ; j< n-i-1 ; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The elements after sorting are: \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}

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 18


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

8. 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()
{

char str1[30],str2[30];
printf(“Enter string1 and String2”);
scanf(“%s%s”,str1,str2);
printf(“Enter your choice:\n 1: For string copy\n 2:For string compare\n
3:For string concatenation\n 4: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);
}
}

© Copyright Acharya Institutes Page 19


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

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


{
int i;
for(i=0;str2[i]!=’\0’;i++)
while(str2[i]!=’\0’)
{
str1[i]=str2[i];
i++;
}
str1[i]=’\0’;
printf(“After copying string2%s to string1 are %s”, str1,str2);
}
void strcomp(char str1[], char str2[])
{
int i; 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;
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);
}

© Copyright Acharya Institutes Page 20


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

void strlength(char str1[])


{
int i,len1=0;

for(i=0;str1[i[!=’\0’;i++)
{
len1++;
}
printf(“The length of string1 :%d\n ”,len1);
}

Output:

1. Enter string1 and


String2 Acharya
Institute
Enter your
choice: 1: For
string copy
2: For string comp

© Copyright Acharya Institutes Page 21


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

3: For string concatenation


4: For string length:
1
After copying string1 and string1 are:
Institute
Institute

2. Enter string1 and


String2 Acharya
Acharya
Enter your choice:
1: For string copy
2: For string compare
3: For string
concatenation 4: For
string length:
2
Strings are equal

3. Enter string1 and


String2 Acharya
Institute
Enter your choice:
1: For string copy
2: For string compare
3: For string
concatenation 4: For
string length:
3
After concatenation String 2 to string 1 is: AcharyaInstitute

4. Enter string1 and


String2 Acharya
Institute
Enter your choice:
1: For string copy
2: For string compare
3: For string
concatenation 4: For
string length:
4
© Copyright Acharya Institutes Page 22
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

The length of String1 is: 7

© Copyright Acharya Institutes Page 23


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
The length of String2 is: 9

9. 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 24
C Programming for Problem Solving Dept. of CSE, AIT-Bangalore

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
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:
1
Enter the marks in three tests:
20 30 40
Enter the name:
Rani
Enter the roll no:
2

Enter the marks in three tests:

© Copyright Acharya Institutes Page 25


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]
67 89 45
Enter the name:
Bhoomi
Enter the roll no:
3
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

10. 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);
}

© Copyright Acharya Institutes Page 26


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

mean = sum/n;
// To find variance
temp=0.0;
for( i = 0; i < n ; i ++ )
{
temp = temp + (*(p+i) - mean) * (*(p+i) - mean);
}
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);

}
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

11. 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 );
}
void main( )
{
int decnum, binnum;

© Copyright Acharya Institutes Page 27


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

[As per Choice Based Credit System (CBCS) scheme - Effective from the academic year 2021-2022]

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


scanf(“%d”,&binnum);
decnum= binarytodecimal (binnum);
printf(“The decimal equivalent is:%d”, decnum);

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 28

You might also like