Computer Programming [MAT 301]
Computer Programming [MAT 301]
Submitted to
The Department of Mathematics
Tri-Chandra Multiple Campus
Ghantaghar, Kathmandu
Submitted by
Sultan Siddiquie
PRACTICAL FILE OF COMPUTER PROGRAMMING
Submitted to:
The Department of Mathematics
Tri-Chandra Multiple Campus
Ghantaghar, Kathmandu
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
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 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 01:
Write a C program to determine whether a given year is a leap year.
Program:
1
Prepared by: Sultan Sidddiquie
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:
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
Output:
5
Prepared by: Sultan Sidddiquie
Program 03:
Write a C program to convert temperature in Centigrade to Fahrenheit and vice versa.
Program:
6
Prepared by: Sultan Sidddiquie
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
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:
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
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:
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
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:
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
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:
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
Output:
21
Prepared by: Sultan Sidddiquie
Program 09:
Write a C program to find HCF and LCM of two given numbers.
Program:
22
Prepared by: Sultan Sidddiquie
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
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:
26
Prepared by: Sultan Sidddiquie
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
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
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
Output:
33
Prepared by: Sultan Sidddiquie
Program 15:
Write a C program to find the sum of two given matrices.
Program:
34
Prepared by: Sultan Sidddiquie
Output:
35
Prepared by: Sultan Sidddiquie
Program 16:
Write a C program to find the transpose of a given matrix.
Program:
36
Prepared by: Sultan Sidddiquie
Output;
37
Prepared by: Sultan Sidddiquie
Program 17:
Write a C program to find the product of two given matrices.
Program:
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
Output:
40
Prepared by: Sultan Sidddiquie
Program 18:
Write a C program to display Floyd triangle pattern.
Program:
41
Prepared by: Sultan Sidddiquie
Output:
42
Prepared by: Sultan Sidddiquie
Program 19:
Write a C program to display Pascal triangle pattern.
Program:
43
Prepared by: Sultan Sidddiquie
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
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:
48
Prepared by: Sultan Sidddiquie
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:
50
Prepared by: Sultan Sidddiquie
Output:
51
Prepared by: Sultan Sidddiquie
Program 23:
Write a C program to find HCF of given numbers (more than two).
Program:
52
Prepared by: Sultan Sidddiquie
Output:
53
Prepared by: Sultan Sidddiquie
Program 24:
Write a C program that uses calloc function to find sum of two given numbers.
Program:
54
Prepared by: Sultan Sidddiquie
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:
56
Prepared by: Sultan Sidddiquie
57
Prepared by: Sultan Sidddiquie
Output:
58
Prepared by: Sultan Sidddiquie
Program 26:
Write a C program that creates a data file containing structure of n records.
Program:
59
Prepared by: Sultan Sidddiquie
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:
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
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:
65
Prepared by: Sultan Sidddiquie
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
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
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:
71
Prepared by: Sultan Sidddiquie
Output:
72
Prepared by: Sultan Sidddiquie
Program 32:
Write a C program to solve by Gauss
elimination method.
Program:
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
Output:
75
Prepared by: Sultan Sidddiquie
Program 33:
Write a C program to solve by Gauss-
Seidel method.
Program:
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
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.