05/02/2021
CSI0205 – Structured Programming
PRACTICAL 1A
Aim - Implement an arithmetic calculator (supporting 4 basic operations) using User defined functions.
There has to be separate functions for each of the operations. The program is to be menu driven. The
operations should take place as per the user’s choice.
Methodology followed -
#include <stdio.h>
//Function Declaration
int add(int, int);
int sub(int, int);
int mul(int, int);
float div(int, int);
//Main function
void main()
{
int x, y, n, ans1, ans2, ans3;
float ans4;
printf("Enter x : ");
scanf("%d", &x);
printf("Enter y : ");
scanf("%d", &y);
printf("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division");
printf ("\n\nEnter your choice : ");
scanf("%d", &n);
switch(n)
{
case 1 : printf("\nSum is %d\n", add(x,y)); break;
case 2 : printf("\nDifference is %d\n", sub(x,y)); break;
case 3 : printf("\nProduct is %d\n", mul(x,y)); break;
case 4 : printf("\nDivision is %f\n", div(x,y)); break;
default : printf("\nInvalid choice\n");
exit (0);
}
}
//Funct ion Defination
int add(int a, int b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
int mul(int a, int b)
{
return a*b;
}
float div(int a, int b)
{
return (float)a/b;
}
Theoretical aspects – I made this calculator using switch case.
Input / Output –
Conclusion - Doing this code, I learnt the concepts of user defined functions while implementing it for a
menu driven calculator taking out 4 basic tasks.
PRACTICAL 1B
Aim - Consider a currency system in which there are notes of seven denominations, namely Rs.1, Rs.2,
Rs.5, Rs.10, Rs.20, Rs.50 and Rs.100. A sum of Rs. N is to be entered as an input. Write a function to
compute the smallest number of notes that will combine to give Rs. N. (using loop)
Methodology followed -
#include <stdio.h>
//Function Declaration
int notes(int);
//Main function
void main()
{
int n, i, den[7]={100,50,20,10,5,2,1}, num[7];
printf("Enter a sum of Rs. N : ");
scanf("%d", &n);
printf("\nTotal currency notes required : %d\n", notes(n));
}
//Function Defination
int notes(int a)
{
int i, d, sum = 0, den[7]={100,50,20,10,5,2,1};
for(i=0;i<=6;i++)
{
d = a/den[i];
a = a%den[i];
printf("\nRs%d notes required : %d", den[i], d);
sum = sum + d;
}
return sum;
}
Theoretical aspects – Bifurcated total amount into factors of different currency notes.
Input / Output –
Conclusion - Doing this code, we practiced the concepts of user defined functions with using loop in it and
simplifying amount into currency notes.
PRACTICAL 1C
Aim - Write a program to check whether a given number is palindrome or not using User Defined Functions
Methodology followed -
#include <stdio.h>
//Function Declaration
int palindrome(int);
//Main function
void main()
{
int n, ans;
printf("Enter any : ");
scanf("%d", &n);
ans=palindrome(n);
if(ans==1)
printf("%d is a palindrome number\n", n);
else
printf("%d is not a palindrome number\n", n);
}
//Function Definition
int palindrome(int a)
{
int temp, rev=0, d;
temp=a;
while(a>0)
{
d=a%10;
rev=rev*10 + d;
a/=10;
}
if(rev==temp)
return 1;
else
return 0;
}
Theoretical aspects – I reversed the digits of the number and compared it with the original number.
Input / Output –
Conclusion - Doing this code, we practiced the concepts of user defined functions and revised the
condition of palindrome number.
PRACTICAL 1D
Aim - Write a program to check whether a given number is Armstrong or not using User Defined Function.
Methodology followed -
#include <stdio.h>
#include <math.h>
//Function Declaration
int arm(int);
//Main function
void main()
{
int n;
printf("Enter number to be checked for Armstrong number : ");
scanf("%d", &n);
arm(n);
}
//Function Definition
int arm(int a)
{
int temp=a, save=a, sum=0, l, d;
while(save>0)
{
save=save/10;
l++;
}
while(a>0)
{
d=a%10;
sum=sum+pow(d,l);
a=a/10;
}
if(temp==sum)
printf("%d is an Armstrong number\n", temp);
else
printf("%d is not a Armstrong number\n", temp);
}
Theoretical aspects - I counted the number of digits in the number and then applied the logic.
Input / Output –
Conclusion - Doing this code, we practiced the concepts of user defined functions and revised the
condition of Armstrong number.