Important Programs in c
-
Upload
rubal-bhatia -
Category
Documents
-
view
220 -
download
0
description
Transcript of Important Programs in c
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 1
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
1
These Programs are published and printed by myUET.net.tc. ©All Rights
Reserved. Legal action may be taken against the person who copies it
without permission.
www.myUET.net.tc
We Feel Your Feelings……!
Online Community of UETians.
Notes of all Subjects , All Departments available for Free.
Video Clips Collection , Photo collection.
Live Chat , Forum and myUET Groups.
Visit now:
www.myUET.net.tc
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 2
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
2
IMPORTANAT NOTICE:
If you are studying C++ instead of C language. Or
you are feeling that these programs are quite different
from the pattern discussed in your class. Then don’t
worry, just make the following changes in programs
and they’ll be all according to your pattern:
Replace
#include<stdio.h> and
#include<conio.h> by #include<iostream.h>
Raplace
Printf() statement by cout<<” “;
Replace
Scanf() statement by cin>>
For further information
www.myUET.net.tc
Or
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 3
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
3
INDEX
Math
digits
Program to count digits
To calculate power of 10
To reverse numbers
To reverse numbers precisely
To separate most significant digits
To separate 6 most significant digits
To separate n least significant digits
To separate n most significant digits
Factorials
Factorial using while loop double
Factorial using while loop int
Factorial using for loop 1
Factorial using for loop 2
Fibbonacci series
Fibbonacci series for loop
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 4
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
4
Fibbonacci series while loop
Fibbonacci series using arrays
Calculating power
Calculating power using int
Calculating power using long
Calculating power using float
Star patterns
Simple stars
Stars taking no. of lines (for loop)
Stars taking no. of lines and star per line (for loop)
Stars reversed pattern (for loop)
Stars taking no. of lines (while loop)
Stars taking no. of lines and star per line (while loop)
Stars reversed pattern (while loop)
Stars with spaces
Right hand triangle (for loop)
Central triangle (half dimond)(for loop)
Right hand triangle (while loop)
Central triangle (half dimond)(while loop)
Stars with starting and ending point
Left triangle (for loop)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 5
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
5
Left triangle with a condition (for loop)
Left triangle (while loop)
Left triangle with a condition (while loop)
Diamonds
Using for loop
Using while loop
Arrays and matrices
Initializing arrays
Initializing 1Darrays with user input
Initializing 2Darrays with user input
Initializing & printing 1Darrays with user input
Initializing & printing 2Darrays with user input
Initializing matrices
Initializing a 3x3 matrix antidaigonal
Initializing a 7x7 identity matrix
Initializing a 7x7 matrix antidaigonal
Initializing a 7x7 matrix cross
Initializing a 7x7 matrix full outergrid
Initializing a 7x7 matrix half outergrid
Initializing a 7x7 matrix tridaigonal
Initializing a 7x7 matrix tridaigonal anti
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 6
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
6
Matrix operations
Matrix scalar multiplication
Matrix addition
Matrix multiplication 3x3
Matrix multiplication 4x4
Matrix multiplication 8x8
Nested loops Nested for loop
Nested while loop
Nested while test1
Nested while test2
Nested while test3
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 7
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
7
math
digits
1- Program to count digits
#include <stdio.h>
#include <conio.h>
int main (void)
{
long number;
int digits;
clrscr();
printf("Enter the number : ");
scanf("%li",&number);
for(digits=0;number>0;digits++)
number = number/10;
printf("The no. of digits are : %i",digits);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 8
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
8
2- To calculate power of 10
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,power;
long result=1;
clrscr();
printf("Enter the power : ");
scanf("%i",&power);
for(i=0;i<power;i++)
result *= 10;
printf("10 raised to the power %i is : %li",power ,result);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 9
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
9
3- To reverse numbers
#include <stdio.h>
#include <conio.h>
int main (void)
{
long num,rev_num=0;
int num_dig,sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&num);
//Separating Digits
for(num_dig=1;num>0;num_dig++)
{
sep_dig = num % 10;
num = num / 10;
rev_num = (rev_num * 10) + sep_dig;
printf("Digit no. %i is : %i\n",num_dig ,sep_dig);
}
printf("The reversed number is : %li\n",rev_num);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 10
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
10
4- To reverse numbers precisely
#include <stdio.h>
#include <conio.h>
int main (void)
{
long num,rev_num=0;
int sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&num);
//Separating Digits
while(num>0)
{
sep_dig = num % 10;
num = num / 10;
rev_num = (rev_num * 10) + sep_dig;
}
printf("The reversed number is : %li\n",rev_num);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 11
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
11
5- To separate most significant digits
#include <stdio.h>
#include <conio.h>
int main (void)
{
long number,temp,divisor=100;
int i,num_dig,sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&number);
//Separating Digits
for(num_dig=0;number>0;num_dig++)
{
sep_dig = number / divisor;
number = number - (sep_dig * divisor);
divisor /= 10;
printf("Digit no. %i is : %i\n",num_dig ,sep_dig);
}
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 12
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
12
6- To separate 6 most significant digits
#include <stdio.h>
#include <conio.h>
int main (void)
{
long number,temp,divisor=100000;
int i,num_dig,sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&number);
//Separating Digits
for(num_dig=0;number>0;num_dig++)
{
sep_dig = number / divisor;
number = number - (sep_dig * divisor);
divisor /= 10;
printf("Digit no. %i is : %i\n",num_dig ,sep_dig);
}
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 13
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
13
6- To separate n least significant digits
#include <stdio.h>
#include <conio.h>
int main (void)
{
long number;
int num_dig,sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&number);
//Separating Digits
for(num_dig=1;number>0;num_dig++)
{
sep_dig = number % 10;
number = number / 10;
printf("Digit no. %i is : %i\n",num_dig ,sep_dig);
}
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 14
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
14
7 - To separate n most significant digits
#include <stdio.h>
#include <conio.h>
int main (void)
{ long number,temp,divisor=1;
int i,num_dig,sep_dig;
clrscr();
printf("Enter the number : ");
scanf("%li",&number);
//Counting Digits
temp = number;
for(i=0;temp>0;i++)
temp = temp/10;
//Calculating initial value of divisor
num_dig = i;
while(num_dig>1)
{
divisor *= 10;
num_dig--;
}
//Separating Digits
for(num_dig=i;num_dig>0;num_dig--)
{
sep_dig = number / divisor;
number = number - (sep_dig * divisor);
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 15
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
15
divisor /= 10;
printf("Digit no. %i is : %i\n",num_dig ,sep_dig);
}
getch();
return 0;
}
Factorials
1- Factorial using while loop double
#include <stdio.h>
#include <conio.h>
int main (void)
{
int n;
double z=1;
clrscr();
printf("Calculating factorial of : ");
scanf("%i",&n);
while(n>0)
{
z *= n;
n-- ; }
printf("%f",z);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 16
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
16
2- Factorial using while loop int
#include <stdio.h>
#include <conio.h>
int main (void)
{
int n,z=1;
clrscr();
printf("Calculating factorial of : ");
scanf("%i",&n);
while(n>0)
{
z *= n;
n--;
}
printf("%i",z);
getch();
return 0;
}
3- Factorial using for loop 1
#include <stdio.h>
#include <conio.h>
int main (void)
{
int n,z=1;
clrscr();
printf("Calculating factorial of : ");
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 17
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
17
scanf("%i",&n);
for(;n>0;n--)
z *= n;
printf("%i",z);
getch();
return 0;
}
4- Factorial using for loop 2
#include <stdio.h>
#include <conio.h>
int main (void)
{
int n,z=1;
clrscr();
printf("Calculating factorial of : ");
scanf("%i",&n);
for(;n>0;)
{
z *= n;
n--;
}
printf("%i",z);
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 18
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
18
Fibbonacci series
1- Fibbonacci series for loop
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,n,x=0,y=1,z;
clrscr();
printf("Number of values to be printed for the fibbonacci series: ");
scanf("%i",&n);
for(i=0;i<n;i++)
{
z = x+y;
x = y;
y = z;
printf("%i\t",x);
}
getch();
return 0;
}
2- Fibbonacci series while loop #include <stdio.h> #include <conio.h> int main (void) { int i,n,x=0,y=1,z; clrscr();
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 19
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
19
printf("Number of values to be printed for the fibbonacci series: "); scanf("%i",&n); i=0; while(i<n) { z = x+y; x = y; y = z; i++; printf("%i\t",x); } getch(); return 0; }
3- Fibbonacci series using arrays #include <stdio.h> #include <conio.h> int main (void) { int a[15]; int i,x=0,y=1,z; clrscr(); /*Calculating and storing the first 15 values of the series in an array*/ i=0; while(i<15) { z = x+y; x = y; y = z; a[i] = x; i++; } /*Printing the values stored in the array*/ i=0; while(i<15) { printf("%i\t",a[i]); i++; } getch();
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 20
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
20
return 0; }
Calculating power
1- Calculating power using int #include <stdio.h> #include <conio.h> int main (void) { int i,number,power,result=1; clrscr(); printf("Enter the number : "); scanf("%i",&number); printf("Enter the power : "); scanf("%i",&power); for(i=0;i<power;i++) result *= number; printf("The result is : %i",result); getch(); return 0; }
2- Calculating power using long #include <stdio.h> #include <conio.h> int main (void) { int i; long number,power,result=1; clrscr(); printf("Enter the number : "); scanf("%li",&number);
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 21
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
21
printf("Enter the power : "); scanf("%li",&power); for(i=0;i<power;i++) result *= number; printf("The result is : %li",result); getch(); return 0; }
3- Calculating power using float #include <stdio.h> #include <conio.h> int main (void) { int i,power; float number,result=1; clrscr(); printf("Enter the number : "); scanf("%f",&number); printf("Enter the power : "); scanf("%i",&power); for(i=0;i<power;i++) result *= number; printf("The result is : %f",result); getch(); return 0; }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 22
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
22
Star patterns
Simple stars
1- Stars taking no. of lines (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); printf("Number of stars per line : "); scanf("%i",&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("*"); } printf("\n"); } getch(); return 0; }
2- Stars taking no. of lines and star per line (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,i,j;
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 23
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
23
clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); for(i=0;i<m;i++) { for(j=0;j<i;j++) { printf("*"); } printf("\n"); } getch(); return 0; }
3- Stars reversed pattern (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); n=m; for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("*"); } n--; printf("\n"); } getch(); return 0; }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 24
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
24
4- Stars taking no. of lines (while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); printf("Number of stars per line : "); scanf("%i",&n); i=0; while(i<m) { j=0; while(j<n) { printf("*"); j++; } printf("\n"); i++; } getch(); return 0; }
5- Stars taking no. of lines and star per line (while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,i,j; clrscr(); printf("Number of lines of stars : ");
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 25
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
25
scanf("%i",&m); i=0; while(i<m) { i++; j=0; while(j<i) { j++; printf("*"); } printf("\n"); } getch(); return 0; }
6- Stars reversed pattern (while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); n=m; i=0; while(i<m) { i++; j=0; while(j<n) { j++; printf("*"); } n--; printf("\n");
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 26
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
26
} getch(); return 0; }
Stars with spaces
1- Right hand triangle (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); n=m; for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" "); } n--; for(j=0;j<=i;j++) { printf("*"); } printf("\n"); } getch(); return 0; }
2- Central triangle (half dimond)(for loop)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 27
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
27
#include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); n=m; for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" "); } n--; for(j=0;j<=(2*i);j++) { printf("*"); } printf("\n"); } getch(); return 0; }
3- Right hand triangle (while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m);
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 28
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
28
n=m; i=0; while(i<m) { i++; j=0; while(j<n) { j++; printf(" "); } n--; j=0; while(j<i) { j++; printf("*"); } printf("\n"); } getch(); return 0; }
4- Central triangle (half dimond)(while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); n=m; i=0; while(i<m)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 29
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
29
{ i++; j=0; while(j<n) { j++; printf(" "); } n--; j=0; while(j<((2*i)-1)) { j++; printf("*"); } printf("\n"); } getch(); return 0; }
Stars with starting and ending point
1- Left triangle (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of stars to start with : "); scanf("%i",&m); printf("Number of stars to end with : "); scanf("%i",&n); for(i=m;i<=n;i++) { for(j=0;j<i;j++) { printf("*");
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 30
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
30
} printf("\n"); } getch(); return 0; }
2- Left triangle with a condition (for loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of stars to start with : "); scanf("%i",&m); printf("Number of stars to end with : "); scanf("%i",&n);
if(m<=n)
{ for(i=m;i<=n;i++) { for(j=0;j<i;j++) { printf("*"); } printf("\n"); } } else { printf("Wrong set of values for input variables"); } getch(); return 0; }
3- Left triangle (while loop) #include <stdio.h> #include <conio.h>
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 31
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
31
int main (void) { int m,n,i,j; clrscr(); printf("Number of stars to start with : "); scanf("%i",&m); printf("Number of stars to end with : "); scanf("%i",&n); i=m; while(i<=n) { j=0; while(j<i) { printf("*"); j++; } i++; printf("\n"); } getch(); return 0; }
4- Left triangle with a condition (while loop) #include <stdio.h> #include <conio.h> int main (void) { int m,n,i,j; clrscr(); printf("Number of stars to start with : "); scanf("%i",&m); printf("Number of stars to end with : "); scanf("%i",&n); if(m<=n) { i=m; while(i<=n)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 32
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
32
{ j=0; while(j<i) { printf("*"); j++; } i++; printf("\n"); } } else { printf("Wrong set of values for input variables"); } getch(); return 0; }
Diamonds
1- Using for loop #include <stdio.h> #include <conio.h> int main (void) { int m,n,p,i,j; clrscr(); printf("Enter a number for the diamond : "); scanf("%i",&m); //upper triangle n=m; for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" ");
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 33
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
33
} n--; for(j=0;j<=(2*i);j++) { printf("*"); } printf("\n"); } //lower triangle p=0; for(i=0;i<=m;i++) { for(j=0;j<n;j++) { printf(" "); } n++; for(j=p;j<=(2*m);j++) { printf("*"); } p+=2; printf("\n"); } getch(); return 0; }
2- Using while loop #include <stdio.h> #include <conio.h> int main (void) { int m,n,p,i,j; clrscr(); printf("Number of lines of stars : "); scanf("%i",&m); //upper triangle n=m; i=0;
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 34
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
34
while(i<=m) { i++; j=0; while(j<n) { j++; printf(" "); } n--; j=0; while(j<((2*i)-1)) { j++; printf("*"); } printf("\n"); } //lower triangle n=1; p=0; i=0; while(i<m) { i++; j=0; while(j<n) { j++; printf(" "); } n++; j=p; while(j<((2*m)-1)) { j++; printf("*"); } p+=2; printf("\n"); } getch(); return 0; }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 35
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
35
Arrays and matrices
Initializing arrays 1- Initializing 1Darrays with user input
#include <stdio.h> #include <conio.h> int main (void) { int i; int a[5]; clrscr(); //initializing the array with zeros for(i=0;i<5;i++) { scanf("%i",&a[i]); } printf("\n\n"); //printing the initialized array for(i=0;i<5;i++) { printf("%i\n",a[i]); } getch(); return 0; }
2- Initializing 2Darrays with user input #include <stdio.h> #include <conio.h> int main (void) { int i,j; int a[3][3]; clrscr();
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 36
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
36
//initializing the array with user input for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%i",&a[i][j]); } } printf("\n\n"); //printing the initialized array for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%i\t",a[i][j]); } printf("\n"); } getch(); return 0; }
3- Initializing & printing 1Darrays with user input #include <stdio.h> #include <conio.h> int main (void) { int i; int a[10]; clrscr(); //printing the array without initializing it for(i=0;i<10;i++) { printf("%i\n",a[i]); } //initializing the array with zeros for(i=0;i<10;i++) { a[i] = 0; }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 37
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
37
//printing the initialized array for(i=0;i<10;i++) { printf("%i\n",a[i]); } getch(); return 0; }
4- Initializing & printing 2Darrays with user input #include <stdio.h> #include <conio.h> int main (void) { int i,j; int a[5][5]; clrscr(); //printing the array without initializing it for(i=0;i<5;i++) { for(j=0;j<5;j++) { printf("%i\t",a[i][j]); } printf("\n"); } //initializing the array with zeros for(i=0;i<5;i++) { for(j=0;j<5;j++) { a[i][j] = 0; } } //printing the initialized array for(i=0;i<5;i++) { for(j=0;j<5;j++)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 38
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
38
{ printf("%i\t",a[i][j]); } printf("\n"); } getch(); return 0; }
Initializing matrices
1- Initializing a 3x3 matrix antidaigonal #include <stdio.h> #include <conio.h> int main (void) { int i,j; int a[7][7]; clrscr(); //initializing the array with user input for(i=0;i<7;i++) { for(j=0;j<7;j++) { if((i+j)==6) a[i][j]=1; else a[i][j]=0; } } //printing the initialized array for(i=0;i<7;i++) { for(j=0;j<7;j++) { printf("%i\t",a[i][j]); } printf("\n"); }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 39
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
39
getch(); return 0; }
2- Initializing a 7x7 identity matrix
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 40
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
40
{ printf("%i\t",a[i][j]); }
printf("\n");
}
getch();
return 0;
}
3- Initializing a 7x7 matrix antidaigonal
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if((i+j)==6)
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 41
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
41
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{ printf("%i\t",a[i][j]); }
printf("\n");
}
getch();
return 0;
}
4- Initializing a 7x7 matrix cross
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(i==3 || j==3)
a[i][j]=1;
else
a[i][j]=0;
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 42
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
42
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{ printf("%i\t",a[i][j]);}
printf("\n");
}
getch();
return 0;
}
5- Initializing a 7x7 matrix full outergrid
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(i==0 || j==0 || i==6 || j==6)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 43
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
43
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{ printf("%i\t",a[i][j]); }
printf("\n");
}
getch();
return 0;
}
6- Initializing a 7x7 matrix half outergrid
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 44
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
44
for(j=0;j<7;j++)
{
if(i==0 || j==0)
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{ printf("%i\t",a[i][j]); }
printf("\n");
}
getch();
return 0;
}
7- Initializing a 7x7 matrix tridaigonal
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 45
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
45
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(i==j || i==(j+1) || i==(j-1))
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
8- Initializing a 7x7 matrix tridaigonal anti
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 46
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
46
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[7][7];
clrscr();
//initializing the array with user input
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if((i+j)==6 || (i+j)==5 || (i+j)==7)
a[i][j]=1;
else
a[i][j]=0;
}
}
//printing the initialized array
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{ printf("%i\t",a[i][j]); }
printf("\n");
}
getch();
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 47
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
47
return 0;
}
Matrix operations
1- Matrix scalar multiplication #include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j,c;
int a[4][4], b[4][4];
clrscr();
//Taking input from user for matrix A
printf("Input row-wise the matrix to be scaled :\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%i",&a[i][j]);
}
}
//Value to be scaled with
printf("\nEnter the constant to scale the input matrix with : ");
scanf("%i",&c);
//Scaling matrix A i.e. B = cA
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
b[i][j] = a[i][j] * c;
}
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 48
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
48
printf("\n\nThe input matrix was\n\n");
//printing matrix A
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
printf("\n\nThe ouput matrix is\n\n");
//printing matrix B
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",b[i][j]);
}
printf("\n");
}
getch();
return 0;
}
2- Matrix addition
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j;
int a[4][4], b[4][4], c[4][4],d[4][4];
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 49
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
49
clrscr();
//Taking input from user for matrix A
printf("Input matrix A row-wise : \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%i",&a[i][j]);
}
}
//Taking input from user for matrix B
printf("Input matrix B row-wise : \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%i",&b[i][j]);
}
}
//Adding the matrices i.e. C = A + B
for(i=0;i<4;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 50
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
50
for(j=0;j<4;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
//Subtracting the matrices i.e. C = A - B
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
d[i][j] = a[i][j] - b[i][j];
}
}
printf("\n\nThe input matrix A is\n\n");
//printing matrix A
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 51
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
51
printf("\n\nThe input matrix B is\n\n");
//printing matrix B
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",b[i][j]);
}
printf("\n");
}
printf("\n\nThe output matrix C is\n\n");
//printing matrix C
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",c[i][j]);
}
printf("\n");
}
printf("\n\nThe output matrix D is\n\n");
//printing matrix D
for(i=0;i<4;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 52
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
52
for(j=0;j<4;j++)
{
printf("%i\t",d[i][j]);
}
printf("\n");
}
getch();
return 0;
}
3- Matrix multiplication 3x3
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j,k;
int a[3][3], b[3][3], c[3][3];
clrscr();
//Taking input from user for matrix A
printf("Input row-wise matrix A :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%i",&a[i][j]);
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 53
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
53
}
//Taking input from user for matrix B
printf("Input row-wise matrix B :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%i",&b[i][j]);
}
}
//Initializing matrix c to zero
//Extremely necessary step
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = 0;
}
}
//Code for multiplication of matrices
//Multiplying matrices A and B i.e. C = A x B
for(i=0;i<3;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 54
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
54
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\n\nThe input matrix A is\n\n");
//printing matrix A
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
printf("\n\nThe input matrix B is\n\n");
//printing matrix B
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("%i\t",b[i][j]); }
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 55
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
55
printf("\n");
}
printf("\n\nThe multiplied matrix C is\n\n");
//printing matrix B
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("%i\t",c[i][j]); }
printf("\n");
}
getch();
return 0;
}
4- Matrix multiplication 4x4
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j,k;
int a[4][4], b[4][4], c[4][4];
clrscr();
//Taking input from user for matrix A
printf("Input row-wise matrix A :\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 56
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
56
{
scanf("%i",&a[i][j]);
}
}
//Taking input from user for matrix B
printf("Input row-wise matrix B :\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%i",&b[i][j]);
}
}
//Initializing matrix c to zero
//Extremely necessary step
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
c[i][j] = 0;
}
}
//Code for multiplication of matrices
//Multiplying matrices A and B i.e. C = A x B
for(i=0;i<4;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 57
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
57
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\n\nThe input matrix A is\n\n");
//printing matrix A
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
printf("\n\nThe input matrix B is\n\n");
//printing matrix B
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",b[i][j]);
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 58
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
58
printf("\n");
}
printf("\n\nThe multiplied matrix C is\n\n");
//printing matrix B
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%i\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
5- Matrix multiplication 8x8
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j,k;
int a[8][8], b[8][8], c[8][8];
clrscr();
//Initializing matrix A
for(i=0;i<8;i++)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 59
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
59
for(j=0;j<8;j++)
{
a[i][j] = i + j;;
}
}
//Initializing matrix B to an identity matrix
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(i==j)
b[i][j] = 1;
else
b[i][j] = 0;
}
}
//Initializing matrix c to zero
//Extremely necessary step
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
c[i][j] = 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 60
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
60
}
//Code for multiplication of matrices
//Multiplying matrices A and B i.e. C = A x B
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<8;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\n\nThe matrix A is\n\n");
//printing matrix A
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%i\t",a[i][j]);
}
printf("\n");
}
printf("\n\nThe matrix B is\n\n");
//printing matrix B
for(i=0;i<8;i++)
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 61
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
61
{
for(j=0;j<8;j++)
{
printf("%i\t",b[i][j]);
}
printf("\n");
}
printf("\n\nThe multiplied matrix C is\n\n");
//printing matrix B
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%i\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 62
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
62
Nested loops 1- Nested for loop
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i,j,k;
clrscr();
for(i=0;i<3;i++)
{
printf("Hello A\t");
for(j=0;j<3;j++)
{
printf("Hello B\t");
for(k=0;k<3;k++)
{
printf("Hello C\t");
}
}
}
getch();
return 0;
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 63
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
63
2- Nested while loop
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i=0,j=0,k=0;
clrscr();
i=0;
while(i<3)
{
i++;
printf("Hello A\t");
j=0;
while(j<3)
{
j++;
printf("Hello B\t");
k=0;
while(k<3)
{
k++;
printf("Hello C\t");
}
}
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 64
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
64
getch();
return 0;
}
3- Nested while test1
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i=0,j=0,k=0;
clrscr();
i=0;
while(i<3)
{
i++;
printf("Hello A\t");
j=0;
while(j<3)
{
j++;
printf("Hello B\t");
//k=0 line removed
while(k<3)
{
k++;
printf("Hello C\t");
}
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 65
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
65
}
}
getch();
return 0;
}
4- Nested while test2
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i=0,j=0,k=0;
clrscr();
i=0;
while(i<3)
{
i++;
printf("Hello A\t");
//j=0 line removed
while(j<3)
{
j++;
printf("Hello B\t");
k=0;
while(k<3)
{
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 66
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
66
k++;
printf("Hello C\t");
}
}
}
getch();
return 0;
}
3- Nested while test3
#include <stdio.h>
#include <conio.h>
int main (void)
{
int i=0,j=0,k=0;
clrscr();
i=0;
while(i<3)
{
i++;
printf("Hello A\t");
//j=0 line removed
while(j<3)
{
j++;
printf("Hello B\t");
//k=0 line removed
I m p o r t a n t P r o g r a m s o f C l a n g u a g e P a g e | 67
myUET | The UETians Online Community | www.myUET.net.tc | www.myUET.netai.net
67
while(k<3)
{
k++;
printf("Hello C\t");
}
}
}
getch();
return 0;
}
We are waiting for your comments and suggestions.
Pass your comments at:
www.myUET.net.tc
www.myUET.netai.net
Or email at: