Posts

Write a program to identify whether the given number is a perfect number or not using a function. 28 is a perfect number.

#include<stdio.h> int perfect(int ); int main() { int n,s; printf("enter the number:"); scanf("%d",&n); s=perfect(n); if(s==n) { printf("%d is perfect",n); } else printf("%d not perfect"); } int perfect(int n) { int i,sum=0; for(i=1;i<=n/2;i++) { if(n%i==0) { sum=sum+i; } } return sum; }

Write a C program to store N numbers in a one dimensional array and calculate its average with the help of the function.

#include<stdio.h> void avg(int arr[], int n); int main() { int n,i,a[100]; printf("Amount of numbers:"); scanf("%d",&n);     printf("\n Enter the numbers:");     for(i=0;i<n;i++)     {    scanf("%d",&a[i]); } avg(a,n); } void avg (int arr[], int n) { int i,sum=0; float a; for(i=0;i<n;i++) sum+=arr[i]; a=(float)sum/n; printf("the average is %f",a); } credit: Manoj Kumar Bhat

Write a C program to determine determinant of a square matrix with the help of function int determinant(int a[][], n) where a is the matrix whose determinant is to be found and n is dimension of square matrix.

#include<stdio.h> #include<math.h> int determinant(int a[10][10], int n); int main() { int i,j,a[10][10],n; printf("enter the size of matrix:"); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("enter the elements of the matrix a[%d][%d]=",i,j); scanf("%d",&a[i][j]); } } determinant(a,n); printf("the determinant of the matrix is %d", determinant(a,n)); } int determinant(int a[10][10], int n) { int i,j,det; for(i=0;i<n;i++) { for(j=0;j<n;j++) { det=(a[0][0]*a[1][1]-(a[0][1]*a[1][0])); } return det; } }

Write a program to calculate the factorial of a given number.

#include<stdio.h> int main() { int i,n,f=1; printf("enter the number:"); scanf("%d",&n); for(i=1;i<=n;i++) { f=f*i; } printf("factorial of %d is %d",n,f); }

Write a program to identify whether the given number is a perfect number or not. 28 is a perfect number.

#include<stdio.h> int main() { int i,sum=0,n; printf("enter the numbers:"); scanf("%d", &n); for(i=1;i<=n/2;i++) { if(n%i==0) { sum=sum+i; } } if(sum==n) { printf("%d is a perfect number",n); } else printf("%d is not a perfect number",n); return 0; }

Write a program to read number and identifies whether the given number is a prime number or not.

#include<stdio.h> #include<conio.h> void main() { int i, num; printf("\n Enter a number:"); scanf("%d",&num); for(i=2;i<num;i++) { if(num%i==0) { printf("\n Not prime!!"); break; } } if(i==num) { printf("\n prime Numbers!!"); } }