Slip 25
Q1. Write a C program to calculate sum of all even elements of a matrix. [15 Marks]
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3],sum=0;
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying sum of all even array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
sum=sum+disp[i][j];
}
}
}
printf("sum=%d=", sum);
return 0;
}
Q2. Write a menu driven program for the following option [25 Marks]
1. Check input number is Armstrong or not
2. Check input number is Perfect or not
#include <stdio.h>
void main()
{
int fig_code;
int number, originalNumber, remainder, result = 0;
int n,i=1,sum=0;
while(1)
{
printf("-------------------------\n");
printf(" 1 --> Armstrong or not\n");
printf(" 2 --> Perfect or not \n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
break;
case 2:
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
break;
default:
exit(0); }
}}
No comments:
Post a Comment