,assignment of PC Lab, Assignment-6, AJU-210692

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

1

Name- Abishekh Kumar Singh


Enrollment No. – AJU/210975
Course – BCA , Section – B

Programming in C Lab Assignment


Assignment -6
Question : 1. Write a C program using a user-defined function to find the area of a rectangle.
Take input from the user.

Formula:-
The area of a rectangle is length * width.
Answer :
#include <stdio.h>

int main(){
float length, width, area;
printf("Enter length of Rectangle\n");
scanf("%f", &length);
printf("Enter width of Rectangle\n");
scanf("%f", &width);
/* Area of Rectangle = Length X Width */
area = length * width;
printf("Area of Rectangle : %0.4f\n", area);

return 0;
}

Question : 2. Write a C program with a user-defined function to find the area of a rectangle.
Take input from the user.
2

Formula:-
Area of circle = 3.14*r*r;
3.14 is the value of PI.
Answer :
#include <stdio.h>
int main()
{
int circle_radius;
float PI_VALUE=3.14, circle_area, circle_circumf;

printf("\nEnter radius of circle: ");

scanf("%d",&circle_radius);

circle_area = PI_VALUE * circle_radius * circle_radius;


printf("\nArea of circle is: %f",circle_area);

circle_circumf = 2 * PI_VALUE * circle_radius;


printf("\nCircumference of circle is: %f",circle_circumf);

return(0);
}

Question : 3. Write a program to find the cube of a given number by defining a user-defined
function. Take input from the user.
Answer :
#include <stdio.h>
double cube(double num);

int main()
{
3

int num;
double c;

printf("Enter any number: ");


scanf("%d", &num);

c = cube(num);

printf("Cube of %d is %.2f", num, c);

return 0;
}

double cube(double num)


{
return (num * num * num);
}

Question : 4. Write a C program to find the absolute value of a given number without using
abs() function.
Answer :
#include <stdio.h>
#include <stdlib.h> // use stdlib.h header file to use abs() function.

int getAbsolute (int num)


{
/* if the passed value (num) is less than 0 (zero),
the number multiplied by (-1) to return an absolute value. */
if (num < 0)
{
num = ( -1 ) * num; // given negative number multiplied by (-1)
printf (" The absolute value is: %d", num);
}
else
4

printf (" The absolute value is: %d", num);


}
return num;
}

int main()
{
int num;
printf (" Enter a number to display the absolute value: ");
scanf ("%d", &num);

// call the functon


getAbsolute(num);
return 0;
}

Question :5. In the C program write a user-defined function to find minimum and maximum
in given three numbers. Take three numbers from the user.
Answer :
5

#include <stdio.h>
int max(int a, int b, int c){
int num;
if (a>b && b>c)
return num=a;
else if (b>a && b>c)
return num=b;
else
return num=c;
}
int min(int a, int b, int c){
int num;
if (a<b && b<c)
return num=a;
else if (b<a && b<c)
return num=b;
else
return num=c;
}
int main()
{
int a,b,c,maximum,minimum;
printf("Enter Numbers:\n");
scanf("%d %d %d",&a,&b,&c);
maximum= max(a,b,c);
minimum=min(a,b,c);
printf("Max No.=%d\nMin No.=%d\n",maximum,minimum);
return 0;
}
6

Question : 6. Write a C program to find the square of a given number using user-defined
functions. Define three functions, take input from the user using one function, calculate a
square in another function, and display the result in another function.
Answer :
#include<stdio.h>
int getNumber();
int square(int n);
void display(int n, int sqr);

int main()
{
int num = getNumber();
int result = square(num);
display(num, result);
return 0;
}

int getNumber()
{
int n;
printf("Enter an Integer number: ");
scanf("%d",&n);
return n;
}

int square(int n)
{
return n*n;
}

void display(int n, int sqr)


{
7

printf("Square of %d = %d\n", n, sqr);


}

Question : 7. Write a program that extracts and adds the two least significant digits of any
number. Define three functions addTwoDigits(), lastDigit() and secondLastDigit(). The
main function should call addTwoDigits(). The lastDigit() and secondLastDigit() functions
should be called from function addTwoDigits().
Enter an integer number: 52368
Sum of last two digits is = 14
Answer :
#include<stdio.h>

// function declarations
int addTwoDigits(int n);
int lastDigit(int n);
int secondLastDigit(int n);

int main()
{
int number;
8

printf("Enter an integer number: ");


scanf("%d",&number);

int sum = addTwoDigits(number);


printf("Sum of last two digits is = %d",sum);

return 0;
}

// function for adding last two digit


int addTwoDigits(int n)
{
int result = lastDigit(n) + secondLastDigit(n);
return result;
}

// function for finding last digit


int lastDigit(int n)
{
return (n%10);
}

// function for finding second last digit


int secondLastDigit(int n)
{
return ((n/10)%10);
}
9

Question : 8. Write a C program to check whether a number can be expressed as the sum of
two prime numbers or not.
Answer :
#include<stdio.h>
int check(int a);
int main()
{
int n, j, flag;

printf("Enter a number: ");


scanf("%d",&n);

// divide number into two parts j and (n-j)


// such as n = j + (n-j)
for(j=2;j<=n/2;j++)
{
// condition for j to be a prime number
if(check(j)==0)
{
// condition for n-j to be a prime number
if(check(n-j)==0)
{
// n= j + (n-j)
printf(" %d + %d = %d\n", j, n-j, n);
flag=0;
}
}
}

if(flag==1)
printf("Can't be expressed as sum of two prime Numbers.\n");
10

return 0;
}

int check(int a)
{
int i, flag=0;

for(i=2;i<=a/2;i++)
{
if(a%i==0)
flag++;
}

if(flag==0)
return 0;
else
return 1;
}

Question : 9. Write a C program to calculate addition of two floating-point numbers using


11

functions. Write three functions:- input(), addition(), display(). Take input from user in user-
defined function input() and return back to the main function. Add numbers in addition()
function and return back to main function. Print the result using display() function.
Answer :
#include<stdio.h>

// function to find sum of two numbers


float addition(float num1, float num2)
{
// declare variable
float sum;

// calculate sum value


sum = num1 + num2;

// return result
return sum;
}

int main()
{
// declare variables
float number1, number2, result;

// take input
printf("Enter two number: ");
scanf("%f %f",&number1, &number2);

// find addition of two numbers


result = addition(number1, number2);

// display result
printf("%.2f + %.2f = %.2f\n",
number1, number2, result);

return 0;
}
12

Question : 10. Write a C program to find the sum of n numbers using functions. Define three
functions input(), sum(), and display(). Take input from the user in the input() function and
return it to the main function. Find the sum of n numbers in the sum() function, return the
sum value to the main function. Print result using display() function.
Answer :
#include<stdio.h>

int sum(int n)
{
int add = 0;
for(int i=1; i<=n; i++)
{
add += i;
}
return add;
}

int main()
{
13

int range, result;


printf("Upto which number you want to find sum: ");
scanf("%d", &range);
result = sum(range);
printf("1+2+3+….+%d+%d = %d",range-1, range, result);
}

Question : 11. Write a C program to find the largest among the three numbers. Take input
from the user. Write a program in such a way that the input numbers may be integer, float,
or both. Output for the different test-cases:-

Enter three numbers: 12.5 8 6.9


Largest number = 12.50

Enter three numbers: 3 5.9 6.0


Largest number = 6.00
Answer :
#include <stdio.h>
int main()
{
int A, B, C;

printf("Enter three numbers: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

else if (B >= A && B >= C)


printf("%d is the largest number.", B);

else
printf("%d is the largest number.", C);
14

return 0;
}

Question :12. Write a C program to find the largest of three numbers. Define three functions
input(), large() and display() to perform the operations.Output for the different test-cases:-

Enter number: 10
Enter number: 12
Enter number: 9
Largest number = 12.00

Enter number: 9.5


Enter number: 4.51
Enter number: 2.99
Largest number = 9.50
Answer :
int main()
{
int A, B, C;

printf("Enter the numbers A, B and C: ");


15

scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

if (B >= A && B >= C)


printf("%d is the largest number.", B);

if (C >= A && C >= B)


printf("%d is the largest number.", C);

return 0;
}

Question : 13. Write a menu-driven program to find addition, subtraction, multiplication,


and division of two numbers using the user defined functions and the program should
accept choice from the user repeatedly.
Enter first number: 10
Enter second number: 5

Which operation you want to perform,


1.Addition
2.Subtraction
3.Multiplication
16

4.Division
Enter your choice: 1 10.00 + 5.00 = 15.00

Do you want to continue (y/n): y


***************************************
Enter first number: 15
Enter second number: 10
Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 2 15.00 – 10.00 = 5.00

Do you want to continue (y/n): y


Answer :
#include<stdio.h>

// functions declaration
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);

// main function
int main()
{
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("%d + %d = %d\n", num1, num2, add(num1, num2));


printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
printf("%d / %d = %d\n", num1, num2, divide(num1, num2));

return 0;
}

// function to add two integer numbers


int add(int n1, int n2)
{
int result;
result = n1 + n2;
return result;
17

// function to subtract two integer numbers


int subtract(int n1, int n2)
{
int result;
result = n1 - n2;
return result;
}

// function to multiply two integer numbers


int multiply(int n1, int n2)
{
int result;
result = n1 * n2;
return result;
}

// function to divide two integer numbers


int divide(int n1, int n2)
{
int result;
result = n1 / n2;
return result;
}
18

Question : 14. Write a C program to find GCD of two numbers by defining a user-defined
function.
Answer :
#include <stdio.h>
int main()
{

int n1, n2, i, GCD_Num;


printf ( " Enter any two numbers: \n ");
scanf ( "%d %d", &n1, &n2);
for( i = 1; i <= n1 && i <= n2; ++i)
{
if (n1 % i ==0 && n2 % i == 0)
GCD_Num = i;
}
printf (" GCD of two numbers %d and %d is %d.", n1, n2, GCD_Num);
return 0;
}

Question :15. Wap in C to Reverse a number using Function


Answer :
#include<stdio.h>
Reverse(int n)
19

{
int sum=0;
while (n!=0)
{
sum = sum*10 + n%10;
n /= 10;
}
return sum;
}
void main()
{
int rev, num;
printf("Enter a Positive Number: ");
scanf("%d", &num);
rev = Reverse(num);
printf("The Reverse of given number %d is: %d", num, rev);
}

You might also like