FYBSC(CS)_Slip 7

 Q 1) A 

i. Write a ‘C’ program to calculate area and circumference of a circle.

#include<stdio.h>
#define PI 3.142
int main()
{
     int r;
     printf("\n Enter radius : ");
     scanf("%d",&r);
     printf("\n Area of circle = %.3f",PI*r*r);
     printf("\n Circumference of circle : %.3f",2*PI*r);
   return 0;
}

Program Output 

Enter radius : 8

Area of circle = 201.088
Circumference of circle : 50.272

ii Write a ‘C’ program to accept a character and check if it is alphabet, digit or special symbol. If it is an alphabet, check if it is uppercase or lowercase.

#include<stdio.h>
#include<ctype.h>
int main()
{
    char c;
    printf("\nEnter any character : ");
    scanf("%c",&c);
    if(isalpha(c))
    {
        if(islower(c))
            printf("\n Lowercase alphabet");
        else
            printf("\n Uppercase alphabet");
    }
    else if(isdigit(c))
        printf("\n You entered Digit");
    else
        printf("It is a special symbol");
    return 0;
}

Program Output:

Enter any character : @
It is a special symbol

OR

Q 1) A Write a ‘C’ program to read a matrix and calculate the sum of its diagonal elements.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,j,arr[5][5],sum=0;
    printf("\nEnter order of matrix : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
       for(j=0;j<n;j++)
       {
            printf("\nEnter matrix element a%d%d : ",i+1,j+1);
            scanf("%d",&arr[i][j]);
       }
    }
    printf("\n\nGiven Matrix is : \n ");
    for(i=0;i<n;i++)
    {
       for(j=0;j<n;j++)
          printf("%d\t",arr[i][j]);
       printf("\n");
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i==j)
                   sum=sum+arr[i][j];
         }
   }
   printf("\nSum of diagonal elements = %d",sum);
   return 0;
 }

Program Output:


Enter order of matrix : 3

Enter matrix element a11 : 1

Enter matrix element a12 : 2

Enter matrix element a13 : 3

Enter matrix element a21 : 4

Enter matrix element a22 : 5

Enter matrix element a23 : 6

Enter matrix element a31 : 7

Enter matrix element a32 : 8

Enter matrix element a33 : 9

Given Matrix is :

1       2       3
4       5       6
7       8       9
Sum of diagonal elements = 15

0 Comments:

Post a Comment