0% found this document useful (0 votes)
13 views49 pages

Practical Mannual PCT

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)
13 views49 pages

Practical Mannual PCT

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/ 49

Government Polytechnic Amravati Practical Manual

Practical No. 01
Aim: Write algorithm and draw flow chart for following problems:
(a) Addition of two numbers
(b) Exchange value of two variables

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Algorithm: (a) Addition of two numbers

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values for num1, num2.

Step 4: Add num1 and num2 and assign the result to a variable sum.

Step 5: Display sum

Step 6: Stop

Algorithm: (b) Exchange value of two variables

Step 1: Start

Step 2: Read value of A

Step 3: Read value of B

Step 4: C=A

Step 5: A=B

Step 6: B=C

Step 7: Print A

Step 8: Print B

Step 9: Stop

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 1
Government Polytechnic Amravati Practical Manual

Flowchart: (a) Addition of two numbers

Flowchart: (b) Exchange value of two variables

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 2
Government Polytechnic Amravati Practical Manual

Program Code: (a) Addition of two numbers

#include <stdio.h>
void main()
{
int A, B, sum;

printf("\nEnter the First Number: ");


scanf("%d", &A);

printf("\nEnter the Second Number: ");


scanf("%d", &B);

sum = A + B;
printf("Sum of Numbers %d + %d = %d", A,B, sum);

return 0;
}

(b) Exchange value of two variables

#include<stdio.h>
void main()
{
int a,b,c;

printf("Enter two variables before swapping:\n");

printf("a:");
scanf("%d", &a);

printf("b:");
scanf("%d", &b);

printf("Values stored in a:%d and b:%d", a,b);

c=a;
a=b;
b=c;

printf("\n Values after swapping a=%d and b=%d", a,b);


}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 3
Government Polytechnic Amravati Practical Manual

Output: (a) Addition of two numbers

Enter the First Number: 20

Enter the Second Number: 10

Sum of Numbers 20 + 10 = 30

(b) Exchange value of two variables

Enter first number: 120

Enter second number: 245

After exchange, first number = 245

After exchange, second number = 120

Result:
Thus, we perform the program for Addition of two numbers and Exchange value of two variables.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 4
Government Polytechnic Amravati Practical Manual

Practical No. 02
Aim: Write a „C‟ program to display hexadecimal, decimal, octal format of entered number using
%d, %c, %i, %f, %g, %u, %o, %s, %x

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

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

int n;
printf("Enter a number: ");
scanf("%d",&n);

printf("\nDecimal value: %d",n);

printf("\nHexa Decimal value : %x",n);

printf("\nHexa Decimal value : %X",n);

printf("\nOctal value : %o",n);

Output:

Enter a number: 45

Decimal value: 45

Hexa Decimal value: 2d

Hexa Decimal value: 2D

Octal value: 55

Result:

Thus, we perform the „C‟ program to display hexadecimal, decimal, octal format of entered number
using %d, %c, %i, %f, %g, %u, %o, %s, %x.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 5
Government Polytechnic Amravati Practical Manual

Practical No. 03
Aim: Write a program to perform following operations:
a) First input name, address, date of birth and email_id using scanf() function and then display the
same using printf ( ) function.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

#include <stdio.h>
void main()
{
char name[20], address[20], email[30];
int DOB;

printf("Input your name: ");


scanf("%s", name);

printf("Input your address: ");


scanf("%s", address);

printf("Input your date of birth: ");


scanf("%d", &DOB);

printf("Input your email: ");


scanf("%s", email);

printf("\n\n%s \n%s \n%d \n%s \n", name, address, DOB, email);


return 0;
}

Output:

Input your name: Daddy


Input your address: Home
Input your date of birth: 01021950
Input your email: [email protected]

Daddy
Home
1021950
[email protected]

Result:
Thus, we perform the „C‟ program to display a) First input name, address, date of birth and email_id
using scanf() function and then display the same using printf ( ) function.
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 6
Government Polytechnic Amravati Practical Manual

Practical No. 04
Aim: (a) Write a program to calculate Inductive Resistance (FL)
with the help of given formula FL = 2 * π*f L. Where π, f, L are given data.
(b) Write a program to calculate Capacitive Resistance (FC)
with the help of given formula FC = 1/(2 * π*f C). Where π, f, C are given data.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code: (a) Inductive Resistance (FL)

#include <stdio.h>
void main()
{
float f, L, FL;

printf("\n Enter the value of f=hz ");


scanf("%f", &f);

printf("\n Enter the value of L=ohm ");


scanf("%f", &L);

FL = 2*3.14*f*L;
printf("The Calculated Inductive Resistance (FL) is %f. ", FL);

return 0;
}

(b) Capacitive Resistance (FC)

#include <stdio.h>
void main()
{
float f, C, FC;

printf("\n Enter the value of f=hz ");


scanf("%f", &f);

printf("\n Enter the value of C=uf ");


scanf("%f", &C);

FC = 1/(2*3.14*f*C);
printf("The Calculated Capacitive Resistance (FC) is %f .", FC);

return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 7
Government Polytechnic Amravati Practical Manual

Output: (a) Inductive Resistance (FL)

Enter the value of f=hz 2.13

Enter the value of L=ohm 5.46

The Calculated Inductive Resistance (FL) is 73.035149.

(b) Capacitive Resistance (FC)

Enter the value of f=hz 5

Enter the value of C=uf 3.37

The Calculated Capacitive Resistance (FC) is 0.009450.

Result:
Thus, we perform the „C‟ program to calculate (a) Inductive Resistance (FL) & (b) Capacitive
Resistance (FC).

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 8
Government Polytechnic Amravati Practical Manual

Practical No. 05
Aim: (a) Write a C program to find the sum of individual digits of a positive integer.
(b) Write a C program to find whether entered number is prime or not.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code: (a) Write a C program to find the sum of individual digits of a positive integer.

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

{
int n,sum=0;

printf("Enter A +ve Integer: ");


scanf("%d", &n);

while(n>0)
{
sum=sum+n%10;
n=n/10;
}
printf("Sum of individual digits of a positive integer is: %d", sum);
}

(b) Write a C program to find whether entered number is prime or not.

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

int n, i, c = 0;

printf("Enter any number n=");


scanf("%d", &n);

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 9
Government Polytechnic Amravati Practical Manual

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


{
if (n % i == 0)
{
c++;
}
}

if (c == 2)
{
printf(" „n‟ is a Prime number");
}
else
{
printf(" „n‟ is not a Prime number");
}
return 0;
}

Output: (a) Write a C program to find the sum of individual digits of a positive integer.

Enter A +ve Integer: 123456

Sum of individual digits of a positive integer is: 21

(b) Write a C program to find whether entered number is prime or not.

Enter any number n = 45.

„n‟ is not a Prime number.

Result:
Thus, we perform the „C‟ program to find the sum of individual digits of a positive integer
and to find whether entered number is prime or not.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 10
Government Polytechnic Amravati Practical Manual

Practical No. 06
Aim: (a) Write a program to find the largest among 5 numbers using 'if- else'.
(b) Write a program using If-else to find whether given number is even or odd.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code: (a) Write a program to find the largest among 5 numbers using 'if- else'.

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

int a,b,c,d,e;

printf("\nENTER THE FIVE NUMBERS:\n");


scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);

if(a>b && a>c && a>d && a>e)


printf("%d is largest number.", a);

else
if(b>c && b>d && b>e)
printf("%d is largest number.", b);

else
if(c>d && c>e)
printf("%d is largest number.", c);

else
if(d>e)
printf("%d is largest number.", d);

else
printf("%d is largest number.", e);

return 0;
getch ();

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 11
Government Polytechnic Amravati Practical Manual

(b) Write a program using If-else to find whether given number is even or odd.

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

int num;

printf("Enter any number: ");


scanf("%d",&num);

if(num % 2 == 0)

printf("Entered Number is Even.");

else

printf("Entered Number is Odd.");

return 0;
}

Output: (a) Write a program to find the largest among 5 numbers using 'if- else'.

ENTER THE FIVE NUMBERS:

10 56 42 89 35

89 is largest number.

(b) Write a program using If-else to find whether given number is even or odd.

Enter any number: 672676746

Entered Number is Even.

Result:
Thus, we perform the „C‟ program to find the largest among 5 numbers using 'if- else' and a
program using If-else to find whether given number is even or odd.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 12
Government Polytechnic Amravati Practical Manual

Practical No. 07
Aim: Determine whether a string is palindrome.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

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

char s[1000];
int i,n,c=0;

printf("Enter the string : ");


gets(s);
n=strlen(s);

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

{
if(s[i]==s[n-i-1])
c++;

if(c==i)
printf("String is palindrome.");
else
printf("String is not palindrome.");

return 0;
}

Output:

Enter the string: oppo

String is palindrome.

Result:
Thus, we perform the „C‟ program to find the whether a string is palindrome or not.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 13
Government Polytechnic Amravati Practical Manual

Practical No. 08
Aim: Write a program to perform addition, subtraction; multiplication and division according to
user‟s choice using switch case statement for given data.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:
#include<stdio.h>
void main()
{

int a, b;
char choice;
printf("Enter your choice:\n");

printf(" A. Addition\n B. Subtraction\n C. Multiplication\n D. Division\n");


scanf("%c", &choice);

printf("Enter 2 integer numbers:\n");


scanf("%d %d", &a, &b);

switch(choice)
{
case 'a': printf("%d + %d = %d\n", a, b, (a+b));
break;

case 'b': printf("%d - %d = %d\n", a, b, (a-b));


break;

case 'c': printf("%d x %d = %d\n", a, b, (a*b));


break;

case 'd': if( b != 0)


printf("%d / %d = %d\n", a, b, (a/b));

else
printf("Number can't be divided by 0\n");

break;
default: printf("You entered wrong choice\n");
break;

}
return 0;
getch ();
}
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 14
Government Polytechnic Amravati Practical Manual

Output:

Enter your choice:

A. Addition
B. Subtraction
C. Multiplication
D. Division

Enter 2 integer numbers:


45 10
45 x 10 = 450

Result:
Thus, we perform the „C‟ program to perform addition, subtraction; multiplication and
division according to user‟s choice using switch case statement for given data.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 15
Government Polytechnic Amravati Practical Manual

Practical No. 09
Aim: a) Write a C program to construct a pyramid of numbers.
b) Write a program to count the number of digits in a number.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code: (a) Write a C program to construct a pyramid of numbers.

1]
#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter the number of rows: ");


scanf("%d", &rows);

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


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

2]
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 16
Government Polytechnic Amravati Practical Manual

(b) Write a program to count the number of digits in a number.

#include<stdio.h>
int main()
{
long long n;
int count = 0;

printf("Enter An Integer: ");


scanf("%lld", &n);

do
{
n /= 10;
++count;
}
while (n != 0);
printf("Number of Digits In Integers Are: %d", count);
}

Output: (a) Write a C program to construct a pyramid of numbers.

1] Enter the number of rows: 5

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

2] Enter the number of rows: 5


12345
1234
123
12
1

(b) Write a program to count the number of digits in a number.

Enter an Integer: 2646462464664164

Number of Digits in Integers Are: 16

Result:
Thus, we perform the „C‟ program to construct a pyramid of numbers and a program to
count the number of digits in a number.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 17
Government Polytechnic Amravati Practical Manual

Practical No. 10
Aim: (a) Find the greatest of the three numbers using conditional operators.
(b) Find the smallest of the three numbers using conditional operators.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code: (a) Find the greatest of the three numbers using conditional operators.

# include <stdio.h>
void main()
{
int a, b, c, big ;

printf("Enter three numbers : ") ;


scanf("%d %d %d", &a, &b, &c) ;

big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

printf("\nThe biggest number is : %d", big) ;

(b) Find the smallest of the three numbers using conditional operators.

#include<stdio.h>
void main()
{
int a, b, c;

printf("Enter three numbers:\n");


scanf("%d %d %d", &a, &b, &c);

((a < b) && (a < c))?

printf("a is smallest.\n"):

(b < c)?

printf("b is smallest.\n"):

printf("c is smallest.\n");
}
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 18
Government Polytechnic Amravati Practical Manual

Output: (a) Find the greatest of the three numbers using conditional operators.

Enter three numbers: 4 9 3

The biggest number is: 9

(b) Find the smallest of the three numbers using conditional operators.

Output 1

Enter three numbers:


10
24
65
a is smallest

Output 2

Enter three numbers:


45
31
65
b is smallest

Output 3

Enter three numbers:


78
54
31
c is smallest

Result:
Thus, we perform the „C‟ program to find the greatest of the three numbers using conditional
operators and smallest of the three numbers using conditional operators.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 19
Government Polytechnic Amravati Practical Manual

Practical No. 11
Aim: Write a program to print the following outputs:

(a) (b)

1 1
2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5

(c) (d)

1 *
222 ***
33333 *****
4444444 *******
555555555 *********

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a)

#include <stdio.h>
void main()
{
int i, j, rows;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf ("%d ", i); // print the number
}
printf ("\n");
}
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 20
Government Polytechnic Amravati Practical Manual

(b)

#include <stdio.h>
void main()
{
int i,j,spc,rows,k;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",i);
printf("\n");
spc--;
}
}

(c)

#include <stdio.h>
void main()
{
int i, j, rows, k = 0;
printf (" Enter a number to define the rows: \n");
scanf ("%d", &rows); // take a number
for ( i =1; i <= rows; i++)
{
for ( j = 1; j <= rows - i; j++)
{
printf (" "); // print the space
}
for ( k = 1; k <= ( 2 * i - 1); k++)
{
printf ("%d ",i); // print the number
}
printf ("\n");
}
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 21
Government Polytechnic Amravati Practical Manual

(d)

#include <stdio.h>
int main()
{
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

Output:

(a) (b)

1 1
2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5

(c) (d)

1 *
222 ***
33333 *****
4444444 *******
555555555 *********

Result:
Thus, we perform the „C‟ program to print the above outputs.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 22
Government Polytechnic Amravati Practical Manual

Practical No. 12
Aim: Write a program to declare, modify and print elements of an one dimensional array.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:
(a)
#include<stdio.h>
void main()
{
int n[5]={0, 1, 2, 3, 4};

printf("%d", n[0]);

printf("%d", n[1]);

printf("%d", n[2]);

printf("%d", n[3]);

printf("%d", n[4]);

(b)

#include<stdio.h>
void main ()
{
int a[5], i;
printf ("Enter Array Elements:\n");

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


{
scanf ("%d", &a[i]); //Run time array initialization
}

printf ("Entered Array Elements are : ");

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


{
printf ("%d ", a[i]);
}
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 23
Government Polytechnic Amravati Practical Manual

Output:

(a)

01234

(b)

Enter Array Elements:


10 200 3000 40000 500000

Entered Array Elements are: 10 200 3000 40000 500000

Result:
Thus, we perform the „C‟ program to declare, modify and print elements of an one
dimensional array.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 24
Government Polytechnic Amravati Practical Manual

Practical No. 13
Aim: a) Write a program to search an element from Array.
b) Write a program to find smallest / largest number from array elements.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:
(a)

#include <stdio.h>
int main()
{
int nbr, i, r, arr[30];

printf("Enter the number of elements in the array: ");


scanf("%d", &nbr);

printf("Enter the array elements: ");


for (i = 0; i < nbr; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the item to be searched: ");


scanf("%d", &r);

//Research starts from the index 0


i = 0;
while (i < nbr && r != arr[i])
{
i++;
}

if (i < nbr)
{
printf("The element is found in the position = %d", i + 1);
}
else
{
printf("Element not found!");
}
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 25
Government Polytechnic Amravati Practical Manual

(b)
#include<stdio.h>
void main ()
{
int a[50],i,n,large,small;

printf("How many elements:");


scanf("%d",&n);

printf("Enter the Array:");

for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}

Output:

(a)

Enter the number of elements in the array: 10


Enter the array elements: 1 2 3 4 5 6 7 8 9 0
Enter the item to be searched: 7
The element is found in the position = 7

(b)

How many elements: 5


Enter the Array: 1 18 36 12 6
The largest element is 36
The smallest element is 1

Result:
Thus, we perform the „C‟ program to search an element from array and to find smallest /
largest number from array elements.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 26
Government Polytechnic Amravati Practical Manual

Practical No. 14
Aim: a) Write a program to calculate addition of 2 dimensional matrix.
b) Write a program to calculate subtraction of 2 dimensional matrix.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a)

#include <stdio.h>
int main()
{
int a[2][3],b[2][3],c[2][3],i,j;

printf("\nENTER VALUES FOR MATRIX A:\n");


for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nENTER VALUES FOR MATRIX B:\n");


for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);

for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];

printf("\nTHE VALUES OF MATRIX C ARE:\n");


for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 27
Government Polytechnic Amravati Practical Manual

(b)
#include <stdio.h>
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;
printf("\n\nSubtraction of two Matrices :\n");
printf("------------------------------\n");
printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &n);
printf("Input elements in the first matrix :\n"); /* Stored values into the array*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]);
}
printf("\nThe Second matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);
}
for(i=0;i<n;i++) /* calculate the subtraction of the matrix */
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]-brr1[i][j];
printf("\nThe Subtraction of two matrix is : \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n\n"); }
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 28
Government Polytechnic Amravati Practical Manual

Output:

(a)

ENTER VALUES FOR MATRIX A: 123123

ENTER VALUES FOR MATRIX B: 123123

THE VALUES OF MATRIX C ARE: 2 4 6


2 4 6

(b)

The First matrix is:

1 1
1 1

The Second matrix is:

1 1
1 1

The Subtraction of two matrixes is:

0 0
0 0

Result:
Thus, we perform the „C‟ program to calculate addition and subtraction of 2 dimensional
matrix.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 29
Government Polytechnic Amravati Practical Manual

Practical No. 15
Aim: a) Write a program that accept a string from user and print that string.
b) Write a program that accept a string and compare it with existing string.
c) Write a program to accept and display that string in reverse order.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a)

#include<stdio.h>
int main()
{
char str[20];
printf("Enter your first name: ");
scanf("%s", str);
printf("Hello %s", str);
getch();
return 0;
}

(b-1)

#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100], Str2[100];
int result, i;

printf("\n Please Enter the First String : ");


gets(Str1);

printf("\n Please Enter the Second String : ");


gets(Str2);

for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);

if(Str1[i] < Str2[i])


{
printf("\n str1 is Less than str2");
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 30
Government Polytechnic Amravati Practical Manual

else if(Str1[i] > Str2[i])


{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}

return 0;
}

(b-2)

#include<stdio.h>
#include<string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string: ");
gets(a);

printf("Enter the second string: ");


gets(b);

if( strcmp(a,b) == 0 )
printf("Result: Entered strings are equal.\n");
else
printf("Result: Entered strings are not equal.\n");
return 0;
}

(c)

#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);

// use strrev() function to reverse a string


printf(" \n After the reverse of a string: %s", strrev (str));
return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 31
Government Polytechnic Amravati Practical Manual

Output:

(a)

Enter your first name: Pavansut


Hello Pavansut

(b-1)

Please Enter the First String: dkshfsdkbnkan


Please Enter the Second String: hdcvklnaklncvla
str1 is Less than str2

(b-2)

Enter the first string: Government


Enter the second string: Polytechnic
Result: Entered strings are not equal.

(c)

Enter a string to be reversed: ECNALUBMA


After the reverse of a string: AMBULANCE

Result:
Thus, we perform the „C‟ program to accept a string from user and print that string, to accept
a string and compare it with existing string and to accept and display that string in reverse order.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 32
Government Polytechnic Amravati Practical Manual

Practical No. 16
Aim: a) Write a program to accept and concatenate two strings.
b) Write a program to find length of a string.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a)

#include<stdio.h>
#include<string.h>
int main()
{
char str1[50], str2[50];

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

strcat(str1, str2);
printf("\nString after concatenation is: %s", str1);
return 0;
}

(b)

#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;

printf("Enter a string to calculate its length:\n");


gets(a);

length = strlen(a);

printf("Length of the string: = %d\n", length);

return 0;
}
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 33
Government Polytechnic Amravati Practical Manual

Output:

(a)

Enter first string: Best Luck


Enter second string: Students.
String after concatenation is: Best Luck Students.

(b)

Enter a string to calculate its length:


Amravati, Maharashtra, India.
Length of the string: = 29

Result:
Thus, we perform the „C‟ program to accept and concatenate two strings and to find length of
a string.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 34
Government Polytechnic Amravati Practical Manual

Practical No. 17
Aim: a) Write a program to convert given string to lower case.
b) Write a program to convert given string to upper case.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a)

#include <stdio.h>
#include <conio.h>
int main ()
{
char upr[50]; // declare character array

printf (" Enter the upper case string: ");


gets (upr); // use gets() function to take string

// use strlwr() function to change upper case into lower string


printf (" \n The lowercase string is: %s", strlwr(upr));

return 0;
}

(b)

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];

/* Input string from user */


printf("Enter your text : ");
gets(str);

strupr(str); // Convert to uppercase

printf("Uppercase string : %s", str);

return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 35
Government Polytechnic Amravati Practical Manual

Output:

(a)

Enter the upper case string: AMRAVATI


The lowercase string is: amravati

(b)

Enter your text: amravati


Uppercase string: AMRAVATI

Result:
Thus, we perform the „C‟ program to convert given string to lower case and to convert given
string to upper case.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 36
Government Polytechnic Amravati Practical Manual

Practical No. 18
Aim: a) Write a program to add two numbers using user defined function.
b) Write a program to find factorial of number using function for given data.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:
(a)
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Calling the function
num3 = sum(num1, num2);
printf("Sum of the entered numbers: %d", num3);
return 0;
}
int sum(int a, int b)
{
return a+b;
}

(b)
#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for (i = 1; i <= n; ++i)
{
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 37
Government Polytechnic Amravati Practical Manual

Output:

(a)

Enter first number: 614646161


Enter second number: 46546464
Sum of the entered numbers: 661192625

(b)

Enter an integer: 7
Factorial of 7 = 5040

Result:
Thus, we perform the „C‟ program to add two numbers using user defined function and to
find factorial of number using function for given data.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 38
Government Polytechnic Amravati Practical Manual

Practical No. 19
Aim: Write a program to interchange given values of two variables using call by value mechanism.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

#include<stdio.h>
void swap(int,int);
void main( )
{
int n1,n2;

printf("Enter the two numbers to be swapped: ");


scanf("%d%d",&n1,&n2);

printf("\nThe values of n1 and n2 in the main function before calling the swap function are n1=%d
n2=%d",n1,n2);

swap(n1,n2);

printf("\nThe values of n1 and n2 in the main function after calling the swap function are n1=%d
n2=%d",n1,n2);
}

void swap(int n1,int n2)


{
int temp;
temp=n1;
n1=n2;
n2=temp;
printf("\nThe values of n1 and n2 in the swap function after swapping are n1=%d n2=%d",n1,n2);
}

Output:

Enter the two numbers to be swapped: 17 54


The values of n1 and n2 in the main function before calling the swap function are n1=17 n2=54
The values of n1 and n2 in the swap function after swapping are n1=54 n2=17
The values of n1 and n2 in the main function after calling the swap function are n1=17 n2=54

Result:
Thus, we perform the „C‟ program to interchange given values of two variables using call by
value mechanism.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 39
Government Polytechnic Amravati Practical Manual

Practical No. 20
Aim: Write a program to interchange given values of two variables using call by reference
mechanism.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

#include <stdio.h>
swap (int *, int *);
int main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Output:

Enter value of a & b: 5 7

Before Swapping:
a=5
b=7

After Swapping:
a=7
b=5

Result:
Thus, we perform the „C‟ program to interchange given values of two variables using call by
reference mechanism.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 40
Government Polytechnic Amravati Practical Manual

Practical No. 21
Aim: a) Write a program to add two integer numbers using pointer.
b) Write a program to find area of circle using pointer.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a-1)

#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}

(a-2)

#include<stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2;
printf("Enter any two Number: ");
scanf("%d%d", &num1, &num2);
printf("\nAddress of %d is %p", num1, &num1);
printf("\nAddress of %d is %p", num2, &num2);
ptr1 = &num1;
ptr2 = &num2;
printf("\n\nptr1 = %p", ptr1);
printf("\nptr2 = %p", ptr2);
printf("\n\nValue at %p is %d", ptr1, *ptr1);
printf("\nValue at %p is %d", ptr2, *ptr2);
sum = *ptr1 + *ptr2;
printf("\n\nSum of %d and %d is %d", *ptr1, *ptr2, sum);
return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 41
Government Polytechnic Amravati Practical Manual

(b)
#include<stdio.h>

void areaperi ( int r, float *a, float *p )


{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

void main( )
{
int radius ;
float area, perimeter ;

printf ( "Enter radius of a circle: " ) ;


scanf ( "%d", &radius ) ;

areaperi ( radius, &area, &perimeter ) ;

printf ( "\nArea = %f cm", area ) ;


printf ( "\nPerimeter = %f cm", perimeter ) ;
}

Output:

(a-1)
Enter two integers to add
64616 6464361
Sum of entered numbers = 6528977

(a-2)
Enter any two Numbers: 454634 84278687
Address of 454634 is 0x7fff2714a51c
Address of 84278687 is 0x7fff2714a520

ptr1 = 0x7fff2714a51c
ptr2 = 0x7fff2714a520

Value at 0x7fff2714a51c is 454634


Value at 0x7fff2714a520 is 84278687

Sum of 454634 and 84278687 is 84733321

(b)
Enter radius of a circle: 6.18 cm
Area = 113.040001 cm
Perimeter = 37.680000 cm

Result:
Thus, we perform the „C‟ program to add two integer numbers using pointer and to find area
of circle using pointer.
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 42
Government Polytechnic Amravati Practical Manual

Practical No. 22
Aim: Write a program to create a structure for employee having data members like emp_name,
emp_id, and emp_salary.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
char name[30];
int id;
double salary;
} Employee;

int main()
{
//number of employees
int n=2;

//array to store structure values of all employees


Employee employees[n];

//Taking each employee detail as input


printf("Enter %d Employee Details: \n",n);
for(int i=0; i<n; i++)
{
printf("\n\nEmployee %d:- ",i+1);

//Name
printf("\nName: ");
scanf("%[^\n]s",employees[i].name);

//ID
printf("Id Code: ");
scanf("%d",&employees[i].id);

//Salary
printf("Salary: Rs. ");
scanf("%lf",&employees[i].salary);

//to consume extra '\n' input


char ch = getchar();
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 43
Government Polytechnic Amravati Practical Manual

printf("\n\n\n");
}

//Displaying Employee details


printf("\n\n-------------- All Employees Details ---------------\n\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);

printf("Id Code\t: ");


printf("%d \n",employees[i].id);

printf("Salary \t: Rs.");


printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}

Output:

Enter 2 Employee Details:

Employee 1:-
Name: Mr. Rakesh
Id Code: 100101
Salary: Rs. 50000
Employee 2:-
Name: Mr. Anil
Id Code: 100102
Salary: Rs. 45000

-------------- All Employees Details ---------------

Name : Mr. Rakesh


Id Code : 100101
Salary : Rs. 50000.00

Name : Mr. Anil


Id Code : 100102
Salary : Rs. 45000.00

Result:
Thus, we perform the „C‟ program to create a structure for employee having data members
like emp_name, emp_id, and emp_salary.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 44
Government Polytechnic Amravati Practical Manual

Practical No. 23
Aim: a) Write a program to add two integer numbers using pointer.
b) Write a program to find area of circle using pointer.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

(a-1)

#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}

(a-2)

#include<stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2;
printf("Enter any two Number: ");
scanf("%d%d", &num1, &num2);
printf("\nAddress of %d is %p", num1, &num1);
printf("\nAddress of %d is %p", num2, &num2);
ptr1 = &num1;
ptr2 = &num2;
printf("\n\nptr1 = %p", ptr1);
printf("\nptr2 = %p", ptr2);
printf("\n\nValue at %p is %d", ptr1, *ptr1);
printf("\nValue at %p is %d", ptr2, *ptr2);
sum = *ptr1 + *ptr2;
printf("\n\nSum of %d and %d is %d", *ptr1, *ptr2, sum);
return 0;
}

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 45
Government Polytechnic Amravati Practical Manual

(b)
#include<stdio.h>

void areaperi ( int r, float *a, float *p )


{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

void main( )
{
int radius ;
float area, perimeter ;

printf ( "Enter radius of a circle: " ) ;


scanf ( "%d", &radius ) ;

areaperi ( radius, &area, &perimeter ) ;

printf ( "\nArea = %f cm", area ) ;


printf ( "\nPerimeter = %f cm", perimeter ) ;
}

Output:

(a-1)
Enter two integers to add
64616 6464361
Sum of entered numbers = 6528977

(a-2)
Enter any two Numbers: 454634 84278687
Address of 454634 is 0x7fff2714a51c
Address of 84278687 is 0x7fff2714a520

ptr1 = 0x7fff2714a51c
ptr2 = 0x7fff2714a520

Value at 0x7fff2714a51c is 454634


Value at 0x7fff2714a520 is 84278687

Sum of 454634 and 84278687 is 84733321

(b)
Enter radius of a circle: 6.18 cm
Area = 113.040001 cm
Perimeter = 37.680000 cm

Result:
Thus, we perform the „C‟ program to add two integer numbers using pointer and to find area
of circle using pointer.
Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 46
Government Polytechnic Amravati Practical Manual

Practical No. 24
Aim: Write simple Python program to print “Hello World” on the screen.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

# Python 'Hello world' program

print("Hello World")

Output:

Hello World

Result:
Thus, we perform the simple Python program to print “Hello World” on the screen.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 47
Government Polytechnic Amravati Practical Manual

Practical No. 25
Aim: Write a Python program to add two numbers and display result on screen.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

# Python 'Hello world' program

a = int(input("Enter First Number: "))


b = int(input("Enter Second Number: "))

sum = a + b

print("Sum:", sum)

Output:

Enter First Number: 1641616


Enter Second Number: 768461664
Sum: 770103280

Result:
Thus, we perform Python program to add two numbers and display result on screen.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 48
Government Polytechnic Amravati Practical Manual

Practical No. 26
Aim: Write a Python program to find out smallest of three numbers using if—elif.

Hardware Requirement‟s: All input & output devices with hardware peripherals.

Software Requirement‟s: System of I5 Generation with OS Windows 7 or newer and Compilers like
Microsoft Visual Studio, Code Blocks Software, Turbo C, Online Compliers, etc.

Program Code:

num1=int(input("Enter the First Number: ")) #Take input from user for first number

num2=int(input("Enter the Second Number: ")) #Take input from user for second number

num3=int(input("Enter the Third Number: ")) #Take input from user for third number

if(num1<=num2 and num1<=num3): #Compare the first number with the second and third number
print(num1,"is the smallest.")

elif(num2<=num1 and num2<=num3): #Compare the second number with the first and third number
print(num2,"is the smallest.")

else:
print(num3,"is the smallest.")

Output:

Enter the First Number: 46161


Enter the Second Number: 5465
Enter the Third Number: 56454646
5465 is the smallest.

Result:
Thus, we perform a Python program to find out smallest of three numbers using if—elif.

Course Code & Title: EC 3409 “C & Python Programming” By Prof. H. S. Holey Page 49

You might also like