Vous êtes sur la page 1sur 28

COMPUTER PROGRAMMING IN C (MODULE A)

** ASSIGNMENT **

By

SUBHAMOY BISWAS

ROLL 001810801123
CLASS BEE-I (UG)

SECTION E1-3 (B)

YEAR 1st

SEMESTER 2nd

DATE OF SUBMISSION 22 APRIL, 2019

SUBJECT COMPUTER PROGRAMMING (PRACTICAL)

DEPARTMENT ELECTRICAL ENGINEERING

1
CONTENTS

SERIAL NUMBER TOPIC PAGE NUMBER


1 Write a program to compute the addition, 4–5
subtraction, multiplication, division between two
numbers and find their remainder.
2 Write a program to compute the perimeter and area 5
of a circle of given radius.
3 Write a program to find the maximum and the 5-6
minimum numbers among three numbers.
4 Write a program to print the average of n numbers. 6-7
5 Write a program to print the roots of a quadratic 7-9
equation from the coefficients.
6 Write a program to print all prime numbers between 9 - 10
two given numbers.
7 Write a program to accept a number from the user 10 - 11
and calculate the sum of all odd values up to that
number.
8 Write a program to print the following pattern. 11 - 12

*
* *
* * *
* * * *

9 Write a program to print the following pattern. 12 – 13


(For number of columns= 4 the output will be this)

*
* *
* * *
* * * *
* * *
* *
*

2
10 Write a program to shift the inputted data by two 13 – 14
bits to the left.
11 Write a program to print the Fibonacci series up to 14 – 15
100.
12 Write a program to accept some integers from the 15 – 16
user and find the highest value and its input position.
13 Write a program to calculate the value of sin(x) using 16 – 17
recursion for a given x.
14 Write a program to check whether a string is 17 – 18
‘Palindrome’ or not.
15 Write a program to find the maximum number in an 19 – 20
array.
16 Write a program to swap two variables without the 20 – 21
help of any third variable.
17 Write a program to multiply two 3X3 matrices. 21 – 24
18 Write a program to exhibit star to delta conversion 24 – 25
and vice versa.
19 Write a program to print the integers in order from 25 - 28
lowest to highest and calculate the sum of the
integers from a given pair of numbers. The pairs
would be specified by integer values. If there are
multiple integer values, ask the user which index is to
be considered.

3
1. Write a program to compute the addition, subtraction, multiplication,
division between two numbers and find their remainder.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int a, b;
printf("\nEnter two numbers: ");
scanf("%d %d", &a, &b);
printf("\nTheir sum= %d\n", (a+b));
printf("\nTheir product= %d\n", (a*b));
printf("\n%d on subtracted from %d gives %d", a, b, (b-a));
printf("\n%d on subtracted from %d gives %d\n", b, a, (a-b));
printf("\nWhen %d is divided by %d then\n", a, b);
printf("Quotient= %d\n", (a/b));
printf("Remainder= %d\n", (a%b));
printf("\nWhen %d is divided by %d then\n", b, a);
printf("Quotient= %d\n", (b/a));
printf("Remainder= %d", (b%a));
getch();
return 0;
}

Output:

Enter two numbers: 45 67

Their sum= 112

Their product= 3015

45 on subtracted from 67 gives 22


67 on subtracted from 45 gives -22

When 45 is divided by 67 then


Quotient= 0
Remainder= 45

4
When 67 is divided by 45 then
Quotient= 1
Remainder= 22

2. Write a program to compute the perimeter and area of a circle of given


radius.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float r;
printf("\nEnter the radius of the circle: ");
scanf("%f", &r);
printf("\nCircumference (or perimeter)= %f", (2*3.14159265358*r));
printf("\nArea= %f", (3.14159265358*r*r));
getch();
return 0;
}

Output:

Enter the radius of the circle: 3.56

Circumference (or perimeter)= 22.368139


Area= 39.815287

3. Write a program to find the maximum and the minimum numbers among
three numbers.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float x, y, z;

5
printf("\nEnter three numbers: ");
scanf("%f %f %f", &x, &y, &z);
printf("\nMaximum= %f.", (x>y)?((x>z)?x:z):((y>z)?y:z));
printf("\nMinimum= %f.", (x<y)?((x<z)?x:z):((y<z)?y:z));
getch();
return 0;
}

Output:

Enter three numbers: -98 97 -97.5

Maximum= 97.000000.
Minimum= -98.000000.

4. Write a program to find the average of n numbers.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float r, s=0, avg;
int i, n;
printf("\nEnter numbers of values whose average is to found out: ");
scanf("%d", &n);
printf("\nEnter %d values...............\n", n);
for(i=1; i<=n; i++)
{
scanf("%f", &r);
s=s+r;
}
printf("\nAverage of the given values is %f", (s/n));
getch();
return 0;
}

6
Output:

Enter numbers of values whose average is to found out: 8

Enter 8 values...............
23.4
45.678
42.97
987.5432
12.78
78.64
9.9
1264.765

Average of the given values is 308.209534

5. Write a program to print the roots of a quadratic equation from the


coefficients.

Ans: #include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a, b, c, D, x, y;
printf("\nAny quadratic equation is of the form: a*x^2 + b*x + c = 0\n");
printf("\nSo, enter a, b, c:\n");
scanf("%f %f %f", &a, &b, &c);
D=(b*b)-(4*a*c);
if(D>=0)
{
if(a!=0)
{
x=(-b+sqrt(D))/(2*a);
y=(-b-sqrt(D))/(2*a);

7
printf("\nThe roots are: %f %f", x, y);
}
else
{
if(b==0)
{
printf("\nNo roots.\n");
}
else
{
printf("\nThe only root is %f", (-c/b));
}
}
}
else
{
if(a!=0)
{
x=(-b)/(2*a);
y=sqrt(-D)/(2*a);
printf("\nThe roots are: %f + i(%f), %f - i(%f)", x, y, x, y);
}
else
{
if(b==0)
{
printf("\nNo roots.\n");
}
else
{
printf("\nThe only root is %f", (-c/b));
}
}
}
getch();
return 0;
}

8
Output:

Any quadratic equation is of the form: a*x^2 + b*x + c = 0

So, enter a, b, c:
6 7 8

The roots are: -0.583333 + i(0.996522), -0.583333 - i(0.996522)

6. Write a program to print all prime numbers between two given numbers.

Ans: #include<stdio.h>
#include<conio.h>
int prime(int n)
{
int i, flag=0;
for(i=2; i<n; i++)
{
if(n%i==0)
flag=1;
}
return flag;
}
int main()
{
int a, b, i;
printf("Enter the two limits: \n");
scanf("%d %d", &a, &b);
if(a<0 || b<0)
{
printf("\nOnly positive limits are allowed.");
}
else
{
printf("\nThe values are..........\n");
if(a>b)

9
{
b=a+b;
a=b-a;
b=b-a;
}
for(i=a+1; i<b; i++)
{
if(prime(i)==0)
printf("%d ", i);
}
}
getch();
return 0;
}

Output:
Enter the two limits:
22 80

The values are..........


23 29 31 37 41 43 47 53 59 61 67 71 73 79

7. Write a program to accept a number from the user and calculate the sum of
all odd values up to that number.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int i, n, s=0;
printf("Enter the limit: \n");
scanf("%d", &n);
if(n<0)
printf("\nOnly positive limit is allowed.");
else

10
{
for(i=1; i<=n; i=i+2)
{
s=s+i;
}
printf("The 1 + 3 + 5 + ............. (upto %d) = %d", n, s);
}
getch();
return 0;
}

Output:
Enter the limit:
64
The 1 + 3 + 5 + ............. (upto 64) = 1024

8. Write a program to print the following pattern.

*
* *
* * *
* * * *

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
for(i=1; i<=4; i++)
{
for(j=3; j>=i; j--)
{
printf(" ");

11
}
for(j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
getch();
return 0;
}

Output:
*
* *
* * *
* * * *

9. Write a program to print the following pattern.

(For number of columns= 4 the output will be this)

*
* *
* * *
* * * *
* * *
* *
*

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n;
printf("\nEnter number of columns: ");
scanf("%d", &n);
for(i=1; i<2*n; i++)

12
{
for(j=1; j<=n; j++)
{
if((j<=n-i) || (i>n && j<=i-n))
printf(" ");
else
printf("* ");
}
printf("\n");
}
getch();
return 0;
}

Output:

Enter number of columns: 4


*
* *
* * *
* * * *
* * *
* *
*

10. Write a program to shift the inputted data by two bits to the left.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter the number to be shifted by two bits to the left: ");
scanf("%d", &n);
printf("\n%d after being shifted two bits to the left is now %d.", n, (n<<2));
getch();
return 0;
}
13
Output:
Enter the number to be shifted by two bits to the left: 67

67 after being shifted two bits to the left is now 268.

11. Write a program to print the Fibonacci series up to 100.

Ans: #include<stdio.h>
#include<conio.h>
int fibo(int n);
int main()
{
int k=1, j=0;
printf("The Fibonacci numbers till 100 are................\n");
while(k!=0)
{
if(fibo(j)<100)
{
printf("%d ", fibo(j));
j++;
}
else
k=0;
}
getch();
return 0;
}
int fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
return (fibo(n-1)+fibo(n-2));

14
}

Output:
The Fibonacci numbers till 100 are................
0 1 1 2 3 5 8 13 21 34 55 89

12. Write a program to accept some integers from the user and find the highest
value and its input position.

Ans: #include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n, k, i, max, pos;
printf("\nEnter number of terms: ");
scanf("%d", &n);
if(n<0)
n=abs(n);
printf("\nEnter %d terms...............\n", n);
for(i=1; i<=n; i++)
{
scanf("%d", &k);
if(i==1)
{
max=k;
pos=1;
}
else
{
if(max<k)
{
max=k;
pos=i;
}

15
}
}
printf("\nThe highest value in the given set is %d.", max);
printf("\nIt's position in the set is %d.", pos);
getch();
return 0;
}

Output:

Enter number of terms: 7

Enter 7 terms...............
234
567
453
34
7890
1234
897

The highest value in the given set is 7890.


It's position in the set is 5.

13. Write a program to calculate the value of sin(x) using recursion for a given x.

Ans: #include<stdio.h>
#include<conio.h>
float sine(float x, int n);
int main()
{
float x;
int n;
printf("\nEnter angle in degrees: ");
scanf("%f", &x);
printf("\nEnter limit of sin(x) series: ");
scanf("%d", &n);
x=(x*3.142)/180;
16
printf("Sine value: %f", sine(x, 2*n-1));
getch();
return 0;
}
float sine(float x, int n)
{
if(n==1)
return x;
int a=1, i, p=1;
float y=1;

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


a=a*i;

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


y=y*x;

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


p=p*(-1);

return(sine(x, n-2) + ((p*y)/a));


}

Output:

Enter angle in degrees: 60

Enter limit of sin(x) series: 9


Sine value: 0.866093

14. Write a program to check whether a string is ‘Palindrome’ or not.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{

17
int i, j, l, flag=0;
char a[20], b[20];
printf("\nEnter the string to be checked: \n");
gets(a);
for(l=0; a[l]!=0; l++);//finding the length
for(i=0,j=l-1; i<l,j>=0; i++,j--)
{
b[i]=a[j];
}
for(i=0; i<l; i++)
{
if(a[i]!=b[i])
{
flag=1;
}
}
if(flag==0)
printf("\nThe given string is Palindrome.");
else
printf("\nThis string is not Palindrome.");
getch();
return 0;
}

Output:
(i)
Enter the string to be checked:
ABCDCBA

The given string is Palindrome.

(ii)
Enter the string to be checked:
ALMOST

This string is not Palindrome.

18
15. Write a program to find the maximum number in an array.

Ans: #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *a, n, i, max;
printf("\nEnter number of elements in the array: \n");
scanf("%d", &n);
printf("\nEnter %d values..............\n", n);
a=(int *)malloc(n*sizeof(int));
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
max=a[0];
for(i=1; i<n; i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("\nMaximum number in the array is: %d", max);
getch();
return 0;
}

Output:

Enter number of elements in the array:


8

Enter 8 values..............
45
8901
2341

19
567
32
5672
8902
8910

Maximum number in the array is: 8910

16. Write a program to swap two variables without the help of any third
variable.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float a, b;
printf("\nEnter two numbers: \n");
printf("\nFirst number: ");
scanf("%f", &a);
printf("\nSecond number: ");
scanf("%f", &b);
b=a+b;
a=b-a;
b=b-a;
printf("\nNow..........................\n");
printf("\nFirst number: %f", a);
printf("\nSecond number: %f", b);
getch();
return 0;
}

Output:

Enter two numbers:

First number: 2.345

20
Second number: 2.346

Now..........................

First number: 2.346000


Second number: 2.345000

17. Write a program to multiply two 3X3 matrices.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3], b[3][3], i, j, k, s=0;
printf("\nEnter the elements of the first matrix A :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\nA[%d][%d]= ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the elements of the second matrix B :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\nB[%d][%d]= ", i, j);
scanf("%d", &b[i][j]);
}
}
printf("\nMatrix A therefore is: \n");
for(i=0; i<3; i++)
{

21
for(j=0; j<3; j++)
{
printf("%5d", a[i][j]);
}
printf("\n");
}
printf("\nMatrix B therefore is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%5d", b[i][j]);
}
printf("\n");
}
printf("\nSo, the product matrix A*B is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
for(k=0; k<3; k++)
{
s+=(a[i][k]*b[k][j]);
}
printf("%5d", s);
s=0;
}
printf("\n");
}
getch();
return 0;
}

Output:

Enter the elements of the first matrix A :


A[0][0]= 2

A[0][1]= 3

22
A[0][2]= 6

A[1][0]= 7

A[1][1]= -9

A[1][2]= -5

A[2][0]= 1

A[2][1]= 6

A[2][2]= -4

Enter the elements of the second matrix B :


B[0][0]= -9

B[0][1]= 4

B[0][2]= 1

B[1][0]= -3

B[1][1]= -5

B[1][2]= -6

B[2][0]= 8

B[2][1]= 3

B[2][2]= 5

Matrix A therefore is:


2 3 6
7 -9 -5
1 6 -4

Matrix B therefore is:


-9 4 1
-3 -5 -6
8 3 5

23
So, the product matrix A*B is:
21 11 14
-76 58 36
-59 -38 -55

18. Write a program to exhibit star to delta conversion and vice versa.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float r1, r2, r3, R1, R2, R3;
printf("\n*** STAR TO DELTA CONVERSION ***\n");
printf("\nEnter the phase resistances of the star-connected system: ");
scanf("%f %f %f", &r1, &r2, &r3);
if(r1<=0 || r2<=0 || r3<=0)
printf("\nResistances cannot be negative or zero.");
else
printf("\nPhase resistances of the equivalent delta connected system are:
");
printf("%.2f\t", r1 + r2 + ((r1*r2)/r3));
printf("%.2f\t", r2 + r3 + ((r2*r3)/r1));
printf("%.2f\n", r3 + r1 + ((r1*r3)/r2));
printf("\n*** DELTA TO STAR CONVERSION ***\n");
printf("\nEnter the phase resistances of the delta-connected system: ");
scanf("%f %f %f", &R1, &R2, &R3);
if(R1<=0 || R2<=0 || R3<=0)
printf("\nResistances cannot be negative or zero.");
else
printf("\nPhase resistances of the equivalent star connected system are:
");
printf("%.2f\t", (R1*R3)/(R1 + R2 + R3));
printf("%.2f\t", (R1*R2)/(R1 + R2 + R3));
printf("%.2f\n", (R2*R3)/(R1 + R2 + R3));
getch();
24
return 0;
}

Output:

*** STAR TO DELTA CONVERSION ***

Enter the phase resistances of the star-connected system: 43.4 34.6


75.8

Phase resistances of the equivalent delta connected system are: 97.81


170.83 214.28

*** DELTA TO STAR CONVERSION ***

Enter the phase resistances of the delta-connected system: 56.9 13.7


98.2

Phase resistances of the equivalent star connected system are: 33.10 4.62
7.97

19. Write a program to print the integers in order from lowest to highest and
calculate the sum of the integers from a given pair of numbers. The pairs would
be specified by integer values. If there are multiple integer values, ask the user
which index is to be considered.

Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int a[50], n, i, j, p, q, k1, k2, t1, t2, s1=0, s2=0, l1, l2, z=0;
printf("\nEnter the number of integers: ");
scanf("%d", &n);
printf("\nEnter %d values................\n", n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);

25
for(i=0; i<n; i++)
{
for(j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1])
{
a[j+1]=a[j]+a[j+1];
a[j]=a[j+1]-a[j];
a[j+1]=a[j+1]-a[j];
}
}
}
printf("\nThe sorted array is......................\n");
for(i=0; i<n; i++)
printf("%d\t", a[i]);
printf("\nEnter two index positions: ");
scanf("%d %d", &p, &q);
if((p>=n || p<0) || (q>=n || q<0))
printf("\nPlease enter correct indices.");
else
{
k1=a[p];
k2=a[q];
for(i=0; i<n; i++)
{
if(a[i]==k1)
{
t1=i;
break;
}
}
for(i=0; i<n; i++)
{
if(a[i]==k2)
{
t2=i;
break;
}

26
}
for(i=0; i<n; i++)
{
if(a[i]==k1)
s1++;
}
for(i=0; i<n; i++)
{
if(a[i]==k2)
s2++;
}
printf("\nEnter the index positions to start counting: ");
scanf("%d %d", &l1, &l2);
if(l1>=t1 && l1<t1+s1 && l2>=t2 && l2<t2+s2)
{
for(i=l1; i<=l2; i++)
{
z=z+a[i];
}
printf("\nRequired sum= %d", z);
}
else
printf("\nWrong index entered.");
}
getch();
return 0;
}

Output:

Enter the number of integers: 10

Enter 10 values................
32
12
12
12
32
45

27
32
56
56
12

The sorted array is......................


12 12 12 12 32 32 32 45 56 56
Enter two index positions: 0 6

Enter the index positions to start counting: 1 5

Required sum= 100

28

Vous aimerez peut-être aussi