You are on page 1of 12

1|Page

Programming with C/C++


Math Practical IV, A.MTH-3202

1. Write C program to print n integer number.


#include<stdio.h>
int main()
{
int i,n;
printf("Enter the number n= ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(" %d \n",i);
}
}

2. Write c program to find summation of n integer.

#include<stdio.h>

int main()

int sum=0,n,i;

printf("Enter n= ");

scanf("%d",&n);

for(i=1;i<=n;i++)

sum=sum+i;

printf(" = %d\n",sum);

3. Write C program to find the summation of the series


1+1/2+1/3+1/4…..1/n.

#include<stdio.h>

int main()

double sum=0,n,i;
2|Page

printf("Enter n= ");

scanf("%lf",&n);

for(i=1;i<=n;i++)

sum=sum+(1/i);

printf(" = %.2lf\n",sum);

4. Write a c program to print the following pattern

*
**
***
****
*****
#include<stdio.h>
main()
{
int n, row, col;
printf("Enter N= ");
scanf("%d", &n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("* ");
}
printf("\n");
}

5.Write a c program to print the following pattern


1
12
123
1234
12345
#include<stdio.h>
main()
3|Page

{
int n, row, col;
printf("Enter N= ");
scanf("%d", &n);
for(row=1;row<=n;row++)
{
for(col=1;col<=row;col++)
{
printf("%d ",col);
}
printf("\n");
}

6. Write a c program to print Floyd’s triangle


1
23
456
7 8 9 10
11 12 13 14 15
#include<stdio.h>

int main()
{
int n, i, j, x = 1;
printf("Enter number of row: ");
scanf("%d", &n);

for(i = 0; i < n; i++)


{
for(j = 0; j <= i; j++)
{
printf("%d ", x++);
}
printf("\n");
}
}

7. Write a C program to find prime number of a given positive integer


#include<stdio.h>
main()
{
int num, count=0,i;
4|Page

printf("Enter any positive number:\n");


scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0)
printf(" is a prime number\n");
else
printf("not a prime number");
}

8. Write a C Program to print the prime number up to n integer.


#include<stdio.h>
main()
{
int num,i,count=0,j;
printf("Enter a number :\n");
scanf("%d",&num);
printf("The prime numbers upto %d are \n",num);
for(i=1;i<=num;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
printf("%d\n",i);
}
}
}

9.write a C program to find the factorial any given integer.


#include<stdio.h>
main()
5|Page

{
int i, fact=1, n;
printf("Enter any positive number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(" the factorial of the number is %d\n",fact);
}

10. write a C program which will sort out the given numbers in ascending order.
#include<stdio.h>

int main()
{
int n, i, j, x;
printf("Enter number of elements: ");
scanf("%d", &n);

int num[n];
for(i = 0; i < n; i++)
scanf("%d", &num[i]);

for(i = 0; i < n; i++)


{
for(j = 0; j < n-1; j++)
{
if(num[j + 1] < num[j])
{
x = num[j];
num[j] = num[j+1];
num[j+1] = x;
}
}
}

printf("\nascending order of given numbers:\n");//lower to higher


for(i = 0; i < n; i++)
printf("%d ", num[i]);

printf("\n");

}
6|Page

11.write a C program which will find out the large number between two given number.
#include<stdio.h>
main()
{
int num1, num2;
printf("Enter two numbers:");
scanf("%d %d",&num1,&num2);
if(num1>num2)
printf("%d is the large number\n",num1);
else if(num1<num2)
printf("%d is the large number\n",num2);
else
printf("equal");
}

12. write a C program to evaluate the GCD & LCD from two numbers.
#include<stdio.h>
int main()
{
int num1,num2,n1,n2,rem,gcd,lcm;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
n1=num1;
n2=num2;
while(n2!=0)
{
rem=n1%n2;
n1=n2;
n2=rem;
}
gcd=n1;
lcm=(num1*num2)/gcd;
printf("GCD=%d\n",gcd);
printf("LCM=%d\n",lcm);
}

13.write a c Program which will calculate the addition and subtraction of two given matrix of any
order.

#include<stdio.h>

int main()
7|Page

{
int n, m, i, j;
printf("Enter Your Matrices row and column Sizes n and m: ");
scanf("%d %d", &n, &m);

int Mat1[n][m], Mat2[n][m];


printf("Enter Your first Matrix:\n");
for(i = 0; i < n ; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &Mat1[i][j]);
}
}
printf("Enter Your Second Matrix:\n");
for(i = 0; i < n ; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &Mat2[i][j]);
}
}

printf("Addition of given two Matrices:\n");


for(i = 0; i < n ; i++)
{
for(j = 0; j < m; j++)
{
printf("%d ", Mat1[i][j]+Mat2[i][j]);
}
printf("\n");
}

printf("Substraction of given two Matrices:\n");


for(i = 0; i < n ; i++)
{
for(j = 0; j < m; j++)
{
printf("%d ", Mat1[i][j]-Mat2[i][j]);
}
printf("\n");
}
}
8|Page

14. write a C program to find the multiplication of any two matrix.

#include<stdio.h>

main()
{
int n1, m1, n2, m2, i, j, k, pdt;
printf("Enter the first Matrix row and column sizes n1 and m1: ");
scanf("%d %d", &n1, &m1);
int Mat1[n1][m1];
printf("Enter Your first Matrix:\n");
for(i = 0; i < n1 ; i++)
{
for(j = 0; j < m1; j++)
{
scanf("%d", &Mat1[i][j]);
}
}

printf("Column size of first Matrix must be equal to the row size of second Matrix.\n");
printf("Enter the second Matrix row and column sizes n2 and m2: ");
scanf("%d %d", &n2, &m2);
int Mat2[n2][m2];
printf("Enter Your second Matrix:\n");
for(i = 0; i < n2 ; i++)
{
for(j = 0; j < m2; j++)
{
scanf("%d", &Mat2[i][j]);
}
}

printf("\nMultiplication of Given Two Matrices:\n");


for(i = 0; i < n1; i++)
{
for(j = 0; j < m2; j++)
{
pdt = 0;
for(k = 0; k < n2; k++)
{
pdt += Mat1[i][k]*Mat2[k][j];
}
9|Page

printf("%d ", pdt);


}
printf("\n");
}

}
15.write a C program to find the transpose matrix of a given matrix.
#include<stdio.h>

int main()
{
int i, j, n, m;

printf("Enter Your Matrix Size: ");


scanf("%d %d", &n, &m);
int x, Transpose[m][n];
printf("Enter Your Matrix:\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &x);
Transpose[j][i] = x;
}
}

printf("\nThe transpose of the given matrix is:\n");


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d ", Transpose[i][j]);
}
printf("\n");
}
return 0;
}
𝟎.𝟖 𝟏
16. write a C program to find the value of the integration f(x)=∫𝟎 𝟏+𝒙𝟐
, for n=6 using Simpson’s 1/3
rule.

#include<stdio.h>
#define f(x) 1/(1+x*x)
int main()
{
10 | P a g e

double l_lim, u_lim;


int n = 6, i, j;
printf("Enter lower limit and upper limit of the integral:\n");
scanf("%lf %lf", &l_lim, &u_lim);
double h = (u_lim - l_lim)/n;
double x[10], y[10];
for(i = 0; i <= n; i++)
{
x[i] = l_lim + i*h;
y[i] = f(x[i]);
}
double res = 0;
for(i = 0; i <= n; i++)
{
if(i==0 || i==n) res += y[i];
else if(i%2 != 0) res += 4*y[i];
else res += 2*y[i];
}
res = (h/3)*res;
printf("\nIntegration obtained by simpson 1/3 rule is %lf\n", res);
}

𝟎.𝟖 𝟏
17. write a C program to find the value of the function f(x)=∫𝟎 𝟏+𝒙
, for n=5 using Simpson’s 3/8 rule.
#include<stdio.h>
#define f(x) 1/(1+x)
int main()
{
double l_lim, u_lim;
int n = 5, i, j;
printf("Enter lower limit and upper limit of the integral:\n");
scanf("%lf %lf", &l_lim, &u_lim);
double h = (u_lim - l_lim)/n;
double x[10], y[10];
for(i = 0; i <= n; i++)
{
x[i] = l_lim + i*h;
y[i] = f(x[i]);
}
double res = 0;
for(i = 0; i <= n; i++)
{
if(i==0 || i==n) res += y[i];
else if(i%3 == 0) res += 2*y[i];
else res += 3*y[i];
11 | P a g e

}
res = (3*h/8)*res;
printf("\nIntegration obtained by simpson 3/8 rule is %lf\n", res);
}

18. Write C program for interpolation of newton’s forward method.


#include<stdio.h>

double u_cal(double u, int n)


{
double temp = u;
int j;
for(j = 1; j < n; j++)
temp *= (u+j);
return temp;
}

int fact(int n)
{
int f = 1, i;
for(i = 2; i <= n; i++) f *= i;
return f;
}

int main()
{
int n, i, j;
printf("Enter the value of n:\n");
scanf("%d", &n);
double x[n], y[n][n], a;
printf("\nEnter the values in form x,y:\n");
for(i = 0; i < n; i++)
scanf("%lf %lf", &x[i], &y[i][0]);

printf("\nEnter the value of a for which the value of y is wanted: \n");


scanf("%lf", &a);

///calculating the forward difference table


for(i = 1; i < n ; i++)
{
for(j = 0; j < n-i; j++)
{
y[j][i] = y[j+1][i-1] - y[j][i-1];
}
12 | P a g e

}
///Displaying the forward difference table
for(i = 0; i < n; i++)
{
printf("%.4lf\t", x[i]);
for(j = 0; j < n-i; j++)
{
printf("%.4lf\t",y[i][j]);
}
printf("\n");
}

///initializing u and sum


double u, sum;
sum = y[0][0];
u = (a - x[0])/(x[1]-x[0]);
for(i = 1; i < n; i++)
{
sum += (u_cal(u,i)*y[0][i])/fact(i);
}
printf("\nValue at %.0lf is %lf\n", a, sum);

You might also like