Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
968 views

C Programming Assignment PDF

This program multiplies two matrices. It begins by prompting the user to enter the number of rows and columns for the first and second matrices. It then inputs the elements of each matrix from the user. It checks that the columns of the first matrix equals the rows of the second matrix. If so, it initializes a result matrix and calculates the product of the two matrices by summing the products of corresponding elements. It outputs the result matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
968 views

C Programming Assignment PDF

This program multiplies two matrices. It begins by prompting the user to enter the number of rows and columns for the first and second matrices. It then inputs the elements of each matrix from the user. It checks that the columns of the first matrix equals the rows of the second matrix. If so, it initializes a result matrix and calculates the product of the two matrices by summing the products of corresponding elements. It outputs the result matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Program to find sum and product fahrenheit = fahrenheit + STEP;

}/* end while */


#inc|ude<stdio.h> getch();
#include<conio.h> }/* end main() */
void main()
{ Program to find area of a triangle
int a, b, sum, product;
printf("Enter Value to A:"); #lnclude<stdio.h>
scanf("%d", &a); #lncIude<conio.h>
printf("\nEnter Value to B:"); #lnclude<math.h>
scanf("%d", &b); vold main()
sum = a + b; {
product = a * b; float a, b, c, s, area;
printf("\nSUM = %d", sum); clrscr();
printf("\nPRODUCT = %d", product); prlntf("Enter three sides :");
getch(); scanf("%f %f %f", &a, &b, &c);
}/* end main() */ s=(a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(sc));
Fahrenheit- Celsius Conversion printf("\nArea of triangle = %6.2f
Table Sq.units", area);
getch();
#include<stdio.h> }/* end maln() */
#include<conio.h>
#define F_LOW 0 Program to swap values of two
#define F_MAX 250 variables
#define STEP 25
#include<stdio.h>
void main() #include<conio.h>
{ vold main()
typedef float REAL; /* Type Definition {
*/ int a, b, temp;
REAL fahrenheit, celsius; /* clrscr();
Declaration */ printf("Enter Value to A:");
fahrenheit = F_LOW; /* Initialization scanf("%d", &a);
*/ printf("\nEnter Value to B:");
clrscr(); scanf("%d", &b);
printf("\tFahrenheit \t\tCelsius\n\n"); temp = a; a = b; b = temp;
while (fahrenheit <= F_MAX) printf("\n\nValue of A = %d", a);
{ printf("\n\nVaIue of B = %d", b);
celsius = (fahrenheit - 32.0)/ 1.8; getch();
printf("\t%f\t\t%f\n", fahrenheit, }/" end main() */
celsius);
Program to find value of y(x,n) printf("Enter coefficients a, b and c
:");
#include<stdio.h> scanf("%f %f %f", &a, &b, &c);
#lnclude<conio.h> d=b*b-4*a*c;
#lncIude<math.h> if (d > 0)
void main() {
{ x1 = (-b + sqrt(d))/(2*a);
int n; x2 = (-b - sqrt(d))/(2*a);
float x, y; printf("\nRoots are real and
clrscr(); unequal");
prlntf("Enter value to x and n :"); printf("\n%6.2f\t%6.2f", x1, x2);
scanf("%f %d",&x, &n); }
switch(n) else
{ if (d == 0)
case 1 : {
y=1+x; x = -b/(2*a);
break; printf("\nRoots are real and equal");
case 2 : printf("\n%6.2f", x);
y = 1 + x/n; }
break; else
case 3 : {
y = 1 + pow(x,n); printf("\nNo real roots, roots are
break; complex")
default: x1r = -b/(2*a);
y = 1 + n*x; x1i = sqrt(fabs(d))/(2*a);
break; x2r = x1r;
}/* end switch */ x2i = -x1i;
printf("\nVaIue of y(x,n) = %6.2f", printf("\n%.21+%.2fi", x1r, x1i);
y); printf("\n%.2f%.2fi", x2r, x2i);
getch(); }
}/* end main() */ getch();
}/* end main() */
Program to find roots of a
Quadratic Equation
Program to find factorial of a
#include<stdio.h> positive integer
#include<conio.h>
#include<math.h> #incIude<stdio.h>
void main() #incIude<conio.h>
{ void main()
float a, b, c, d, x1, x2, x, x1r, x1i, x2r, {
x2i; int k, i;
unsigned kfact;
clrscr(); #include<stdio.h>
printf("Enter the number :"); #incIude<conio.h>
scanf("%d", &k); void main()
kfact = 1; {
/* loop to generate numbers from 1 to int i = 0,j, num, div, bin[30];
k */ clrscr();
for(i = 1; i <= k; i++) printf("Enter a positive integer:");
kfact = kfact * i; scanf("%d", &num);
printf(\n%d factorial is %u", k, whiIe(num != 0)
kfact); {
getch(); bin[i++] = num%2;
}/* end main() */ num = num/2;
}
Program to print FIoyed's triangle printf("\n\nThe equivalent binary
as shown below: number is :");
1 for(i = i - 1; i >= 0; i--)
23 printf("%1d", bin[i]);
456 getch();
7 8 9 10 }/* end main() */
Program to print Floyd's triangle
Program to find sum of the digits
#incIude<stdio.h> of given integer
#include<conio.h>
void main() #include<stdio.h>
{ #inc|ude<conio.h>
int i, j, rows, count = 1; void main()
clrscr(); {
printf("Enter the number of rows :"); int n, q, r, sum=0;
scanf("%d", &rows); cIrscr();
printf("\n"); printf("Enter a positive integer :");
for(i=1;i<=rows; i++) scanf("%d", &n);
{ q = n;
for(i=1;j<=i;j++) while (q > 0)
printf("%8d", count++); {
printf("\n\n"); r = q % 10; sum += r; q /= 10;
} }
getch(); printf("\n\nSum of digits = %d",
}/* end main() */ sum);
getch();
Program to convert a decimal }/* end main() */
number to binary
Program to reverse the integer term = (term * x * x * (-1))/((i + 1)*
(i + 2));
#include<stdio.h> i = i + 2;
#include<conio.h> } /* end while */
void main() printf("\n\nSum of the sine series is =
{ %f" , sum);
long int n, q, r, rn = 0; getch();
clrscr(); }/* end main() */
printf("Enter a positive integer :");
scanf("%Id", &n); Program To Multiply Two Matrix
q = n;
while (q > 0) #include <stdio.h>
{ int main()
r=q% 10; rn=rn*10+r;q/=10; {
} int m, n, p, q, c, d, k, sum = 0;
printf("\n\n%Id is reversed as %Id", int first[10][10], second[10][10],
n, rn); multiply[10][10];
getch(); printf("\nPlease enter the numbers of
}/* end main() */ same line separated with space");

Program : 12 printf("\nEnter the number of rows


Program to find the summation of and columns of first matrix\n");
a sine series scanf("%d%d", &m, &n);
printf("Enter the elements of first
#include<stdio.h> matrix\n");
#include<conio.h>
void main() for ( c = 0 ; c < m ; c++ )
{ for ( d = 0 ; d < n ; d++ )
float x, term, sum = 0.0; scanf("%d", &first[c][d]);
int i = 1, n;
clrscr(); printf("Enter the number of rows and
printf("Enter x in radians :"); columns of second matrix\n");
scanf("%f", &x); scanf("%d%d", &p, &q);
printf("Enter end term power (n) :");
scanf("%d", &n); if ( n != p )
/* initialize the values */ printf("Matrices with entered orders
term = x; can't be multiplied with each
/* loop for the summation of series */ other.\n");
while (i <= n) else
{ {
sum += term; printf("Enter the elements of second
matrix\n");
printf("Enter the elements of first
for ( c = 0 ; c < p ; c++ ) matrix\n");
for ( d = 0 ; d < q ; d++ ) for ( c = 0 ; c < m ; c++ )
scanf("%d", &second[c][d]); for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
for ( c = 0 ; c < m ; c++ )
{ printf("Enter the elements of second
for ( d = 0 ; d < q ; d++ ) matrix\n");
{ for ( c = 0 ; c < m ; c++ )
for ( k = 0 ; k < p ; k++ ) for ( d = 0 ; d < n ; d++ )
{ scanf("%d", &second[c][d]);
sum = sum +
first[c][k]*second[k][d]; for ( c = 0 ; c < m ; c++ )
} for ( d = 0 ; d < n ; d++ )
multiply[c][d] = sum; sum[c][d] = first[c][d] +
sum = 0; second[c][d];
}}
printf("Product of entered matrices:- printf("Sum of entered matrices:-
\n"); \n");
for ( c = 0 ; c < m ; c++ ) for ( c = 0 ; c < m ; c++ )
{ {
for ( d = 0 ; d < q ; d++ ) for ( d = 0 ; d < n ; d++ )
printf("%d\t", multiply[c][d]); printf("%d\t", sum[c][d]);

printf("\n"); printf("\n");
}} }
getch(); getch();
} }

Program To Add Two Matrix 1. program to check palindrome of


any Word
#include<stdio.h>
main() #include<stdio.h>
{ #include<conio.h>
int m, n, c, d, first[10][10], int main()
second[10][10], sum[10][10]; {
printf("\nPlease enter the numbers char str[30];
of same line separated with space"); char *p,*t;
printf("\nEnter the number of rows printf("Enter any string : ");
and columns of matrix "); gets(str);
scanf("%d%d", &m, &n); for(p=str ; *p!=NULL ; p++);
for(t=str, p-- ; p>=t; )
{
if(*p==*t) 1) Program Addition
{
p--; #include<stdio.h>
t++; #include<conio.h>
} void main()
else {
break; int number;
} float amount;
if(t>p) number = 100;
printf("String is palindrome"); amount = 30.75 + 75.35;
else clrscr();
printf("String is Not printf("%d\n", number);
palindrome"); printf("%6.2f", amount);
getch(); getch();
} }
2. program to check palindrome of
any digit number. 2) Program using Cosine Function
#include<stdio.h>
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ #include<math.h>
long no;
int a,b,sum; #define PI 3.1416
//clrscr(); #define MAX 180
printf("Enter ANY no:"); void main()
scanf("%ld",&no); {
sum=0; int angle;
a=no; float x,y;
while(a>0) angle = 0;
{ clrscr();
b=a%10; printf("\t\t\tAngle\t\tCos(angle)\n\n")
sum=sum * 10 + b; ;
a=a / 10;
} while (angle <= MAX)
if(sum==no) {
printf("%d is palindrome",no); x = (PI/MAX]*angle;
else y = cos(x);
printf("%d is not printf("\t%d\t\t%.4f\n, angle, y);
pallindrome\n",no); angle = angle + 10;
getch(); }/* end while */
} getch(); .
}/* end main() */ big = a;
else
Average of n values big = c;
else
#include<stdio.h> if (b > c)
#include<conio.h> big = b;
#define N 10 /* Symbolic Constant */ else
void main() big = c;
{ printf("\nBiggest number is %d", big);
int count; /* getch();
Declaration of */ } /* end main() */
float sum, average, number; /*
Variables */ Program to find out the GCD of
sum = 0; /* two positive integers
Initialization */
count = 0; /* of #include<stdio.h>
Variables*/ #include<conio.h>
clrscr(); void main()
vvhile (count < N) {
{ int a, b;
scanf("%f", &number); clrscr();
sum = sum + number; printf("Enter two positive integers:");
count = count + 1; scanf("%d %d", &a, &b);
} /* end while */ printf("\nThe GCD of %d and %d is ",
average = sum/N; a, b);
printf("N = %d\tSum = %f", N, sum); while(a != b)
printf("\tAverage = %f", average); {
getch(); if (a > b]
} /* end main() */ a = a - b;
else
Program to find biggest of three b = b - a;
numbers using nested if }
printf("The GCD between the two
#include<stdio.h> integers : %d", a);
#include<conio.h> getch();
void main() /* end main() */
{
int a,b,c,big; Program to find sum of the digits
printf("Enter three numbers:"); of given integer
scanf("%d%d%d",&a, &b, &c);
if (a > b) #include<stdio.h>
if (a > c) #include<conio.h>
void main() #include<conio.h>
{ #include<math.h>
int n, q, r, sum = 0; void main()
{
printf("Enter a positive integer :"); int n,k,flag = 0,r;
scanf("%d", &n); printf("Enter a positive integer :");
q = n; scanf("%d", &n);
while {q > O) for (k= 2; k <= sqrt(n) && flag == 0;
{ k++)
r = q %10; sum += r; q /= 10; {
printf("\n\nSum of digits = %d", r = n%k:
sum); if(!r)
getch(); flag = 1;
} /* end main() */ }
/* Program : 14 */ if(!flag)
/* Program to check Armstrong printf("\n%d is a prime number",n);
number */ else
#include<stdio.h> printf("\n%d is not a prime
#include<conio.h> numbar",n);
void nain() getch();
{ }/* End main() */
int n, q, r, s = 0;
clrscr(); Program to find sum of N integers
printf{"Enter a positive integer :"); using Array
scanf("%d", &n);
q = n; #include<stdio.h>
while (q > 0) #include<conio.h>
{ void main()
r = q %10; s += r*r*r; q /= 10; {
} int x[50], n, i, sum = 0;
if (n == s) clrscr();
printf("\n\n%d is an Armstrong printf("How many integers?");
number", n); scanf("%d", &n);
else printf("\nEnter %d integers:" n);
printf("\n\n%d is not an Armstrong /* loop to read and sum N integers */
number",n); for(i = 0; i < n; i++)
getch(); {
} / * end main() */ scanf("%d", &x[i]);
sum += x[i];
Program to check prime number }
printf("\nSum of all integers
#include<stdio.h> =%d",sum);
getch(); } /* End main() */
} /* end main() */
Program to count the number of
Program to counts vowels in a vowels, consonants, digits,
sentence whitespace characters and other
characters in a line of text
#include<stdio.h>
#include<conio.h> #include<stdio.h>
#include<string.h> #include<conio.h>
void main() void main()
{ {
char st[80], ch; char ch, line[80];
int count = 0, i = 0; int count = 0, vowels = 0, consonants
clrscr(); = 0;
/* loop to read a string */ int digits = 0, whitespc = 0, other =
printf("Enter a sentence :\n"); 0;
while((ch = getchar()) != '\n) clrscr();
st[i++] = ch; printf("Enter a line of text below:\n");
/* terminate the string with NULL scanf("%[^\n)", line);
character */ while(('ch = toupper(line[count])) !=
st[i] = '\0'; \0')
/* loop to count the vewels in the {
string */ if(ch == A'| |ch == 'E'| |ch == 'l' |
for (i = 0; i < strlen (st); i++) |ch == 'O'| |ch == 'U')
switch(st[i]) vowels++;
{ else if (ch >= A && ch <= Z]
caseA: consonants++;
case 'E: else if (ch >= '0 && ch <= 9')
case'I': digits++;
case'O': else if (ch == ' ' | | ch == '\t`)
case`U`: whitespc++;
casea': else
case'e': other++;
case'i': count++;
case`o': } /* end while */
caseu': printf("\n\nNo. of vowels: %d",
count++; vowels);
break; printf("\n\nNo. of consonants: %d",
} /* end switch */ consonants);
printf(" \n\n%d vowels are present in printf{"\n\nNo. of digits: %d", digits);
the sentence",count) printf("\n\nNo. of whitespace
getch(); characters: %d", whitespc);
printf("\n\nNo. of other characters: Program to apply binary search
%d", other); method to search a set of N
getch(); numbers using
} /* end main() */ function

Program to arrange names in #include<stdio.h>


alphabetical order #include<conio.h>
/* function prototype declaration */
#include<stdio.h> int bi_search(int x[], int n, int s);
#include<conio.h> void main(void)
#include<string.h> { int x[20], i, n, s;
void main() clrscr();
{ printf("How many numbers?");
char names[50][20], temp[20]; scanf("%d", &n);
int n, i, j; /* loop to read all numbers */
clrscr(); printf("\nEnter all numbers in the list
printf("How many names?"); :\n");
scranf(%d", &n); for(i=0; i < n; i++)
printf(" \nEnter %d names one by scanf("%d", &x[i]);
one:\n",n); printf("\nEnter the number to be
for(i=0; i < n; i++) searched :");
scanf("%s", names[i]); scanf("%d", &s);
/* loop to arrange names in /* Calling the Function */
alphabetical order */ if(bi_search(x,n,s))
for(i=0; i < n-1; i++) printf ("\n The number %d is
for(j=i+1; j < n; j++) present in the list",s);
if(strcmp(names[i], names[j]) > 0) else
{ printf("\nThe number %d is not
strcpy[temp, names[i]); present in the list", s);
strcpy(names[i], names[j]); } /* end main() */
strcpy(names[j], temp); /* function to search the number */
} int bi_search(int x[], int n, int s)
/* loop to print the alphabetical list of {
names */ int i,j,flag = 0, start = 0, mid, end =
printf("\nNames in alphabetical order n;
:"); while(start < end && flag ==0)
for(i=0; i < n; i++) {
printf("\n%s",narnes[i]); mid = (start + end) / 2;
getch(); if(x[mid] < s)
} /* end main() */ end = mid;
else
if(x[mid] < s)
start = mid + 1; /* loop to arrange the numbers in
else ascending order */
flag = 1; for(i = 0; i < n - 1; i++)
} for(j = 0; j < n - i; j++)
return flag; if(x[j] > x[ j+1 ])
} {
temp = X[j]; X[j] = X[ j + 1 ];
Program to sort the elements of x[ j + 1 ] = temp;
an array in ascending }
order using bubble sort }

#include<stdio.h> Program to write the power(x, n)


#include<conio.h> function
void main(void)
{ #include<stdio.h>
int i, n, x[50]; #include<conio.h>
/* function prototype declaration */
/* function prototype declaration */ duble power(int x, int n);
void sort(int x[], int n); void main(void)
clrscr(); {
printf("How many values?"); int a, n;
scanf("%d", &n); printf("Enter a, n:");
/* loop to read all values */ scanf("%d %d", &a, &n);
printf("'\nEnter %d values one by printf("\n\n%d to power %d is %1f,
one: \n\n", n); a, n, power(a, n));
for(i = 0; i < n; i++) getch();
scanf("%d", &x[i]); }/* end main() */
sort(x, n); /* function calling */ / * function definition */
/* loop to print the sorted values */ double power(int x, int n)
printf("\nNumbers in ascending order: {
\n"); double: p = 1.0;
for(i = 0; i < n; i++) if(n >= 0)
printf("%5d", x[i]); while(n--)
getch(); p*=x;
} /* end main() */ else
while(n++)
/* Function definition */ p/=x;
void sort(int x[], int n) return(p);
{ }
int i, j, temp;
Program to find factorial using while( (ch = getche()) != '\r' )
recursive function and fputc( ch, p );
also to calculate nCr fclose(p);
}
#include<stdio.h>
#include<conio.h> Reading a File in C language

/* function prototype declaration */ #include <stdio.h>


unsigned long fact(int n); int main ( void )
void main(void) { static const char filename[] =
{ "file.txt";
int r, n; FILE *file = fopen ( filename, "r" );
printf("Enter values to n and r :"]; if ( file != NULL )
scanf("%d %d", &n, &r); {
prinrf("\nThe value of nCr = %u", char line [ 128 ]; /* or other
fact(n)/fact(r)*fact(n-r)); suitable maximum line size */
getch(); while ( fgets ( line, sizeof line, file
} /* end main() */ ) != NULL ) /* read a line */
/* Recursive function to find factorial {
*/ fputs ( line, stdout ); /* write
unsigned long fact(int k) the line */
{ }
if(k == 1) fclose ( file );
return 1; }
else else
return k*fact(k-1); {
} perror ( filename ); /* why didn't
the file open? */
Program to Writing Data To A } return 0;}
Simple File /* file.txt
This text is the contents of the file named,
"file.txt".
#include<stdio.h> It is being used with some example code to
demonstrate reading a file line by
void main()
line. More interesting things could be done with
{ the output, but I'm trying to
FILE *p; keep this example very simple.
*/
char ch;
/* my output
This text is the contents of the file named,
p = fopen("c:\Hello.txt","w"); "file.txt".
It is being used with some example code to
clrscr(); demonstrate reading a file line by
line. More interesting things could be done with
the output, but I'm trying to
printf("\nEnter the text nd hit
keep this example very simple.
Enter to Terminate\n"); */

You might also like