Posts

Showing posts from March, 2020

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!!"); } }

Write a program to generate Fibonacci number using do while loop.

#include<stdio.h> #include<conio.h> void main() { int n1=0,n2=1,n3,n,i=0; printf("enter the value of n:"); scanf("%d",&n);     do     {     n3=n1+n2;     n1=n2;     n2=n3;     i++; } while(i<n-2); printf("the fibonacci number is %d",n3); }

Write a program to read a sentence and counts the total number of character (excluding space) using while loop.

#include<stdio.h> #include<string.h> int main() { int count=0,i=0; char word[50]; printf("enter the string:"); scanf("%[^\n]",word); while(word[i]!='\0') { if(word[i]==' ' && word[i+1]!=' ') { count++; } i++; } printf("the total number of character is %d",strlen(word)-count); }

Write a C program for matrix addition with the help of function add(int a[ ][20], int b[ ][20], int n, int m) where a and b are matrix to be added and n and m are dimension of a and b.

#include<stdio.h> void add(int a[][20], int b[][20], int c[][20], int m, int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } } } void main() { int a[20][20],b[20][20],c[20][20]; int i,j,m,n; printf("enter row and coumn of matrices:"); scanf("%d%d",&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\na[%d][%d]=",i,j); scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\nb[%d][%d]=",i,j); scanf("%d",&b[i][j]); } } add (a,b,c,m,n); printf("the matrix after addition is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",c[i][j]); } if(j=m-1) { printf("\n\n"); } } } credit: Unknown

Write a program to evaluate transpose of n by n matrix with the help of function void transpose(int matrix[ ][20], int n) where matrix is the matrix to be transformed and n is the dimension of matrix.

#include<stdio.h> void transpose(int mat[][20], int n); int main() { int n,i,j,a[20][20],p; printf("enter the dimension of rows:"); scanf("%d%d",&n,&n); printf("enter the matrix:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("enter the elements a[%d][%d]:\t", i+1,j+1); scanf("%d",&a[i][j]); } } printf("\nenter the elements of matrix: \n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d \t",a[i][j]); if(j==n-1) { printf("\n\n"); } } } transpose (a,n); return 0; } void transpose(int a[][20],int n) { int trans[20][20],i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { trans[j][i]=a[i][j]; } } printf("\ntranspose of matrix:\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf(" %d \t",trans[i][j]); } if(j=n-1) { printf("\n\n...

Write a recursive program to find the factorial of a given number.

#include<stdio.h> long fact(int); int main() { int x; printf("enter the number:"); scanf("%d",&x); printf("the factorial of %d is %ld",x,fact(x)); } long fact(int n) {   if(n==0)   return (1);   else return (n*fact(n-1)); }

Write a program to evaluate GCD of two given integers. Use function that returns GCD.

#include<stdio.h> int gcd( int , int ); int main() { int x,y; printf("enter any two integers:"); scanf("%d%d", &x,&y); printf("the gcd of %d and %d is %d",x,y,gcd(x,y)); } int gcd(int n1, int n2) {  int i,gcd;  for(i=1;i<=n1 && i<=n2;i++)  {   if(n1%i==0 && n2%i==0)   gcd=i;  }  return(gcd); }