0% found this document useful (0 votes)
21 views18 pages

Maths

The document contains multiple C programs demonstrating various programming concepts such as finding squares of numbers using different loops, counting vowels and consonants in a string, generating Fibonacci series, calculating factorials, and performing matrix operations (addition, subtraction, multiplication). Each program includes user input and outputs the results accordingly. The programs serve as examples for learning basic programming structures and algorithms.

Uploaded by

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

Maths

The document contains multiple C programs demonstrating various programming concepts such as finding squares of numbers using different loops, counting vowels and consonants in a string, generating Fibonacci series, calculating factorials, and performing matrix operations (addition, subtraction, multiplication). Each program includes user input and outputs the results accordingly. The programs serve as examples for learning basic programming structures and algorithms.

Uploaded by

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

/*Program to find the square of numbers using while loop*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

int i,n;

i=1;

printf("Enter the value of n:");

scanf("%d",&n);

while(i<=n)

{
printf("The square value of %d is %d\n",i,i*i);
i++;
}
getch();

}
OUTPUT:

Enter the value of n:10


The square value of 1 is 1
The square value of 2 is 4
The square value of 3 is 9
The square value of 4 is 16
The square value of 5 is 25
The square value of 6 is 36
The square value of 7 is 49
The square value of 8 is 64
The square value of 9 is 81
The square value of 10 is 100
/*Program to find the square of numbers using for loop*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("The square value of %d is %d \n",i,i*i);
}
getch();
}

OUTPUT

Enter the value of n:12


The square value of 1 is 1
The square value of 2 is 4
The square value of 3 is 9
The square value of 4 is 16
The square value of 5 is 25
The square value of 6 is 36
The square value of 7 is 49
The square value of 8 is 64
The square value of 9 is 81
The square value of 10 is 100
The square value of 11 is 121
The square value of 12 is 144
/*Program to find the square of numbers using do while loop*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

int i,n;
i=1;printf("enter the value of n:");
scanf("%d",&n);
do
{
printf("the square value of %d is %d\n",i,i*i);
i++;
}
while(i<=n);
getch();
}
OUTPUT

Enter the value of n:10

The square value of 1 is 1


The square value of 2 is 4
The square value of 3 is 9
The square value of 4 is 16
The square value of 5 is 25
The square value of 6 is 36
The square value of 7 is 49
The square value of 8 is 64
The square value of 9 is 81
The square value of 10 is 100
/*Program to find the square of the numbers using goto statement*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
printf("Enter the value of n:");
scanf("%d",&n);
i=1;
L:
printf("The square value of %d is %d\n",i,i*i);
i++;
if(i<=n)
goto L;
getch();
}
OUTPUT

Enter the value of n:10


The square value of 1 is 1
The square value of 2 is 4
The square value of 3 is 9
The square value of 4 is 16
The square value of 5 is 25
The square value of 6 is 36
The square value of 7 is 49
The square value of 8 is 64
The square value of 9 is 81
The square value of 10 is 100
/*Program to find the number of vowels and consonents in a given string*/
#include<stdio.h>
#include<conio.h>
void main()
{
int vow,con,i;
char str[50],ch;
vow=0;
con=0;
printf("Enter any sentence:");
gets(str);
for(i=0;str[i]!='\0';i++)
{
ch=toupper(str[i]);
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vow++;
break;
default:
if(ch>=65&&ch<=91)
con++;
break;
}
}
printf("Number of vowels:%d\n",vow);
printf("Number of consonents:%d\n",con);
getch();
}
OUTPUT
Enter any sentence:MATHEMATICS

Number of vowels:4

Number of consonents:7
/*Program to find the character between two given character*/
#include<stdio.h>
#include<conio.h>
main()
{
char sc,ec;
int i;
printf("\n Enter the first character:");
sc=getche();
printf("\n Enter the ending character:");
ec=getche();
printf("\n The character between %c and %c is:\n",sc,ec);
if(sc==ec)
printf("%c",sc);
else
if(sc<=ec)
{
for(i=sc;i<=ec;i++)
printf("%c",i);
}
else
{for(i=sc;i>=ec;i--)
printf("%c",i);
}
getch();
}
OUTPUT

Enter the first character:A


Enter the ending character:Z
The character between A and Z is:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
/*Program to find Ascii value for given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num;
printf("Enter any number : ");
scanf("%d",&n);
num=(n>=0&&n<=9)?(n+48):-1;
printf("\n Ascii value of the number %d",num);
getch();
}
OUTPUT

Enter any number : 7


Ascii value of the number 55

/*Program to print the prinme number between to given number*/


#include<stdio.h>
#include<conio.h>
main()
{
int sv,ev,n,d,dividers;
printf("Enter the starting value : ");
scanf("%d",&sv);
printf("Enter the ending value : ");
scanf("%d",&ev);
printf("\n Prime number between %d and %d are:\n",sv,ev);
for(n=sv;n<=ev;n++)
{
dividers=0;
for(d=1;d<=n;d++)
{if(n%d==0)
dividers++;
}
if(dividers==2)
printf("%5d",n);
}
getch();
}
OUTPUT

Enter the starting value : 2


Enter the ending value : 36

Prime number between 2 and 36 are:


2 3 5 7 11 13 17 19 23 29 31

/*Program to find thr fibonacci series*/


#include<stdio.h>
#include<conio.h>
main()
{
int n;
printf("Enter the value of n :");
scanf("%d",&n);
printf("\n Fibonacci series :\n");
fib(n);
getch();
}
fib(int n)
{
int f1,f2,f3,i;
f1=-1;
f2=1;
for(i=1;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf("%5d",f3);
}
}
OUTPUT

Enter the value of n :10


Fibonacci series :
0 1 1 2 3 5 8 13 21 34

/*Program to find factorial for given number*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Factorial value of %d is %d",n,fact(n));
getch();
}
fact(int n)
{
return((n>0)?n*fact(n-1):1);
}
OUTPUT

Enter the value of n:6


Factorial value of 6 is 720

/*program to find the power of x to y*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y;
printf("Enter the value of x:");
scanf("%d",&x);
printf("Enter the value of y:");
scanf("%d",&y);
printf("power(%d,%d)ie,%d^%d is %d",x,y,x,y,power(x,y));
getch();
}
power(int x,int y)
{
int i,f=1;
for(i=1;i<=y;i++)
{
f=f*x;
}
return(f);
}
OUTPUT

Enter the value of x:2


Enter the value of y:5
power(2,5)ie,2^5 is 32

/*Program for printing pascal triangle*/


#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
long pascaltriangle (int n,int i)
{
if (n==i||i==0)
return 1;
else
return pascaltriangle (n-1,i)+pascaltriangle(n-1,i-1);
}
int main()
{
int n,i,j,space=0;
printf("Enter the value of lines:");
scanf("%d",&n);
for(i=0;n>=i;i++)
{
for(space=0;space<n-4;space++)
printf("\n");
for(j=0;i>=j;j++)
{
long res=pascaltriangle(i,j);
printf("%d",res);
printf("\t");
}
}
return 0;
}
OUTPUT
Enter the value of lines:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

/*program to find the addition of two matrix*/


#include<math.h>
#include<stdio.h>
main()
{
int i,j,a[3][3],b[3][3],c[i][j];
printf("\n Enter the element of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the element of a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the element of second matrix:\n");
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
{
printf("\n Enter the element of b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n sum of two matrices:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
Enter the element of first matrix:

Enter the element of a[0][0]:5


Enter the element of a[0][1]:6

Enter the element of a[0][2]:7

Enter the element of a[1][0]:5

Enter the element of a[1][1]:6

Enter the element of a[1][2]:7

Enter the element of a[2][0]:5

Enter the element of a[2][1]:6

Enter the element of a[2][2]:7

Enter the element of second matrix:

Enter the element of b[0][0]:5

Enter the element of b[0][1]:6

Enter the element of b[0][2]:7

Enter the element of b[1][0]:5

Enter the element of b[1][1]:6

Enter the element of b[1][2]:7

Enter the element of b[2][0]:5

Enter the element of b[2][1]:6

Enter the element of b[2][2]:7

Sum of two matrices:


10 12 14
10 12 14
10 12 14

/*Program to find the subtraction of two matrix matrix*/


#include<math.h>
#include<stdio.h>
main()
{
int i,j,a[3][3],b[3][3],c[i][j];
printf("\n Enter the element of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the element of a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the element of second matrix:\n");
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
{
printf("\n Enter the element of b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n subtraction of two matrices:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
Enter the element of first matrix:

Enter the element of a[0][0]:5


Enter the element of a[0][1]:6

Enter the element of a[0][2]:7

Enter the element of a[1][0]:5

Enter the element of a[1][1]:6

Enter the element of a[1][2]:7

Enter the element of a[2][0]:8

Enter the element of a[2][1]:9

Enter the element of a[2][2]:8

Enter the element of second matrix:

Enter the element of b[0][0]:1

Enter the element of b[0][1]:2

Enter the element of b[0][2]:3

Enter the element of b[1][0]:4

Enter the element of b[1][1]:3

Enter the element of b[1][2]:2

Enter the element of b[2][0]:3

Enter the element of b[2][1]:2

Enter the element of b[2][2]:1


Subtraction of two matrices:
4 4 4
1 3 5
5 7 7

/* Program to find the mutiplication of two matrix*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,k,a[3][3],b[3][3],mul[3][3];
printf("\n Enter the elements of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n the value of a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the value of second matrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n the value of b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
k=i==j;
{
for(k=0;k<3;k++)
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplied matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT
Enter the elements of first matrix:

the value of a[0][0]:2

the value of a[0][1]:2

the value of a[0][2]:-1

the value of a[1][0]:2

the value of a[1][1]:-1

the value of a[1][2]:2

the value of a[2][0]:-1

the value of a[2][1]:2

the valu of a[2][2]:2

Enter the value of second matrix :

the value of b[0][0]:2

the value of b[0][1]:2

the value of b[0][2]:-1

the value of b[1][0]:2

the value of b[1][1]:-1

the value of b[1][2]:2

the value of b[2][0]:-1

the value of b[2][1]:2

the value of b[2][2]:2


Multiplied matrix is :
9 0 0
0 9 0
0 0 9

You might also like