Você está na página 1de 34

MADANAPALLE INSTITUTE OF TECHNOLOGY & SCIENCE

(AUTONOMOUS)
B. Tech I Year
(Common to all branches)
COMPUTER PROGRAMMING PRACTICALS
(14CSU12P02)

LIST OF EXPERIMENTS
1) a) Write a C program to swap the two numbers.
b) Write a C program to find the roots of a quadratic equation.
c) Write a C program to compute the factorial of a given number.
2) a) Write a C program to find the series of prime numbers in the given range.
b) Write a C program to generate Fibonacci numbers in the given range.
3) a) Write a C program to check for number palindrome.
b) Write a C program to generate Pascal Triangle.
4) Implement the following operations on matrices using C
a) Sum of Two Matrices b) Product of Two matrices
c) Transpose of Matrix
5) Write a C program to find Factorial, GCD, fibonacci, towers of hanoi, sum of digits, base
conversions, reversal of numbers. (Using recursion).
6) Write a C program to implement all string operations(strlen(), strcpy(), , strcmp(),
strcat(), strrev(), strstr(), strchr()) without using standard string library functions.
7) Write a C program to find the student grade by using structures.
8) Write a C program to perform the operations addition, subtraction, multiplication of
Complex numbers using structures.
9) Write a C program to copy the file contents from one file to another file(pass file names
as command line arguments).

WEEK 1
A) Write a C program to swap the two numbers.
Algorithm

Step 1: Start
step 2: read 'a' and 'b' value
step 3: inter change the values
temp = a
a = b
b = temp
step 4: write a and b values.
Step 5: stop
Program
#include <stdio.h>
void main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
}

printf("After Swapping\nx = %d\ny = %d\n",x,y);

Output

b) Write a C program to find the roots of a quadratic equation.


Algorithm
1. Read the value of a , b , c
2. d = b*b - 4*a*c
3. a.) If d < 0 then display The Roots are Imaginary.
b.) Else if d = 0
Then display Roots are Equal.
r = -b / 2*a
Display r
c.) Else
r1 = -b + d / 2*a
r2 = -b - d / 2*a
Display Roots are real and r1, r2
4. Stop
Program
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,d;
double r1,r2;
printf("enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if (d<0)
{
printf("the roots are imaginary");
r1=(-b+sqrt((b*b)-(4*a*c)))/2*a;
r2=(+b+sqrt((b*b)-(4*a*c)))/2*a;
printf("%lf\n%lf\n",r1,r2);
}
else if (d>0)
{
printf("the roots are real and unequal");
r1=(-b+sqrt((b*b)-(4*a*c)))/2*a;
r2=(+b+sqrt((b*b)-(4*a*c)))/2*a;
printf("%lf%lf",r1,r2);
}
else
{
printf("the roots are real and equal");
r1=(-b)/2*a;
r2=(+b)/2*a;
printf("%lf%lf",r1,r2);
}}

Output

c) Write a C program to compute the factorial of a given number.


Algorithm

step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Program

#include<stdio.h>
Void main()
{
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);

while(i<=num){
f=f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);

Output

WEEK 2

A.) Write a C program to find the series of prime numbers in the given range.
Algorithm
Step 1:
Step 2:

Input N & M
While (N < M)
I=2
Step 4:
While (I<N)
Step 5:
IF N%I == 0
goto Step 7
Step 6:
I++
Step 7: IF I==NUM
Print NUM
Step 7:
N++
Step 8: end

Program

#include <stdio.h>
int main()
{
int n1, n2, i, j, flag;
printf("Enter two numbers(intevals): ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
flag=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d ",i);
}
return 0;

}
Output

b) Write a C program to generate Fibonacci numbers in the given range.


Algorithm

1. Start
2. Declare variables i, a,b , show
3. Initialize the variables, a=0, b=1, and show =0
4. Enter the number of terms of Fibonacci series to be printed
5. Print First two terms of series
6. Use loop for the following steps
->show=a+b
->a=b
->b=show
->increase value of i each time by 1
-> print the value of show
7. End

Program
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.

for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
}

return 0;

Output

WEEK - 3
a) Write a C program to check for number palindrome.
Description
A palindromic number or numeral palindrome is a number that remains the same when its digits are
reversed.

Algorithm
step 1 : input n
step 2 : s = 0, temp=n
step 3 : while(n>0)
{
rem=n%10
s=s*10+rem
n=n/10
}
step 4 : if(s==temp)
print 'it is a palindrome'
else
print 'it is not a palindrome'
step 5 : stop

Program
#include <stdio.h>
void main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
if(reverse==n)
printf("%d is a palindrome.",n);

else
printf("%d is not a palindrome.",n);
}

Output

b) Write a C program to generate Pascal Triangle.

Algorithm
1. Start
2. Declare variables x, y, n, a, z, s
3. Enter the limit
4. Initialize the value of variables, s=n , x=0, y=0 , z=s
5. Do the following operations in loop
a. x = 0 to n
b. a= 1, x++
c. z=s to 0
d. print space
e. zf. y = o to x
g. print a
h. a = a*(x-y)/(y+1)
i. y= y+1
j. go to next line
6. Repeat the process to n
7. Print the final required triangle

8. Stop

Program
#include<stdio.h>
void main(){
int rows,i,j,k;
printf("Enter the rows of pascal's triangle : ");
scanf("%d",&rows);

for(i=1;i<= rows;i++) {
for(j=1;j<= rows-i;j++)
printf(" ");
for(k=1;k<i;k++)
printf("%d",k);
for(k=i;k>=1;k--)
printf("%d",k);
printf("\n");
}

Output

Week 4
4) Implement the following operations on matrices using C
a) Sum of Two Matrices
Algorithm

1. Start
2. Declare variables and initialize necessary variables
3. Enter the element of matrices by row wise using loops
4. Check the number of rows and column of first and second matrices
5. Add the matrices using nested loops.
6. Print the addition in matrix form as console output.
7. Stop

Program
#include<stdio.h>
void main()
{
int i,j,c,d,m1[10][10],m2[10][10],s[10][10];
printf("enter the matrix1 size\n");
scanf("%d",&i);
scanf("%d",&j);
printf("enter the elements of matrix1\n");
for(c=0;c<i;c++)
{
for(d=0;d<j;d++)
{
scanf("%d",&m1[c][d]);
}
}
printf("enter the matrix2 size\n");
scanf("%d",&i);
scanf("%d",&j);
printf("enter the elements of matrix2\n");
for(c=0;c<i;c++)
{
for(d=0;d<j;d++)
{
scanf("%d",&m2[c][d]);
}

}
for(c=0;c<i;c++)
{
for(d=0;d<j;d++)
{
s[c][d]=m1[c][d]+m2[c][d];
printf("%d\t",s[c][d]);
}
printf(\n);
}
}
Output

b) Transpose of Matrix
Algorithm
1. START
2.Take m1 and m2 as integer matrix.
3. Input the values for original matrix and store it in m1.
4. Convert each row of the matrix ma in to column of matrix m2.
5. Display both matrix m1 and m2.
6. STOP.

Program
#include <stdio.h>
void main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix\n");

for (c = 0; c < m; c++)


{
for(d = 0; d < n; d++)
{
scanf("%d",&matrix[c][d]);
}}
for (c = 0; c < m; c++)
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}}
printf("Transpose of entered matrix :-\n");
for (c = 0; c < n; c++)
{
for (d = 0; d < m; d++)
{
printf("%d\t",transpose[c][d]);
printf("\n");
}
}
}
Output

c.) Product of Two matrices


Algorithm

1. Start
2. Declare variables and initialize necessary variables
3. Enter the element of matrices by row wise using loops
4. Check the number of rows and column of first and second matrices

5. If number of rows of first matrix is equal to the number of columns


of second matrix, go to step 6. Otherwise, print matrix multiplication
is not possible and go to step 3.
6. Multiply the matrices using nested loops.
7. Print the product in matrix form as console output.
8. Stop

Program
#include <stdio.h>
void main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each
other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c <
for (d = 0; d
for (k = 0;
sum = sum
}

m; c++) {
< q; d++) {
k < p; k++) {
+ first[c][k]*second[k][d];

multiply[c][d] = sum;
sum = 0;
}

}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
}
Output

Week 5 (Recursive Func. Programs)

Write a C program to find Factorial, GCD, fibonacci, towers of hanoi, sum of digits, base
conversions, reversal of numbers.

Factorial
Algorithm

Step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. Fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
Program
#include<stdio.h>
int factorial(int n);
Void main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
}
int factorial(int n)
{
if(n!=1 && n!=0)
{
return n*factorial(n-1);
}
else
{
return 1;
}
}

Output

Fibonacci
Algorithm
1. Start
2. Declare variables i, a,b , show
3. Initialize the variables, a=0, b=1, and show =0
4. Enter the number of terms of Fibonacci series to be printed
5. Print First two terms of series

Use loop for the following steps


->show=a+b
->a=b
->b=show
->increase value of i each time by 1
-> print the value of show
6. End

Program
#include<stdio.h>
void printFibonacci(int);
int main()
{
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
return 0;
}

void printFibonacci(int n)
{
static long int first=0,second=1,sum;
if(n>0)
{
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
Output

GCD
Algorithm

1.) Start
2.) Read n1,n2
3.) Using if (n2!=0) then return n2,n1%n2
4.) Else return n1
5.) End
Program
#include <stdio.h>
int hcf(int n1,int n2);
void main()
{
int n1,n2;
printf("Enter two positive integers: ");
scanf("%d%d",&n1,&n2);
printf("H.C.F of %d and %d = %d", n1, n2, hcf(n1,n2));
}
int hcf(int n1,int n2)
{
if (n2!=0)

return hcf(n2, n1%n2);


else
return n1;
}
Output

Towers of Hanoi
Algorithm
1. if n==1, move the single disk from A to C and stop.
2. Move the top n-1 disks from A to B, using C as auxiliary.
3. Move the remaining disk from A to C.
4. Move the n-1 disks from B to C, using A as auxiliary.

Program
#include<stdio.h>
void tow(int,char,char,char);
void main()
{
int n;
printf("enter the number of discs\n");
scanf("%d",&n);
tow(n,'a','c','b');
}
void tow(int n,char x,char y,char z)
{
if(n>0)
{
tow(n-1,x,z,y);
printf("the disc %d moves from %c -> %c\n",n,x,y);
tow(n-1,z,y,x);
}
}

Output

Sum of Digits
Algorithm

Input a Number

Initialize Sum to zero

While Number is not zero

Get Remainder by Number Mod 10

Add Remainder to Sum

Divide Number by 10

Print sum

Program
#include<stdio.h>
int add(int n);
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Sum = %d",add(n));
return 0;
}
int add(int n)
{
if(n!=0)
return n+add(n-1);
}

Output

Reverse of a Number
Algorithm

Input: num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num
Program
#include<stdio.h>
int reverse_function(int);
int main()
{
int num,reverse_number;
printf("\nEnter any number:");
scanf("%d",&num);
reverse_number=reverse_function(num);
printf("\nAfter reverse the no is :%d",reverse_number);
return 0;
}
int sum=0,rem;
reverse_function(int num){
if(num){
rem=num%10;
sum=sum*10+rem;
reverse_function(num/10);
}
else
return sum;
return sum;
}

Output

Base Conversions (digits to binary)


Algorithm
1. Divide the given number by 2.
2. Store the remainder.
3. Repeat the step 1 on the quotient.
4. When quotient is zero, print stored remainders in reverse order.

Program
#include<stdio.h>
int bin(int);
void main()
{
int n,c;
printf("enter a number");
scanf("%d",&n);
c=bin(n);
printf("the converted binary number is %d\n",c);
}
int bin(int n)
{
if(n==0)
{
return 0;
}
else
{
return (n%2+10*bin(n/2));
}
}

Output

Week - 6
Write a C program to implement all string operations(strlen(), strcpy(), , strcmp(), strcat(), strrev(), strstr(),
strchr()) without using standard string library functions.
1.) String length

Algorithm
1. Start
2. Declare an array and i
3. Read string
4. Using for loop condition count the length of string and print the
value of i
5. End

Program
#include<stdio.h>
void main()
{
char a[100],i;
printf("enter the string\n");
scanf("%s",a);
for(i=0;;)
{
if(a[i]=='\0')
{
break;
}
else
{
i++;
}
}
printf("%d\n",i);
printf("the string is \n %s",a);
}

Output

2.) String copy


Algorithm
1. Start
2. Read two arrays and i
3. Enter the string 1
4. Using while condition assign first string characters to second
string
5. Print string
6. End

Program
#include<stdio.h>
void main()
{
char a[100],b[100];
int i;
printf("enter the string\n");
scanf("%s",a);
i=0;
while(a[i]!='\0')
{
b[i]=a[i];
i++;
}
printf("the enterd string is");
printf("%s",b);
}

Output

3.) String comparison


Algorithm
1.Start
2.Read two character arrays
3.Enter the two strings
4.Using while condition compare string characters
5.Print the result
6. End
Program
#include<stdio.h>
int main() {
char str1[30], str2[30];
int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}

Output

4.) String concatenation


Algorithm
1.Start
2.Declare two character arrays and integer variables
3.Read two strings
4.Using nested for loops append the two strings
5.Print the appended string
6.End
Program
#include <stdio.h>
int main()
{
char s1[100], s2[100];
int i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0'; ++i);
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}

Output

5.) String reverse


Algorithm
1. Start
2. declare two character arrays and two integer variables
3. read the string 1
4. using for and if else conditions find the length of string
5. using the condition given below assign the reversed characters to
string 2
6. print the string
7. end

Program
#include<stdio.h>
void main()
{
char s1[100],s2[100];
int i,j;
printf("enter the string");
scanf("%s",s1);
for(i=0;;)
{
if(s1[i]=='\0')
{
break;
}
else
{
i++;
}
}
printf("the length of string is %d",i);
for(j=0;j<i;j++)
{
s2[j]=s1[i-1];
}
printf("the string is %s",s2);
}

Output

6.) Find sub string in entered string


Algorithm
1. start
2. declare two character arrays and required int variables
3. input the string
4. read the start and end position to print
5. using while loop copy the sub string characters into second
string
6. print the second string
7. end

Program
#include <stdio.h>
int main()
{
char string[1000], sub[1000];
int position, length, c = 0;
printf("Input a string\n");
gets(string);
printf("Enter the position and length of substring\n");
scanf("%d%d", &position, &length);
while (c < length) {
sub[c] = string[position+c-1];
c++;
}
sub[c] = '\0';
printf("Required substring is \"%s\"\n", sub);
return 0;
}

Output

Week 7
Write a C program to find the student grade by using structures.

Algorithm
1. Start
2. Using structures declare name, roll no. , marks, total and
grade variables
3. And then read name, roll no, marks(using for loop)
4. Calculate total of marks
5. Using if condition assign grade to student
6. Print details and grade scored
7. End

Program
#include<stdio.h>
void main()
{
int i;
struct student
{
char n[100];
char r[100];
int s1[6];
int t;
char g;
}s;
s.t=0;
printf("nter the name\n");
scanf("%s\n",&s.n);
printf("enter roll no.\n");
scanf("%s\n",&s.r);
printf("enter the marks of each subject\n");
for(i=0;i<6;i++)
{
scanf("%d\n",&s.s1[i]);
s.t=s.t+s.s1[i];
}
printf("the name is %s\n",s.n);
printf("the roll no. is %s\n",s.r);
printf("the entered marks are\n");
printf("the total scored by student is %d\n",s.t);
for(i=0;i<6;i++)
{
printf("%d\n",s.s1[i]);

}
if(s.t<=500)
{
printf("the grade is A");
}
else if(s.t>=400&&s.t<= 300)
{
printf("the grade is B");
}
else
{
printf("the grade id c");
}
}

Output
Enter the name: Arjun
Enter the roll no. : 15691a04n8
Enter the marks: 87 89 85 88 84 83
The name is: Arjun
The roll no is: 15691a04n8
Total marks: 516
Grade: A

WEEK - 8
Write a C program to perform the operations addition, subtraction, multiplication of
Complex numbers using structures.

Algorithm
1.Start
2.Using structures declare the variables
3.And then assign valyes to real and imaginary
variables
4.Usie do while loop Then print the operations you
want to do
5.And then use switch case to perform operations
6.Enter the choice for operation
7.And print the result
8.End
Program
#include<stdio.h>
void main()
{
struct arth
{
int a,b;
}c1,c2,c;
int ch;
printf("enter the real numbres");
scanf("%d%d",&c1.a,&c2.a);
printf("enter the imaginary valuesd");
scanf("%d%d",&c1.b,&c2.b);
do{
printf("the operations are");
printf("1. add\n 2. sub \n 3. mul\n");
printf("entre choices");
scanf("%d",&ch);
switch(ch)
{
case 1:c.a=c1.a+c2.a;
c.b=c1.b+c2.b;
printf("the sum is %d + i%d",c.a,c.b);
break;
case 2:c.a=c1.a-c2.a;
c.b=c1.b-c2.b;
printf("the sub is %d - i%d",c.a,c.b);
break;
case 3:c.a=c1.a*c2.a+c1.b*c2.b;

c.b=c1.b*c2.a+c1.a*c2.b;
printf("the sum is %d + i%d",c.a,c.b);
break;
default :printf("invalid");
}
}while(ch<4);
}

Output

WEEK -9
Write a C program to copy the file contents from one file to another file(pass file names as command line
arguments).

Algorithm
1. Start
2. Use required arguments in main function
3. Declare two file pointer variables
4. Using fopen function, open source file and destination file
present in CPU(if destination file not present in CPU it creates
new file but source file must be in CPU) and also give the modes
of usage of files
5. Use while loop and given condition below
6. At end of program, using fclose function, close the files
7. End

Program
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
FILE *f1,*f2;
char d;
f1=fopen("argv[1]","r");
f2=fopen("argv[2]","w+");
while((d=fgetc(f1))!=EOF)
{
fputc(d,f2);
}
fclose(f1);
fclose(f2);
}

Output
While entering the terminal for compilation
1. cc <filename>.c
2. ./a.out <source file name>.c <destination file name>.c
3. After this the terminal opens the source file execution and then continue..

Você também pode gostar