0% found this document useful (0 votes)
22 views

Good Programs in C

The document contains 3 C programs: 1. A program to find prime numbers between 2 and 1000 by checking if each number is divisible only by 1 and itself. 2. A program to write text in a window by setting the text and background colors, clearing the screen, and outputting the text string. 3. A program to multiply two matrices by initializing a result matrix to 0, inputting the matrices, and calculating each element of the result as the sum of the products of corresponding elements of the first matrix and rows of the second.

Uploaded by

Anonymous zwCV8Z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Good Programs in C

The document contains 3 C programs: 1. A program to find prime numbers between 2 and 1000 by checking if each number is divisible only by 1 and itself. 2. A program to write text in a window by setting the text and background colors, clearing the screen, and outputting the text string. 3. A program to multiply two matrices by initializing a result matrix to 0, inputting the matrices, and calculating each element of the result as the sum of the products of corresponding elements of the first matrix and rows of the second.

Uploaded by

Anonymous zwCV8Z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

PROGRAM TO FIND PRIME NUMBERS


#include<stdio.h>
#include<conio.h>
#define MAX 1000
main()
{
char isprime[MAX];
int i,j,count;
clrscr();
//make all numbers prime by default
for(i=2;i<MAX;i++)
isprime[i]='Y';
//cancel out the multiples of numbers one by one
for(i=2;i<MAX;i++) {
for(j=2; i*j<MAX;j++)
isprime[i*j]='N';
}
//display the prime numbers
for(i=2;i<MAX;i++) {
if (isprime[i]=='Y')
printf("%d ,",i);
// pause after processing 100 candidate numbers
if (i%100==0)
getch();
}
}

2. PROGRAM TO WRITE TEXT IN A WINDOW


#include<conio.h>
#include<stdio.h>
void main()
{
char hello[20]="Good morning!";
textbackground(YELLOW);
clrscr();
window(10,10,40,20);
textcolor(YELLOW);
textbackground(BLUE);
clrscr();
cputs(hello);
getch();
}

3. PROGRAM TO MULTIPLY TWO MATRICES


#include<stdio.h>
#include<conio.h>
void main() {
int a[10][10],b[10][10],c[10][10];
int row,col,rowsa,colsa,rowsb,colsb,k;
clrscr();
printf("Enter the number
scanf("%d",&rowsa);
printf("Enter the number
scanf("%d",&colsa);
printf("Enter the number
scanf("%d",&rowsb);
printf("Enter the number
scanf("%d",&colsb);

of rows in Matrix A: ");


of columns in Matrix A: ");
of rows in Matrix B: ");
of columns in Matrix B: ");

if(colsa!=rowsb) {
printf("The number of columns in A and rows in B dont match");
getch();
return ;
}
//initialising the final Matrix C of the order rowsa X colsb
for(row=0;row<rowsa;row++)
for(col=0;col<colsb;col++)
c[row][col]=0;
//input elements of Matrix A
for(row=0;row<rowsa;row++)
for(col=0;col<colsa;col++) {
printf("Enter the element on %d row and %d col of Matrix A: ",row+1,col+1);
scanf("%d",&a[row][col]);
}
//input elements of Matrix B
for(row=0;row<rowsb;row++)
for(col=0;col<colsb;col++) {
printf("Enter the element on %d row and %d col of Matrix B: ",row+1,col+1);
scanf("%d",&b[row][col]);
}
//the final Matrix C is of the order rowsa X colsB
for(k=0;k<colsb;k++)
for(row=0;row<rowsa;row++)
for(col=0;col<colsa;col++)
//Starting with the first row of Matrix A , element at each ith column of Matrix A is multiplied
//by the element at the corresponding ith row of Matrix B
//This step is repeated for the other remaining rows of Matrix A
//ie each row of Matrix A makes a row in final Matrix C
//ie each column of Matrix B makes a column in final Matrix C
c[row][k]= c[row][k]+a[row][col]*b[col][k];

//output the final Matrix C


for(row=0;row<rowsa;row++)
for(col=0;col<colsb;col++)
printf("The element on %d row and %d col of Matrix C is:
getch();

%d\n",row+1,col+1,c[row][col]);

You might also like