,assignment of PC Lab, Assignment-6, AJU-210692
,assignment of PC Lab, Assignment-6, AJU-210692
,assignment of PC Lab, Assignment-6, AJU-210692
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;
scanf("%d",&circle_radius);
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;
c = cube(num);
return 0;
}
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 main()
{
int num;
printf (" Enter a number to display the absolute value: ");
scanf ("%d", &num);
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;
}
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
return 0;
}
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;
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;
}
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>
// return result
return sum;
}
int main()
{
// declare variables
float number1, number2, result;
// take input
printf("Enter two number: ");
scanf("%f %f",&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
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:-
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
return 0;
}
4.Division
Enter your choice: 1 10.00 + 5.00 = 15.00
// 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;
return 0;
}
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 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);
}