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

ASSIGNMENT-X

Q1. Write a program to swap two number using pointer/call by


reference.
Answer:
#include<stdio.h>
void swap (int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("The order after swappping is %d %d \n",*a,*b);
}
int main()
{
int num1,num2;
printf("Enter the first number ");
scanf("%d",&num1);
printf("Enter the second number ");
scanf("%d",&num2);
printf("The orignal order of numbers is %d %d \n",num1,num2);
swap(&num1,&num2);
return 0;
}

Q2. Write a program to find factorial of number using recursion.


Answer:
//factorial using recursion
#include<stdio.h>
int factorial(int n)
{
if(n>=1)
return n*factorial(n-1);
else
return 1;

}
int main()
{
int num;
printf("Enter the number ");
scanf("%d",&num);
printf("The factorial is %d \n",factorial(num));
return 0;
}

Q3. Write a program to find area of circle, rectangle, square, and triangle
using function.
Answer:
// area of circle rectangle square and triangle
#include <stdio.h>
#include <math.h>
int area;
void square(int s)
{
area = s * s;
printf("The area of the square having side %d is %d \n", s,
area);
}
void rectangle(int l, int b)
{
area = l * b;
printf("The area of the rectangle having length %d and breadth
%d is %d \n", l, b, area);
}
void circle(int r)
{
const float PI = 3.14;
area = PI * r * r;
printf("The area of the circle having radius %d is %d \n", r,
area);
}
void triangle(int s1, int s2, int s3)
{
int s;
s = (s1 + s2 + s3) / 2;
area = sqrt((s * (s - s1) * (s - s2) * (s - s3)));
printf("The area of the triangle having sides %d %d %d is %d
\n", s1, s2, s3, area);
}
int main()
{
int choice;

printf("Press 1 for Area of circle \nPress 2 for Area of


Square \nPress 3 for Area of Rectangle\nPress 4 for Area of
Triangle\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
int radius;
printf("Enter the radius of the circle ");
scanf("%d", &radius);
circle(radius);
}

break;
case 2:
{
int side;
printf("Enter the length of the side of the square ");
scanf("%d", &side);
square(side);
}
break;
case 3:
{
int length, breadth;
printf("Enter the length of the side of the rectangle
");
scanf("%d", &length);
printf("Enter the breadth of the side of the rectangle
");
scanf("%d", &breadth);
rectangle(length, breadth);
}
break;
case 4:
{
int s1, s2, s3;
printf("Enter the length of the first side of the
triangle ");
scanf("%d", &s1);
printf("Enter the length of the second side of the
triangle ");
scanf("%d", &s2);
printf("Enter the length of the third side of the
triangle ");
scanf("%d", &s3);
triangle(s1, s2, s3);
}
break;
deafult:
{
printf("You have entered an incorrect choice \n");
}
}
return 0;
}

Q4. Write a program to generate 10 employee details using structure. Enter


employee name, department (HR, TECH, SERVICE), salary, and rating (1-5)
from user. Update the salary by adding a 10% increment if the rating is 3 and
more. Otherwise, add a 7% increment. Count the number of employee in HR
department and print their details.
Answer:
#include <stdio.h>
#include <string.h>
struct employee
{
int rating;
float salary;
char name[20], department[10];
} e[10];
int main()
{
int count = 0;
for (int i = 0; i < 10; i++)
{
printf("Enter the details of %d Employee \n",i+1);
printf("Enter the Employee name ");
scanf("%s", e[i].name);
printf("Enter the Employee department ");
scanf("%s", e[i].department);
printf("Enter the Employee salary ");
scanf("%f", &e[i].salary);
printf("Enter the Employee rating ");
scanf("%d", &e[i].rating);
}
for (int i = 0; i < 10; i++)
{
if (e[i].rating >= 3)
e[i].salary += (0.1 * e[i].salary);
else
e[i].salary += (0.07 * e[i].salary);
if (strcmp(e[i].department, "HR") == 0)
{
count++;
printf("\nThe Employee name is ");
printf("%s", e[i].name);
printf("\nThe Employee department is ");
printf("%s", e[i].department);
printf("\nThe Employee salary is %.2f \n",
e[i].salary);
printf("The Employee rating is %d \n", e[i].rating);
}
}
printf("\nThe number of employees in HR is %d \n", count);
return 0;
}

You might also like