Tuesday, February 10, 2009

C Programs

Contents
========

================
Average of n numbers
================
/*Sum of n numbers */
/*It take the value for num*/
/*it adds the number upto that number and find out the average*/
#include
int main()
{
float num, sum, i;
num=0;
sum=0;
i=0;
float Average;
Average=0;

printf (" Enter the number =");
scanf("%f",&num);
for(i=1;i<=num;i++) { sum=sum+i; } Average=(sum/num); printf("%f\n",Average); return 0; }
============
Prime number
============
/*Program for the prime number*/
/*Input the number*/
/*If the number is 1, print PRIME*/
/*Check wheather the number is greater than zero*/
/*Devide the number from 2 to number-1*/
/*If once the reminder came to 0, Print the number is not prime and break the prgram*/
/*Otherwise print the NUMBER IS PRIME */

#include
int main()
{
int num,i;
printf("Enter the number=");
scanf("%d",&num);
if(num==1)
printf ("1 is a prime number \n");
else if (num<=0) printf ("The input should be greater than 0 \n"); else { i=2; while(i<=num-1) { if(num%i==0) { printf("%d is not a Prime number\n",num); break; } i=i++; } if(i==num) printf("%d is a prime number\n",num); } } ================
Fibonacci series

================
#include
int fibo(int num) /*Recursion function for fibonacci series*/
{
int static i, x=-1, y=1, temp=0;
if(num==0)
return 0;
else {
temp=x+y;
x=y;
y=temp;
printf("%d\n", temp);
fibo(num-1);
}
return x;
}
int main()
{
int fib, no;
printf("Enter the no of fibonacci series to be printed =");
scanf("%d", &no);
if(no<=0) { printf("No is not valid. Enter the No greater than 0\n"); } else { fib=fibo(no); } return 0; } =================== Multiplication Table
===================
/*Enter the number whose table is going to print*/
/*Enter the depth of the table*/
/*print the table by using the while loop*/
#include
int main()
{
int a,b,c,i;
i=1;
printf ("Enter the number = ");
scanf ("%d",&b);
printf ("Enter the depth of the table = ");
scanf ("%d",&a);
printf ("The mutiplication Table of %d\n",b);
printf ("------------------------------\n");
while (i<=a) { c=i*b; ("%d\t*%d\t=%d\n",i,b,c); i=i++; } } ============================= Printing a matrix by using function
==============================

/*Get the number of rows and column*/
/*create a function matrix */
/*Input the matrix by using the for loop*/
/*print the matrix by using the for loop*/
/*call the matrix function from the main function*/
/*End the program*/

#include
matrix()
{
int A[10][10];
int i=0, j=0, row=0, col=0;
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
/*Inputing the matrix*/
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Printing the matrix*/ printf("The given Matrix is \n"); for(i=1; i<=row; i++) { (j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } } int main() { matrix(); return 0; } ===============
Matrix addition

===============
/*---Program for matrix addition---*/
#include
int main()
{
int A[10][10];
int B[10][10];
int SUM[10][10];
int i=0, j=0, row=0, col=0;
/*input the values for the Rows and column for both Matrices*/
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
B[10][10]=0;
SUM[10][10]=0;

/*Inputing the matrix*/
printf("Enter the values for the matrix A\n");
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Inputing the matrix B*/ printf("Enter the values for the matrix B\n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&B[i][j]); } } /*Summming operation */ for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { SUM[i][j]=A[i][j]+B[i][j]; } } /*Printing the matrix A*/ printf("The Matrix A is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } /*Printing the matrix B*/ printf("The Matrix B is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",B[i][j]); } printf("\n"); } /*Printing the matrix SUM */ printf("The sum of the Matrix is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",SUM[i][j]); } printf("\n"); } return 0; } =====================
Matrix multiplication

=====================
/*Input the Values for the rows and columns for both matrices*/
/*Check wheather the column of first and row of the second is equal*/
/*If NO Print the NOTE and terminate the program*/
/*If YES Input the matrices*/
/*multiply the matrices*/
/*Print the Output matrix*/

#include
#include
int main()
{
int i,j,k;
int a,b,c,d; /*three variables to get the row and colum of the two matrix*/
int sum;
int m[10][10],n[10][10],l[10][10];
printf("Note: For matrix multiplication the number of columns in the first matrix should be equal to the number of rows in the second matrix\n");
printf("The first matrix:\n");
printf("Enter the number of rows for the first matrix : ");
scanf("%d",&a);
printf("Enter the number of columns for the first matrix : ");
scanf("%d",&b);
printf("The Second matrix:\n");
printf("Enter the number of rows of the second matrix : ");
scanf("%d",&d);
printf("Enter the number of columns of the second matrix : ");
scanf("%d",&c);

if(b==d)
{
printf("Enter the values of the first matrix:\n");
for(i=0;i
Transpose of Matrix

===================
/*input the rows and column values*/
/*Input the matrix */
/*Interchange the row and column values*/
/*Perform the transpose operation by using the for loop*/
/*print the the output matrix*/

#include
int main()
{
int A[10][10];
int B[10][10];
int i=0, j=0, row=0, col=0;
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
B[10][10]=0;
/*Inputing the matrix*/
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Printing the matrix*/ printf("The given Matrix is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } for(i=1; i<=col; i++) for(j=1; j<=row; j++) B[i][j]=A[j][i]; /*Printing the out put matrix*/ printf ("The Transpose of the matrix is\n"); for (i=1; i<=col; i++) { for (j=1; j<=row; j++) { printf("%d\t",B[i][j]); } printf("\n"); } return 0; } ====================================

0 comments:

  © PC Template PC by PC 2008

Back to TOP