0% found this document useful (0 votes)
3 views84 pages

Computer Programming [MAT 301]

This document is a practical file for a Computer Programming course submitted by Sultan Siddiquie at Tri-Chandra Multiple Campus. It includes a comprehensive index of 33 C programming projects, covering various topics such as leap year determination, geometric calculations, number theory, and matrix operations. Each program is accompanied by source code and execution details.

Uploaded by

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

Computer Programming [MAT 301]

This document is a practical file for a Computer Programming course submitted by Sultan Siddiquie at Tri-Chandra Multiple Campus. It includes a comprehensive index of 33 C programming projects, covering various topics such as leap year determination, geometric calculations, number theory, and matrix operations. Each program is accompanied by source code and execution details.

Uploaded by

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

PRACTICAL FILE OF COMPUTER PROGRAMMING

Submitted to
The Department of Mathematics
Tri-Chandra Multiple Campus
Ghantaghar, Kathmandu

For the partial fulfillment of final examination of


Computer Programming [MAT 301]
of
4 years B.Sc. - 3rd year

Submitted by
Sultan Siddiquie
PRACTICAL FILE OF COMPUTER PROGRAMMING

Submitted to:
The Department of Mathematics
Tri-Chandra Multiple Campus
Ghantaghar, Kathmandu

For the partial fulfillment of final examination of


Computer Programming [MAT 301]

Submitted by:
Sultan Siddiquie
TU Exam Symbol No.: 500371433
TU Registration No.: 5-2-37-981-2020
Index of Programs
Name: Sultan Siddiquie Exam Symbol No.:
Campus Roll No.: 09/077

S. N. Objectives Page No.


Program 01: Write a C program to determine whether a given year is a 01-02
leap year.
Program 02: Write a C program to find surface area, volume of a right 03-05
cone, cylinder, and sphere.
Program 03: Write a C program to convert temperature in Centigrade to 06-07
Fahrenheit and vice-versa.
Program 04: Write a C program to find reverse of a number. 08-09

Program 05: Write a C program to identify whether a given number is 10-12


perfect, and also display perfect numbers between two given
numbers.
Program 06: Write a C program to identify whether a given number is 13-15
prime, and also display prime numbers between two given
numbers.
Program 07: Write a C program to identify whether a given number is 16-18
Armstrong, and also display Armstrong numbers between
two given numbers.
Program 08: Write a C program to identify whether a given number is 19-21
strong, and also display strong numbers between two given
numbers.
Program 09: Write a C program to find HCF and LCM of two given 22-23
numbers.
Program 10: Write a C program to convert decimal number into fraction. 24-25

Program 11: Write a C program to convert lower case letters in a sentence 26-27
into upper case using ASCII values.
Program 12: Write a C program to convert binary into hexadecimal. 28-29

Program 13: Write a C program to convert hexadecimal into binary. 30-31

Program 14: Write a C program to hexadecimal into denary number 32-33


system.
Program 15: Write a C program to find the sum of two given matrices. 34-35

Program 16: Write a C program to find the transpose of a given matrix. 36-37

Program 17: Write a C program to find the product of two given matrices. 38-40

Program 18: Write a C program to display Floyd triangle pattern. 41-42


S.N. Objectives Page No.

Program 19: Write a C program to display Pascal triangle pattern. 43-44

Program 20: Write a C program to prepare a result taking marks in 4 45-47


papers, and give grade secured by a particular student.
Program 21: Write a C program to find permutation and combination of 48-49
given things taken some at a time.
Program 22: Write a C program to generate Fibonacci sequence of given 50-51
terms, and also find the sum (using recursive function).
Program 23: Write a C program to find HCF of given numbers (more than 52-53
two).
Program 24: Write a C program that uses calloc function to find sum of two 54-55
given numbers.
Program 25: Write a C program that uses malloc function to allocate 56-58
memory, also readjust the allocation using realloc function.
Program 26: Write a C program that creates a data file containing 59-60
structure of n records.
Program 27: Write a C program that creates a data file containing records 61-64
of product with field name, product code, product name,
quantity, and price. Also, append the file with more records of
products.
Program 28: Write a C program to solve by using 65-66
bisection method. (Take error of tolerance from user.)
Program 29: Write a C program to solve by using Newton- 67-68
Raphson method. (Take error of tolerance from user.)
Program 30: Write a C program to evaluate ∫ √ by using 69-70
trapezoidal rule. (Take number of sub-intervals from user.)
Program 31: Write a C program to evaluate ∫ √ by using 71-72
Simpson’s 1/3 rule. (Take number of sub-intervals from
user.)
Program 32: Write a C program to solve 73-75
by Gauss elimination method.
Program 33: Write a C program to solve 76-78
by Gauss-Seidel method.
Prepared by: Sultan Sidddiquie

Program 01:
Write a C program to determine whether a given year is a leap year.

Program:

//01. Leap Year


#include<stdio.h>
#include<time.h>
int main()
{
int y;
printf(" Enter the year in AD:\t");
scanf("%d",&y);
if(y>=0)
if(y%400==0)
{
printf("\n %d is a leap year.",y);
}
else if(y%4==0&&y%100!=0)
{
printf("\n %d is a leap year.",y);
}
else
{
printf("\n %d is not a leap year.",y);
}
else
printf("\n Please enter valid date. ");
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d\n",tm.tm_year+1900,
tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

1
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 01:
Write a C program to determine whether a given year is a leap year.

Output:

2
Prepared by: Sultan Sidddiquie

Program 02:
Write a C program to find surface area, volume of a right cone, cylinder, and sphere.

Program:

//02. surface area, volume


#include<stdio.h>
#include<time.h>
#define Pi 3.14159
int main()
{
float r, l, h;
float A1, A2, A3, V1, V2, V3;
int choice;
/* A1=Area of Right Cone, A2=Area of Cylinder, A3=Area of Sphere,
V1=Volume of Right Cone, V2=Volume of Cylinder, V3=Volume of Sphere;
r=radius, l=slant length, h=vertical height */
printf(" Type 1 for Right Cone\n Type 2 for Cylinder\n Type 3 for Sphere\n ");
scanf("%d",&choice);
if(choice==1)
{
printf(" Enter the radius (r):\t");
scanf("%f",&r);
printf(" Enter the slant length (l):\t");
scanf("%f",&l);
printf(" Enter the vertical height (h):\t");
scanf("%f",&h);
A1=Pi*r*(r+l);
V1=(Pi*r*r*h)/3;
if(r>=0&&l>=0&&h>=0)
{
printf("\n The total surface area of the right cone is %.2f square units.",A1);
printf("\n The volume of the right cone is %.2f cubic units.",V1);
}
else
{
printf(" Please enter valid values.");
}
}
else if(choice==2)
{
printf(" Enter the radius (r):\t");
scanf("%f",&r);
printf(" Enter the vertical height (h):\t");
scanf("%f",&h);
A2=2*Pi*r*(r+h);
V2=Pi*r*r*h;
if(r>=0&&h>=0)
{
printf("\n The total surface area of the cylinder is %.2f square units.",A2);
printf("\n The volume of the cylinder is %.2f cubic units.",V2);

3
Prepared by: Sultan Sidddiquie

}
else
{
printf(" Please enter valid values.");
}
}
else if(choice==3)
{
printf(" Enter the radius (r):\t");
scanf("%f",&r);
A3=4*Pi*r*r;
V3=(4*Pi*r*r*r)/3;
if(r>=0)
{
printf("\n The total surface area of the sphere is %.2f square units.",A3);
printf("\n The volume of the sphere is %.2f cubic units.",V3);
}
else
{
printf(" Please enter valid value.");
}
}
else
{
printf(" INVALID SELECTION");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

4
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 02:
Write a C program to find surface area, volume of a right cone, cylinder, and sphere.

Output:

5
Prepared by: Sultan Sidddiquie

Program 03:
Write a C program to convert temperature in Centigrade to Fahrenheit and vice versa.

Program:

//03. Temperature Conversion


#include<stdio.h>
#include<time.h>
int main()
{
int selection;
float C, F;
printf(" Type 1 to convert from centigrade to fahrenheit.\n");
printf(" Type 2 to convert from fahrenheit to centigrade.\n");
scanf("%d",&selection);
if(selection==1)
{
printf(" Enter the centigrade value:\t");
scanf("%f",&C);
F=(1.8*C)+32;
printf(" The fahrenheit value of %.2f C is %.2f F.",C,F);
}
else if(selection==2)
{
printf(" Enter the fahrenheit value:\t");
scanf("%f",&F);
C=(F-32)/1.8;
printf(" The centigrade value of %.2f F is %.2f C.",F,C);
}
else
{
printf("\n INVALID SELECTION");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

6
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 03:
Write a C program to convert temperature in Centigrade to Fahrenheit and vice versa.

Output:

7
Prepared by: Sultan Sidddiquie

Program 04:
Write a C program to find reverse of a number.

Program:

//04.Reverse of a number
#include<stdio.h>
#include<time.h>
int main()
{
int n, r, N;
//r is remainder, rev is reverse which is initialized with 0.
printf(" Enter the number:\t");
scanf("%d", &n);
int rev=0;
N=n;
while (n!=0)
{
r = n%10;
rev = rev*10 + r;
n = n/10;
}
printf(" The reverse of %d is %d ",N,rev);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

8
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 04:
Write a C program to find reverse of a number.

Output:

9
Prepared by: Sultan Sidddiquie

Program 05:
Write a C program to identify whether a given number is perfect, and also display perfect numbers
between two given numbers.

Program:

//05. Perfect Numbers


#include<stdio.h>
#include<time.h>
int main()
{
int i, j, k, m, n, p, s;
int choice;
printf(" Type 1 to test perfect number.\n");
printf(" Type 2 to display perfect numbers in a given range.\n ");
scanf("%d",&choice);
if(choice==1)
{
printf(" Enter the number: ");
scanf("%d",&n);
s=0;
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(n==s)
printf(" %d is perfect number.",n);
else
printf(" %d is not perfect number.",n);
}
else if(choice==2)
{
printf(" Enter the range:\t");
scanf("%d %d",&m,&p);
printf(" Perfect numbers between %d and %d are: ",m,p);
for(j=m;j<p;j++)
{
s=0;
for(k=1;k<j;k++)
{
if(j%k==0)
s=s+k;
}
if(j==s)
printf(" %d ",j);
}
}
else
{
printf(" INVALID SELECTION");

10
Prepared by: Sultan Sidddiquie

}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

11
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 05:
Write a C program to identify whether a given number is perfect, and also display perfect numbers between
two given numbers.

Output:

12
Prepared by: Sultan Sidddiquie

Program 06:
Write a C program to identify whether a given number is prime, and also display prime numbers
between two given numbers.

Program:

//06. Prime numbers


#include<stdio.h>
#include<time.h>
int main ()
{
int choice;
printf(" Type 1 to test prime number.\n");
printf(" Type 2 to display prime numbers in a given range.\n ");
scanf("%d",&choice);
if(choice==1)
{
int n, i, k=0, r;
printf(" Enter the number:\t");
scanf("%d",&n);
if (n<2)
{
printf("\n Undefined");
}
else
{
for (i=2;i<=n/2;i++)
if (n%i==0)
k=k+1;
if(k==0)
printf("\n %d is Prime Number.",n);
else
printf("\n %d is not Prime Number.",n);
}
}
else if(choice==2)
{
int p, j, a, b, c;
printf("\n Enter the range:\t");
scanf("%d %d",&a,&b);
printf("\n The prime numbers between %d and %d are:\t",a,b);
for(p=a+1;p<b;p++)
{
c=0;
if(p<=1)
continue;
for(j=2;j<=p/2;j++)
{
if(p%j==0)
{
c=1;

13
Prepared by: Sultan Sidddiquie

break;
}
}
if(c==0)
{
printf(" %d " ,p);
}
}
}
else
{
printf(" INVALID SELECTION");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

14
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 06:
Write a C program to identify whether a given number is prime, and also display prime numbers between two
given numbers.

Output:

15
Prepared by: Sultan Sidddiquie

Program 07:
Write a C program to identify whether a given number is Armstrong, and also display Armstrong
numbers between two given numbers.

Program:

//07. Armstrong numbers


#include<stdio.h>
#include<time.h>
int main()
{
int choice;
printf(" Type 1 to test Armstrong number.\n");
printf(" Type 2 to display Armstrong numbers in a given range.\n ");
scanf("%d",&choice);
if(choice==1)
{
int n, a, b, r, sum=0,d=0;
printf(" Enter the natural number:\t");
scanf("%d",&n);
a=n;
b=n;
while(n!=0)
{
n/=10;
d+=1; //(d= number of digits in given natural number)
}
while(a!=0)
{
r=a%10;
a/=10;
sum+=pow(r,d); //pow(r,d)=r^d
}
if (b==sum)
printf("\n %d is an Armstrong number.",b);
else
printf("\n %d is not an Armstrong number.",b);
}
else if(choice==2)
{
int i, p, q, n, x, y, sum=0, d=0, r;
printf("\n Enter the range to display Armstrong numbers:\t");
scanf("%d %d",&p,&q);
printf("\n The Armstrong numbers between %d and %d are:\t",p,q);
for(i=p+1;i<q;i++)
{
x=i;
y=i;
d=0;
while(x!=0)
{

16
Prepared by: Sultan Sidddiquie

x/=10;
d+=1; //(d= number of digits in given natural number)
}
sum=0;
while(y!=0)
{
r=y%10;
y=y/10;
sum=sum+pow(r,d);
}
if(i==sum)
printf(" %d" ,i);
}
}
else
{
printf("\n INVALID SELECTION");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

17
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 07:
Write a C program to identify whether a given number is Armstrong, and also display Armstrong numbers
between two given numbers.

Output:

18
Prepared by: Sultan Sidddiquie

Program 08:
Write a C program to identify whether a given number is strong, and also display strong numbers
between two given numbers.

Program:

//08. Strong numbers


#include<stdio.h>
#include<time.h>
int main()
{
int choice;
printf(" Type 1 to test Strong Number.\n");
printf(" Type 2 to display Strong Numbers in the given range.\n ");
scanf("%d",&choice);
if(choice==1)
{
int n, N, r, s=0, f=1, i;
printf(" Enter the number:\t");
scanf("%d",&n);
N=n;
while(n!=0)
{
r=n%10;
n=n/10;
f=1;
for(i=1;i<=r;i++)
f=f*i;
s=s+f;
}
if(s==N)
printf("\n %d is Strong number.",N);
else
printf("\n %d is not Strong number.",N);
}
else if(choice==2)
{
int p, q, n, f=1, s=0, r, i, j;
printf("\n Enter the range:\t");
scanf("%d %d",&p,&q);
printf("\n The strong numbers between %d and %d are:\t",p,q);
for(j=p+1;j<q;j++)
{
s=0;
n=j;
while(n!=0)
{
r=n%10;
n=n/10;
f=1;
for(i=1;i<=r;i++)

19
Prepared by: Sultan Sidddiquie

f=f*i;
s=s+f;
}
if(s==j)
printf(" %d ",j);
}
}
else
{
printf("\n Invalid Selection");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

20
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 08:
Write a C program to identify whether a given number is strong, and also display strong numbers between two
given numbers.

Output:

21
Prepared by: Sultan Sidddiquie

Program 09:
Write a C program to find HCF and LCM of two given numbers.

Program:

//09. HCF LCM (2 numbers)


#include<stdio.h>
#include<time.h>
int main()
{
int a, b, i, hcf, lcm, A, B, r;
printf(" Enter first number:\t");
scanf("%d",&a);
printf(" Enter second number:\t");
scanf("%d",&b);
A=a;
B=b;
for(i=1;i<=a||i<=b;i++)
{
if(a%i==0&&b%i==0)
hcf = i;
}
while(a!=0)
{
r=b%a;
b=a;
a=r;
}
printf(" HCF = %d", hcf);
printf("\n LCM = %d",(A*B)/b);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

22
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 09:
Write a C program to find HCF and LCM of two given numbers.

Output:

23
Prepared by: Sultan Sidddiquie

Program 10:
Write a C program to convert decimal number into fraction.

Program:

//10. Dec-Frac
#include<stdio.h>
#include<time.h>
int main()
{
int c=1000000, b;
float a;
printf(" Enter a decimal number:\t");
scanf("%f",&a);
b=(a-(int)(a))*c+(int)(a)*c;
//int a=integer in given number: if a=2.34, then (int)(a)=2
while(1)
{
if(b%10==0)
{
b=b/10;
c=c/10;
}
else
break;
}
printf("\n The fraction is %d/%d.",b,c);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

24
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 10:
Write a C program to convert decimal number into fraction.

Output:

25
Prepared by: Sultan Sidddiquie

Program 11:
Write a C program to convert lower-case letters in a sentence into upper-case using ASCII values.

Program:

//11. Lower-Upper Case


#include<stdio.h>
#include<time.h>
int main()
{
char l;
printf("The sentence in lower-case is:\t");
while(1)
{
l=getchar();
if (l=='\n')
break;
if (l>=97&&l<=122)
l=l-32;
putchar(l);
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

26
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 11:
Write a C program to convert lower-case letters in a sentence into upper-case using ASCII values.

Output:

27
Prepared by: Sultan Sidddiquie

Program 12:
Write a C program to convert binary into hexadecimal.

Program:

//12. Bin-Hex
#include<stdio.h>
#include<time.h>
int main()
{
int i,j,k;
long long rem,b;
char hex[20];
int binhex[16]={0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111};
printf("Enter the binary value:\t");
scanf("%lld",&b);
printf("The hexadecimal value of %lld is %c ",b,hex[k]);
for (i=0;b!=0;i++)
{
rem=b%10000;
for (j=0;j<16;j++)
{
if(binhex[j]==rem)
{
if (j<10)
{hex[i]=j+48;}
else
{hex[i]=j+55;}
break;
}
}
b=b/10000;
}
for (k=i-1; k>=0; k--)
{printf("%c",hex[k]);}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

28
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 12:
Write a C program to convert binary into hexadecimal.

Output:

29
Prepared by: Sultan Sidddiquie

Program 13:
Write a C program to convert hexadecimal into binary.

Program:

//13. Hex-Bin
#include<stdio.h>
#include<time.h>
int main()
{
char hex[16], bin[64];
int i;
printf("Enter the hexadecimal value:\t");
scanf("%s",&hex);
for (i=0;hex[i]!='\0';i++)
{ switch(hex[i])
{
case '0': strcat(bin, "0000"); break;
case '1': strcat(bin, "0001"); break;
case '2': strcat(bin, "0010"); break;
case '3': strcat(bin, "0011"); break;
case '4': strcat(bin, "0100"); break;
case '5': strcat(bin, "0101"); break;
case '6': strcat(bin, "0110"); break;
case '7': strcat(bin, "0111"); break;
case '8': strcat(bin, "1000"); break;
case '9': strcat(bin, "1001"); break;
case 'a': case 'A': strcat(bin, "1010"); break;
case 'b': case 'B': strcat(bin, "1011"); break;
case 'c': case 'C': strcat(bin, "1100"); break;
case 'd': case 'D': strcat(bin, "1101"); break;
case 'e': case 'E': strcat(bin, "1110"); break;
case 'f': case 'F': strcat(bin, "1111"); break;
default: printf("Please enter valid Hexadecimal Value.");
}
}
printf("The hexadecimal value %s equals the binary value %s",hex, bin);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

30
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 13:
Write a C program to convert hexadecimal into binary.

Output:

31
Prepared by: Sultan Sidddiquie

Program 14:
Write a C program to convert hexadecimal into denary number system.

Program:

//14. Hex-Den
#include<stdio.h>
#include<time.h>
#include<math.h>
int main()
{
char hex[16];
long long Den=0;
int i, den, len;
printf("Enter the hexadecimal value:\t");
scanf("%s",hex);
len=strlen(hex);
len--;
for(i=0; hex[i]!='\0'; i++)
{
if(hex[i]>='0' && hex[i]<='9')
{den = hex[i] - 48;}
else if(hex[i]>='a' && hex[i]<='f')
{den = hex[i] - 97 + 10;}
else if(hex[i]>='A' && hex[i]<='F')
{den = hex[i] - 65 + 10;}
Den = Den + den * pow(16, len-i);
}
printf("The denary value is %lld",Den);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

32
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 14:
Write a C program to convert hexadecimal into denary number system.

Output:

33
Prepared by: Sultan Sidddiquie

Program 15:
Write a C program to find the sum of two given matrices.

Program:

//15. Matrix Sum


#include<stdio.h>
#include<time.h>
int main()
{
int i,j,r,c;
int p[15][15],q[15][15];
printf("Enter the size of matrix %cr x c%c:\t",34,34);
scanf("%d %d",&r,&c);
printf("Enter the elements of matrix P:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("p[%d][%d]\t",i+1,j+1);
scanf(" %d",&p[i][j]);
}
}
printf("Enter the elements of matrix Q:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("q[%d][%d]\t",i+1,j+1);
scanf(" %d",&q[i][j]);
}
}
printf("The sum of given matrices is:\n P+Q= ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{printf("\t%d",p[i][j]+q[i][j]);}
printf(" \n");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

34
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 15:
Write a C program to find the sum of two given matrices.

Output:

35
Prepared by: Sultan Sidddiquie

Program 16:
Write a C program to find the transpose of a given matrix.

Program:

//16. Matrix transpose


#include<stdio.h>
#include<time.h>
int main()
{
int i,j,r,c;
int a[10][10],b[10][10];
printf("Enter the size of matrix %cr x c%c:\t",34,34);
scanf("%d %d",&r,&c);
printf("Enter the elements of matrix A:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("a[%d][%d]\t",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("The given matrix is\n A = ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{printf("\t%d",a[i][j]);}
printf("\n");
}
printf("The transpose of A is:\n A' = ");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{printf("\t%d",a[j][i]);}
printf("\n");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

36
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 16:
Write a C program to find the transpose of a given matrix.

Output;

37
Prepared by: Sultan Sidddiquie

Program 17:
Write a C program to find the product of two given matrices.

Program:

//17. Product of 2 Matrices


#include<stdio.h>
#include<time.h>
int main()
{
int i,j,k;
int m,n,p,q;
int a[15][15],b[15][15],c[15][15];
do
{
printf("For the multiplication of matrices, n=p (must be).\n");
printf("Enter the size of matrix A %cm x n%c:\t",34,34);
scanf("%d %d",&m,&n);
printf("Enter the size of matrix B %cp x q%c:\t",34,34);
scanf("%d %d",&p,&q);
} while(n!=p);
printf("Enter the elements of matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("a[%d][%d]\t",i+1,j+1);
scanf("\t%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("b[%d][%d]\t",i+1,j+1);
scanf("\t%d",&b[i][j]);
}
}
printf("The product of matrices A and B is:\n A x B = ");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{

38
Prepared by: Sultan Sidddiquie

for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n\t");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

39
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 17:
Write a C program to find the product of two given matrices.

Output:

40
Prepared by: Sultan Sidddiquie

Program 18:
Write a C program to display Floyd triangle pattern.

Program:

//18. Floyed triangle pattern


#include<stdio.h>
#include<time.h>
int main()
{
int n=1,i,r,j;
printf("Enter the number of rows: ");
scanf("%d",&r);
printf("The Floyd's Triangle Pattern for %d rows:\n\n",r);
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",n);
++n;
}
printf("\n\n");
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

41
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 18:
Write a C program to display Floyd triangle pattern.

Output:

42
Prepared by: Sultan Sidddiquie

Program 19:
Write a C program to display Pascal triangle pattern.

Program:

//19. Pascal triangle pattern.


#include<stdio.h>
#include<time.h>
int main()
{
int r,coeff=1,gap,i,j;
printf("Enter the number of rows: ");
scanf("%d",&r);
printf("The Pascal's Triangle Pattern for %d rows:\n\n",r);
{
for(i=0;i<r;i++)
{
for(gap=1;gap<=r-i;gap++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0||i==0)
coeff=1;
else
coeff=coeff*(i-j+1)/j;
printf("%6d",coeff);
}
printf("\n\n");
}
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

43
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 19:
Write a C program to display Pascal triangle pattern.

Output:

44
Prepared by: Sultan Sidddiquie

Program 20:
Write a C program to prepare a result taking marks in 4 papers, and give grade secured by a particular
student.

Program:

//20. Marksheet
#include<stdio.h>
#include<time.h>
int main()
{
float S1, S2, S3, S4, Total, Per;
printf(" Enter the marks in Subject 1:\t");
scanf("%f",&S1);
if(S1<=100&&S1>=0)
{
printf(" Enter the marks in Subject 2:\t");
scanf("%f",&S2);
{
if(S2<=100&&S2>=0)
{
printf(" Enter the marks in Subject 3:\t");
scanf("%f",&S3);
if(S3<=100&&S3>=0)
{
printf(" Enter the marks in Subject 4:\t");
scanf("%f",&S4);
}}}
}
Total=S1+S2+S3+S4;
//if full marks of each subject is 100
Per=Total/4;
if((S1<=100&&S2<=100&&S3<=100&&S4<=100)&&
(S1>=0&&S2>=0&&S3>=0&&S4>=0))
//if pass marks is 40 in each subject
{
if(S1<40||S2<40||S3<40||S4<40)
printf(" Result: Failed");
else
{
printf("\n Result: Passed \n");
if(Per>=90)
{printf(" Grade: A+");}
else if(Per>=80)
{printf(" Grade: A");}
else if(Per>=70)
{printf(" Grade: B+");}
else if(Per>=60)
{printf(" Grade: B");}
else if(Per>=50)
{printf(" Grade: C+");}

45
Prepared by: Sultan Sidddiquie

else
{printf(" Grade: C");}
printf("\n Total Marks: %.2f",Total);
printf("\n Percentage: %.2f",Per);
}
}
else
{printf("\n Please enter correct values.");}
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

46
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 20:
Write a C program to prepare a result taking marks in 4 papers, and give grade secured by a particular student.

Output:

47
Prepared by: Sultan Sidddiquie

Program 21:
Write a C program to find permutation and combination of given things taken some at a time.

Program:

//21. Permutation and Combination


#include<stdio.h>
#include<time.h>
int main()
{
int n, r, d, P, C;
int i, j, k;
int N=1, R=1, D=1;
printf(" Enter the number of items (n): ");
scanf("%d",&n);
printf(" Enter the number of items to be selected (r): ");
scanf("%d",&r);
if(n>=1&&r>=0&&n>=r)
{
//N=n!
for(i=1;i<=n;i++)
{N=N*i;}
//R=r!
for(j=1;j<=r;j++)
{R=R*j;}
//D=d!=(n-r)!
d=n-r;
for(k=1;k<=d;k++)
{D=D*k;}
//Permutation
P=N/D;
printf("\n P(%d,%d) = %d",n,r,P);
//Combination
C=N/(D*R);
printf("\n C(%d,%d) = %d",n,r,C);
}
else
{printf(" Error");}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

48
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 21:
Write a C program to find permutation and combination of given things taken some at a time.

Output:

49
Prepared by: Sultan Sidddiquie

Program 22:
Write a C program to generate Fibonacci sequence of given terms, and also find the sum (using
recursive function).

Program:

//22. Fibonacci sequence and sum


#include<stdio.h>
#include<time.h>
int fibo(int n);
int main()
{
int n,i,s=0;
printf(" Enter the number of terms (n):\t");
scanf("%d",&n);
printf(" The Fibonacci sequence of %d terms is:\n",n);
for(i=0;i<n;i++)
{
printf(" %d\t",fibo(i));
s =s+fibo(i);
}
printf("\n The sum of first %d terms is %d.",n,s);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}
int fibo(int n)
{
if(n==0||n==1)
return n;
return (fibo(n-1)+fibo(n-2));
}

50
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 22:
Write a C program to generate Fibonacci sequence of given terms, and also find the sum (using recursive
function).

Output:

51
Prepared by: Sultan Sidddiquie

Program 23:
Write a C program to find HCF of given numbers (more than two).

Program:

//23. HCF (more than two)


#include<stdio.h>
#include<time.h>
int main()
{
int n,i,j=1,hcf;
printf("Enter the amount of numbers:\t");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter number %d = ",i+1);
scanf("%d",&arr[i]);
}
hcf=arr[0];
while(j<=n)
{
if(arr[j]%hcf==0)
{j++;}
else
{hcf=arr[j]%hcf;
i++;}
}
printf("\nHCF = %d ",hcf);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

52
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 23:
Write a C program to find HCF of given numbers (more than two).

Output:

53
Prepared by: Sultan Sidddiquie

Program 24:
Write a C program that uses calloc function to find sum of two given numbers.

Program:

//24. calloc funtion, sum of two numbers.


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a, b, sum;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
// Allocate memory for the sum using calloc
int*result=(int*)calloc(1,sizeof(int));
if(result==NULL)
{
printf("Memory allocation failed.\nExiting program.\n");
return 1;
}
// Calculate the sum
*result=a+b;
printf("The sum is:\n %d + %d = %d\n",a,b,*result);
// Free the allocated memory
free(result);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

54
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 24:
Write a C program that uses calloc function to find sum of two given numbers.

Output:

55
Prepared by: Sultan Sidddiquie

Program 25:
Write a C program that uses malloc function to allocate memory, also readjust the allocation using
realloc function.

Program:

//25. malloc function, realloc function


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
int *ptr;
int n, i;
printf(" Enter the number of elements: ");
scanf("%d",&n);
printf("\n Entered number of elements: %d\n",n);
// Allocate memory for 'n' integers
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("\n Memory allocation failed.");
exit(0);
}
else
{
printf("\n Memory successfully allocated using malloc.\n");
// Initialize the array with values
for(i=0;i<n;++i)
{
ptr[i]=i+1;
}
// Display the elements of the array
printf("\n The elements of the array are:\n\n");
for(i=0;i<n;++i)
{
printf("%d ",ptr[i]);
}
}
// Reallocate memory to increase the size
int newSize=2*n;
ptr=(int*)realloc(ptr,newSize*sizeof(int));
if(ptr==NULL)
{
printf("\n Memory reallocation failed.\n");
exit(0);
}
else
{
printf("\n\n Memory successfully reallocated using realloc.\n");

56
Prepared by: Sultan Sidddiquie

// Initialize the new elements


for(i=n;i<newSize;++i)
{
ptr[i]=i+1;
}
// Display the updated elements of the array
printf("\n Updated elements of the array are:\n\n");
for(i=0;i<newSize;++i)
{
printf(" %d ",ptr[i]);
}
}
// Free the allocated memory
free(ptr);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n\n Executed on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

57
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 25:
Write a C program that uses malloc function to allocate memory, also readjust the allocation using realloc
function.

Output:

58
Prepared by: Sultan Sidddiquie

Program 26:
Write a C program that creates a data file containing structure of n records.

Program:

//26. structure of n records


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct Members
{char name[35]; int age; char address[20];};
int main()
{
int i,n;
FILE *file;
printf("How many record do you want to enter? ");
scanf("%d",&n);
struct Members rec[n];
printf("The details of the members of the organization:\n");
for (i=1;i<=n;i++)
{char name[35];
printf("Enter name (first_middle_last): ");
scanf("%s",rec[i].name);
printf("Enter age: ");
scanf("%d",&rec[i].age);
printf("Enter address: ");
scanf("%s",&rec[i].address);}
file= fopen("Members.txt","w");
if(file==NULL)
{printf("File not opened");
return 1;}
for (i=1;i<=n;i++)
{fprintf(file,"Member %d",i);
fprintf(file,"\nName: %s",rec[i].name);
fprintf(file,"\nAge: %d",rec[i].age);
fprintf(file,"\nAddress: %s\n\n",rec[i].address);}
fclose(file);
printf("File created.\nSee your data in %cMembers.txt%c",34,34);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

59
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 26:
Write a C program that creates a data file containing structure of n records.

Output:

60
Prepared by: Sultan Sidddiquie

Program 27:
Write a C program that creates a data file containing records of product with field name, product code,
product name, quantity, and price. Also, append the file with more records of products.

Program 27.1:

//27.1 Create File


#include <stdio.h>
#include <time.h>
int main()
{
FILE *fp;
int code,quantity,i;
int n;
float rate,price;
char productName[10],filename[10];
printf("Input file name: ");
scanf("%s",filename);
fp=fopen(filename,"w");
if(fp==NULL)
{
printf("Error opening file for writing.\n");
return 1;
}
printf("How many entries you need to enter?\n");
scanf("%d",&n);
printf("\nInput inventory data:\n\n");
printf("Product Code\t Product Name\t Quantity\t Rate\n");
for (i=1;i<=n;i++)
{
scanf("%d %s %d %f",&code,productName,&quantity,&rate);
price=rate*quantity;
fprintf(fp,"%d\t %s\t %d\t %4.2f\t %6.2f\n",
code,productName,quantity,rate,price);
}
fclose(fp);
printf("\nContents of %s\n\n",filename);
printf("Product Code \t Product Name \t Quantity \t Rate\t\t Price\n");
fp = fopen(filename,"r");
if (fp==NULL)
{
printf("Error opening file for reading.\n");
return 1;
}
for (i=1;i<=n;i++)
{
fscanf(fp, "%d\t%s\t%d\t%f\t%f",
&code,productName,&quantity,&rate,&price);
printf("%d\t\t %s\t\t %d\t\t %4.2f\t\t %6.2f\n",
code,productName,quantity,rate,price);
}

61
Prepared by: Sultan Sidddiquie

fclose(fp);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

62
Prepared by: Sultan Sidddiquie

Program 27.2:

//27.2 Append
#include <stdio.h>
#include <time.h>
int main()
{
FILE *fp;
int code,quantity,i;
int n;
float rate,price;
char productName[10],filename[10];
printf("Input file name: ");
scanf("%s",filename);
fp = fopen(filename,"a+");
if (fp==NULL)
{
printf("Error opening file for appending.\n");
return 1;
}
printf("How many entries you need to enter?\n");
scanf("%d",&n);
printf("\nInput inventory data:\n\n");
printf("Product Code\t Product Name\t Quantity\t Rate\n");
for (i=1;i<=n;i++)
{
scanf("%d %s %d %f",&code,productName,&quantity,&rate);
price=rate*quantity;
fprintf(fp, "%d\t %s\t %d\t %4.2f\t %6.2f\n",
code,productName,quantity,rate,price);
}
fclose(fp);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

63
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 27:
Write a C program that creates a data file containing records of product with field name, product code,
product name, quantity, and price. Also, append the file with more records of products.

Output:

64
Prepared by: Sultan Sidddiquie

Program 28:
Write a C program to solve by using bisection method. (Take error of
tolerance from user.)

Program:

//28. Bisection method


#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<time.h>
float f(float x)
{return (x*x*x-9*x+1);}
void bisect (float *x, float a, float b, int *itr)
{*x=a+(b-a)/2;
++(*itr);
printf("Iteration no. %3d X= %7.5f\n",*itr,*x);}
int main()
{
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
tm.tm_hour,tm.tm_min,tm.tm_sec);
int itr=0, maxitr;
float x, a, b, Tol, x1;
printf("Enter the interval:\t");
scanf("%f %f",&a,&b);
printf("Enter the tolerance value:\t");
scanf("%f",&Tol);
printf("Enter maximum number of iterations:\t");
scanf("%d",&maxitr);
bisect (&x,a,b,&itr);
do{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect (&x1,a,b,&itr);
if(fabs(x1-x)<Tol)
{printf("Iterations = %d\nRoot = %5.4f\n",itr,x1);
return 0;}
x=x1;}while(itr<maxitr);
printf("\nSolution does not converge.\nIterations not sufficient");
return 1;
}

65
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 28:
Write a C program to solve by using bisection method. (Take error of tolerance
from user.)

Output:

66
Prepared by: Sultan Sidddiquie

Program 29:
Write a C program to solve by using Newton-Raphson method. (Take error of
tolerance from user.)

Program:

//29. Newton-Raphson
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define F(x) (sin(x)-x*x*x-1)
#define f(x) (cos(x)-3*x*x)
main()
{
float x0,x1,F0,F1,f0,Tol; int step=1, maxitr;
printf("\nEnter initial guess: ");
scanf("%f", &x0);
printf("Enter tolerable error: ");
scanf("%f", &Tol);
printf("Enter maximum iteration: ");
scanf("%d", &maxitr);
printf("\nStep\t\tx0\t\tF(x0)\t\tx1\t\tF(x1)\n");
do
{
f0=f(x0); F0=F(x0);
if (f0==0.0)
{printf("Mathematical Error."); exit(0);}
x1 = x0-F0/f0;
printf("%d\t\t%f\t%f\t%f\t%f\n", step, x0, F0, x1, F1);
x0 = x1;
step++;
if(step>maxitr)
{printf("Not Convergent."); exit(0);}
F1=F(x1);
} while(fabs(F1)>Tol);
printf("\nRoot is: %f",x1);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
}

67
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 29:
Write a C program to solve by using Newton-Raphson method. (Take error of
tolerance from user.)

Output:

68
Prepared by: Sultan Sidddiquie

Program 30:
Write a C program to evaluate ∫ √ by using trapezoidal rule. (Take number of sub-
intervals from user.)

Program:

//30. Trapezoidal
#include<stdio.h>
#include<stdlib.h>
#define Pi 3.14159
#include<time.h>
float y(float x)
{
return sqrt(sin(x));
}
main()
{
float a=0,b=Pi/2,h /*h=stepSize*/,s;
int i,n;
printf("The lower limit is %3.4f\n",a);
printf("The upper limit is %3.4f\n",b);
printf("Enter the number of sub-intervals:\n");
scanf("%d",&n);
h=(b-a)/n;
s=y(a)+y(b);
for(i=1;i<=n-1;i++)
s+=2*y(a+i*h);
printf ("Value of integral is %6.4f\n",(h/2)*s);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\n Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
}

69
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 30:
Write a C program to evaluate ∫ √ by using trapezoidal rule. (Take number of sub-
intervals from user.)

Output:

70
Prepared by: Sultan Sidddiquie

Program 31:
Write a C program to evaluate ∫ √ by using Simpson’s 1/3 rule. (Take number of
sub-intervals from user.)

Program:

//31. Simpson's 1/3


#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define f(x) sqrt(1+(x*x))
int main()
{
float l=0, u=2, integ = 0.0, h, k;
int n,i;
//l=lower limit, u=upper limit, h=(u-l)/n=stepSize, n=number of sub-intervals
printf("The lower limit of integration is %.2f\n",l);
printf("The upper limit of integration is %.2f\n",u);
printf("Enter number of sub intervals: ");
scanf("%d",&n);
h=(u-l)/n;
/* Finding Integration Value */
integ=f(u)+f(l);
for (i=1;i<=n-1;i++)
{
k=l+i*h;
if (i%2==0)
{integ+=2*f(k);}
else
{integ+=4*f(k);}
}
integ*=h/3;
printf("\nRequired value of integration is: %.4f",integ);
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("\nExecuted on: %d/%02d/%02d %02d:%02d:%02d",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

71
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 31:
Write a C program to evaluate ∫ √ by using Simpson’s 1/3 rule. (Take number of sub-
intervals from user.)

Output:

72
Prepared by: Sultan Sidddiquie

Program 32:
Write a C program to solve by Gauss
elimination method.

Program:

//32. Gauss Elimination


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int main()
{
float a[5][5], x[5], ratio;
int i, j, k, n;
printf("Enter the number of unknowns:\t");
scanf("%d",&n);
printf("\n");
/* Reading Augmented Matrix */
printf("Enter the elements of Augmented Matrix\n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
}
printf("\n");
}
/* Applying Gauss Elimination */
for (i=1;i<=n-1;i++)
{
if (a[i][i]==0.0)
{
printf("Mathematical Error!");
exit(0);
}
for (j=i+1;j<=n;j++)
{
ratio=a[j][i]/a[i][i];
for (k=1;k<=n+1;k++)
{
a[j][k]=a[j][k]-ratio*a[i][k];
}
}
}
/* Obtaining Solution by Back Substitution */
x[n]=a[n][n+1]/a[n][n];
for (i=n-1;i>=1;i--)
{
x[i]=a[i][n+1];

73
Prepared by: Sultan Sidddiquie

for (j=i+1;j<=n;j++)
{
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
}
/* Displaying Solution */
printf("\nSolution:\n");
for (i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i,x[i]);
}
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
return 0;
}

74
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 32:
Write a C program to solve by Gauss
elimination method.

Output:

75
Prepared by: Sultan Sidddiquie

Program 33:
Write a C program to solve by Gauss-
Seidel method.

Program:

//33. Gauss Siedel


#include<stdio.h>
#include<math.h>
#include<time.h>
main()
{
time_t t=time(NULL);
struct tm tm=*localtime(&t);
printf("Executed on: %d/%02d/%02d %02d:%02d:%02d\n",
tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,
tm.tm_hour,tm.tm_min,tm.tm_sec);
printf("Enter number of unknown values:\t");
int N;
scanf("%d",&N);
float a[N][N+1],x[N],aerr,maxerr,T,s,err;
int i,j,itr,maxitr;
// first initializing the array x[i]
for(i=0;i<N;i++)
x[i]=0;
printf("Enter the elements of the augmented matrix row-wise:\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
printf("Enter the allowed error:\n");
scanf("%f",&aerr);
printf("Enter the maximum iterations:\n");
scanf("%d",&maxitr);
printf("Iteration\t x[1]\t x[2]\tx[3]\n");
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<N;i++)
{
s=0;
for(j=0;j<N;j++)
if(j!=i)
s+=a[i][j]*x[j];
T=(a[i][N]-s)/a[i][i];
err=fabs(x[i]-T);
if(err>maxerr)
maxerr=err;
x[i]=T;
}
printf("%3d\t\t",itr);
for(i=0;i<N;i++)

76
Prepared by: Sultan Sidddiquie

printf("%7.2f",x[i]);
printf("\n");
if (maxerr<aerr)
{
printf("Converges in %d iterations\n",itr);
for (i=0;i<N;i++)
printf("x[%d] = %7.2f\n",i+1,x[i]);
return 0;
}
}
printf("Solution does not converge\nIterations not sufficient\n");
return 1;
}

77
Prepared by: Sultan Sidddiquie

>>Go to Index of Programs


Program 33:
Write a C program to solve by Gauss-
Seidel method.

Output:

78
The programs in this document may contain some inaccuracies.
So the readers are requested to do their own research, as much as
possible.

You might also like