Q 1) A
i. Write a ‘C’ program to calculate area and perimeter of a rectangle.
#include <stdio.h>
#include <conio.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);
/* Perimeter of Rectangle = 2 X(Length + Width) */
perimeter = 2*(length + width);
printf("Perimeter of Rectangle : %0.4f\n", perimeter);
getch();
return 0;
}
Program Output
Enter length of Rectangle 6.5
Enter width of Rectangle 5
Area of Rectangle : 32.5000
Perimeter of Rectangle : 23.0000
ii Write a ‘C’ program to calculate the sum of factors of a number.
#include <stdio.h>
int main()
{
int num, i, sum;
sum=0;
printf("\nEnter a positive integer: ");
scanf("%d", &num);
printf("\nFactors of %d are: ", num);
for (i = 1; i<=num; i++)
{
if (num % i == 0)
{
printf("%d ", i);
sum=sum+i;
}
}
printf("\nSum of Factors of %d is:\t %d ", num,sum);
return 0;
}
Program Output:
Enter a positive integer: 98
Factors of 98 are: 1 2 7 14 49 98
Sum of Factors of 98 is: 171
OR
Q 1) A Write a ‘C’ program to check if a matrix is upper triangular.
#include <stdio.h>
int main()
{
int n;
printf("Enter Dimension\n");
scanf("%d",&n);
int flag = 0;
int mat[n][n];
int i, j;
printf("\nEnter Matrix Elements\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d",&mat[i][j]);
}
}
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (mat[i][j] != 0)
flag = 0;
else
flag = 1;
printf("\nGiven Array Is\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
if (flag == 1)
printf("Upper Triangular Matrix");
else
printf("Not an Upper Triangular Matrix");
return 0;
}
Program Output:
Enter Dimension
3
Enter Matrix Elements
1
2
3
0
4
5
0
0
6
Given Array Is
1 2 3
0 4 5
0 0 6
Upper Triangular Matrix
0 Comments:
Post a Comment