Palak-CO Lab File
Palak-CO Lab File
Practical File
Submitted By -
Palak Yadav
2K18/A4/472
INDEX
Page Done Checked Teacher's
S.No Topic
No. On On Sign.
EXPERIMENT 1
AIM: Write a program to find the maximum of 3 numbers.
THEORY: Largest of three numbers can be calculated by using the comparison operators. A
number can be compared with the other two numbers, respectively and largest number can be
determined.
ALGORITHM:
Step 1: Start
Step 2: Input the three numbers a,b,c
Step 3: if a>=b go to step 4, else go to step 5
Step 4: if c>=a, max=c, else max=a and go to step 6
Step 5: if c>=a, max=c, else max=b
Step 6: Print max as maximum number
Step 7: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
scanf("\n\tEnter the three numbers: %d %d %d",&a,&b,&c);
if(a>=b)
{
if(c>=a)
max=c;
else
max=a;
}
else
{
if(c>=b)
max=c;
else
max=b;
}
printf("\n\tThe maximum of all three = %d",max);
getch();
}
OUTPUT:
RESULT: I have computed the maximum of three numbers and learned the use of comparison
operators.
CONCLUSION: I have successfully learned to use the comparison and assignment operators
and how to find the largest of three numbers.
EXPERIMENT 2
AIM: Write a program to find the value of a number raised to some power, using library
function.
THEORY: A number a, raised to the power b, means a multiplied to itself b times. It can be
done using a loop (for, while, do-while) or using the pow(a,b) library function.
ALGORITHM:
Step 1: Start
Step 2: Input the base and exponent
Step 3: Use the pow(a,b) function and Print the value
Step 4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
printf("\n\tEnter the base and the exponent: ");
scanf("%d%d",&a,&b);
c=pow(a,b);
printf("\n\tThe value of %d raised to the power %d = %d",a,b,c);
getch();
}
OUTPUT:
RESULT: I have computed the value of a number raised to some power using library function.
CONCLUSION: I have successfully learned to use the library functions and the method to
determine the base raised to exponent terms.
EXPERIMENT 3
AIM: Write a program to find the sum of 2 square matrices.
THEORY: A matrix is a 2D array and each term can be accessed using a for loop. Sum of two
square matrices is equivalent to a matrix of same order and each term as a sum of respective
terms of both matrices.
ALGORITHM:
Step 1: Start
Step 2: Input the order of the matrices and the matrices respectively.
Step 3: Create a new matrix of same order
Step 4: Assign each term of the new matrix as the sum of respective terms of two matrices
Step 5: Print the new matrix as the sum of two matrices.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10,i,j;
int A[10][10],B[10][10],C[10][10];
clrscr();
printf("\nEnter the order of the matrices: ");
scanf("%d",&n);
printf("Enter matrice A (row wise): \n");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter matrice B (row wise): \n");
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&B[i][j]);
}
}
OUTPUT:
CONCLUSION: I have successfully learned to take input and access the 2D arrays and to
operate upon them.
EXPERIMENT 4
AIM: Write a program to print the following pattern, with user-input number of rows
THEORY: Such patterns can be printed using nested loops. Nested loop means running a loop
within a loop, or more.
ALGORITHM:
Step 1: Start
Step 2: Input the number of rows.
Step 3: Start the nested for loops
for(i=1;i<=n;++i)
{
printf("\t");
for(j=1;j<=n-i+1;++j)
printf("* ");
for(j=1;j<=2*(i-1);++j)
printf(" ");
for(j=1;j<=n-i+1;++j)
printf("* ");
printf("\n");
}
Step 4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
clrscr();
printf("\n\tEnter the number of rows: ");
scanf("%d",&n);
printf("\n\t");
for(i=1;i<=n;++i)
{
printf("\t");
for(j=1;j<=n-i+1;++j)
printf("* ");
for(j=1;j<=2*(i-1);++j)
printf(" ");
for(j=1;j<=n-i+1;++j)
printf("* ");
printf("\n");
}
getch();
}
OUTPUT:
RESULT: I have printed the required pattern, taking number of rows as input from the user.
CONCLUSION: I have successfully learned to use the nested loops and print patterns.
EXPERIMENT 5
AIM: Write a program to check whether a number is prime or not.
THEORY: A number is prime if it is not divisible by any other number other than 1 and itself.
Here, we learn to use the modulus operator and the loop. If the number is not divisible by any
number until its half, then it's a prime number.
ALGORITHM:
Step 1: Start
Step 2: Input the number and create a temporary variable initialized.
Step 3: Start a loop till half of the number and check if it gets divided by any.
If yes, change the value of temporary variable and go to step 4, else continue.
Step 4: Break out of the loop, check the value of temporary variable and print that the number
isn't prime and go to step 6
Step 5: Compare the value of temporary variable and print that the number is prime.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,temp=0;
clrscr();
printf("\tEnter the number: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
temp=1;
break;
}
}
if(temp==1)
printf("\tThe number, %d, is not prime.",n);
else
printf("\tThe number, %d, is prime.",n);
getch();
}
OUTPUT:
CONCLUSION: I have successfully learned to determine if a number is prime or not and the
application of loop and modulus operator.
EXPERIMENT6
AIM: Write a program to exponential function.
THEORY: A number a raised to power b means a multiplied to itself b times. It is done through
loop. A loop is run b times wherein product is calculated by multiplying a by itself.
ALGORITHM:
Step 1: Start
Step 2: Input the base,b and the exponent,e.
Step 3: Initialize a variable p=1
Step 4: Start a loop for e times
p=p*b
Step 5: Print the value of p as b raised to power e.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int b,e,i;
long p=1;
clrscr();
printf("\n\n\tEnter the base and exponent: ");
scanf("%d %d",&b,&e);
for(i=1;i<=e;++i)
{
p*=b;
}
printf("\tThe value of %d raised to power %d = %d.",b,e,p);
getch();
}
OUTPUT:
CONCLUSION: I have successfully learned to find the value of a term raised to some power.
EXPERIMENT 7
AIM: Write a program to print the pattern of a filled circle taking radius from the user.
THEORY: Such patterns can be printed using nested loops. Nested loop means running a loop
within a loop, or more.
ALGORITHM:
Step 1: Start
Step 2: Input the radius, r.
Step 3: Start the nested for loops
for(i=0;i<=2r;++i)
{
for(j=0;j<=2r;++j)
{
dis=(i-r)2 + (j-r)2
if(dis<r2)
printf("* ");
else
printf(" ");
}
printf("\n");
}
Step 4: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
float dist;
int radius,i,j;
clrscr( );
RESULT: I have printed the required pattern, taking radius as input from the user.
CONCLUSION: I have successfully learned to use the nested loops and print patterns.
EXPERIMENT 8
AIM: Write a program to find prime numbers till 'n' using Sieve of Eratosthenes.
THEORY: A number is prime if it is not divisible by any other number other than 1 and itself.
Here, we learn to use the modulus operator and the loop. If the number is not divisible by any
number until its half, then it's a prime number.
ALGORITHM:
1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
2. Initially, let p equal 2, the first prime number.
3. Starting from p2, count up in increments of p and mark each of these numbers greater
than or equal to p2 itself in the list. These numbers will be p(p+1), p(p+2), p(p+3), etc..
4. Find the first number greater than p in the list that is not marked. If there was no such
number, stop. Otherwise, let p now equal this number (which is the next prime), and
repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked are prime.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void SieveOfEratosthenes(int n)
{
int prime[n+1];
for(int i=0;i<=n;i++)
prime[i]=1;
for (int p=2; p*p<=n; p++)
{
if (prime[p] == 1)
{
for (int i=p*p; i<=n; i += p)
prime[i] = 0;
}
}
for (int p=2; p<=n; p++)
if (prime[p]==1)
printf("%d ",p);
}
void main()
{
int n;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
printf("\nFollowing are the prime numbers smaller than or equal to %d: \n",n);
SieveOfEratosthenes(n);
getch();
}
OUTPUT:
RESULT: I have determined all the prime numbers less than or equal to the given number.
CONCLUSION: I have successfully learned to determine the required prime numbers using the
Sieve of Eratosthenes method.
EXPERIMENT 9
AIM: Write a program for the fast exponential function.
THEORY: In competitions, for calculating large powers of a number we are given a modulus
value(a large prime number) because as the values of xy is being calculated it can get very large
so instead we have to calculate (xy % modulus value.) The answer is we can try exponentiation by
squaring which is a fast method for calculating exponentiation of a number.
ALGORITHM:
Pseudocode :-
long exponentiation(long base, long exp)
if (exp == 0) return 1;
if (exp == 1) return base % N;
long t = exponentiation(base, exp / 2);
t = (t * t) % N;
if (exp % 2 == 0) return t;
else return ((base % N) * t) % N;
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define N 1000000007 // prime modulo value
if (exp % 2 == 0)
return t;
else
return ((base % N) * t) % N;
}
int main()
{
long int base = 5;
long int exp = 100000;
clrscr();
printf("\nEnter the base and the exponent: ");
scanf("%d %d", &base, &exp);
long int modulo = exponentiation(base, exp);
printf("\n%d\n",modulo);
getch();
return 0;
}
OUTPUT:
CONCLUSION: I have successfully learned to find the value of a term raised to some power
using a fast exponentiation method.
EXPERIMENT 10
AIM: Write a program to reverse an integer.
THEORY: Reverse of a number means changing the digits to their opposite tens places. It can
be done using modulus operator in a loop.
ALGORITHM:
Step 1: Start
Step 2: Input the number, n and initialize s=0,r.
Step 3: if n>0, go to step 4, else go to step 5
Step 4: r = n % 10
s = s * 10 + r
n = n / 10
Go to step 3
Step 5: Print the reversed number, s.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
int reversDigits(intnum)
{
int rev_num = 0;
while (num> 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
int main()
{
int num;
clrscr();
scanf("%d",&num);
printf("Reverse of no. is %d", reversDigits(num));
return 0;
}
OUTPUT:
RESULT: I have reversed the given number.
THEORY: Depending on the degree, the coefficients of the polynomial expression are taken
from the user in an array and for a value of 'x', it its value is computed.
ALGORITHM:
Step 1: Start
Step 2: Input the degree
Step 3: if n>0, go to step 4, else go to step 5
Step 4: r = n % 10
s = s * 10 + r
n = n / 10
Go to step 3
Step 5: Print the reversed number, s.
Step 6: Stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
float x, a[10], y1;
int deg, i;
printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1);
return 0;
}
float poly(float a[ ], int deg, float x)
{
float p;
int i;
p = a[deg];
return p;
}
OUTPUT:
ALGORITHM:
Step 1: Start
Step 2: Input the order of the matrices and the matrices respectively.
Step 3: Create a new matrix of required order
Step 4: For i from 1 to m:
For j from 1 to q:
Let sum=0
For k from 1 to n:
Set sum sum + Aik x Bkj
Set Cij sum
Step 5: Print the new matrix as the product of two matrices.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int A[10][10],B[10][10],C[10][10],m,n,p,q,i,j,k;
clrscr();
printf("\tEnter the size of 1st matrix: ");
scanf("%d %d",&m,&n);
printf("\tEnter the size of 2nd matrix: ");
scanf("%d %d",&p,&q);
if(p!=n)
{
printf("\n\tMatrices can't be multiplied.\n\tExit...");
getch();
exit(0);
}
printf("\tEnter elements of matrix A: \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
}
printf("\n\tEnter elements of matrix B: \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&B[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
C[i][j]=0;
for(k=0;k<n;k++)
C[i][j]+=A[i][k]*B[k][j];
}
}
printf("\n\tMultiplication of A & B is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf(" %d ",C[i][j]);
}
getch();
}
OUTPUT:
RESULT: I have computed the product of two matrices.
CONCLUSION: I have successfully learned to take input and access the 2D arrays and to
operate upon them.
EXPERIMENT 13
AIM: Write a program to find the factorial of a number.
THEORY: This program finds the factorial of a number, using loops. A factorial is product of all
the numbers from 1 to the user specified number.
ALGORITHM:
Step 1: Start
Step 2: Take input from user and store it in a variable named n.
Step 3: Set p 1
Step 4: For i from 1 to n:
Set p pxi
Step 5: Print p as the factorial of number as an output.
Step 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
long p=1;
int n,i;
clrscr();
printf("\tEnter the number: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
p*=i;
printf("\tFactorial of %d = %d .",n,p);
getch();
}
OUTPUT:
CONCLUSION: I have successfully learned to take input and access the 2D arrays and to
operate upon them.
EXPERIMENT 14
AIM:
Write a program to string length.
THEORY:
This program takes a string as input and finds its length without using the built-in function,
called strlen function.
ALGORITHM:
Step 1: Take a string as input and store it in the array and initialise the value of len variable to 0.
Step 2: Using for loop to count the number of characters in the string and store in a variable .
Step 3: Increase the value of len by 1 after every iteration.
Step 4: Print the variable(len) as output, which is the length of the string .
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
int i,len=0;
printf("enter a string :");
gets(str);
for(i=0;str[i]!='\0';i++)
len++;
printf("\n\nlength of given string %s = %d\n",str,len);
return 0;
}
OUTPUT:
RESULT:
The length of any string can also be determined without using strlen function .It can be
determined by using for loop and simple logic.
CONCLUSION:
I have successfully learned to find the length of a string.
EXPERIMENT 15
AIM:
Write a program to string concatenation.
THEORY:
Concatenation of two strings means to add two strings and make it a single unit or term . In C
Programming, we can easily concatenate two strings using strcat() function, and it is defined in
string.h header file.
For ex.: 1st string : super
2nd string : computer
Concatenation String :- supercomputer
ALGORITHM:
Step 1 : Take two strings from user and stored it into two character type variable s1,s2 with size
of 100 characters.
Step 2: Now use strcat() function to concatenate both the strings like this:-
strcat(string1, string2);
Step 3: Now print output, which is the concatenated string of two given strings.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100] , s2[100];
printf("enter first string :\n");
gets(s1);
printf("\nenter second string :\n");
gets(s2);
strcat(s1, s2);
printf("\nstring obtained on concatenation : %s\n",s1);
return 0;
}
OUTPUT:
RESULT:
String concatenation can easily be done by strcat() function.
CONCLUSION:
I have successfully learned string concatenation.
EXPERIMENT 16
AIM:
Write a program to string reverse.
THEORY:
This program takes a string from user and reverses that string using for loop.
For ex : Input : hello
output :olleh
ALGORITHM:
Step 1:Take two strings from user and stored it into character type variable s1 with size of 100
characters.
Step 2: Find out the length of the string and stored in some variable.
Step 3: Now with the help of for loop, print the string in reverse order by using following
technique :
for(len -1; i>=0; i--) //this syntax can print any number or string in reverse order
Step 4: Now print output, which is the reverse of the given string.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100],s2[100];
int i,k=0,len;
printf("enter a string :");
scanf("%[^\n]s",s1);
len= strlen(s1);
printf("\n\nstring in reverse order :\n");
for(i=len-1,k=0; i >=0; i--,k++)
{
s2[k]=s1[i];
}
printf("%s",s2);
return 0;
}
OUTPUT:
RESULT:
A string can be reversed by using simple logic.
CONCLUSION:
I have successfully learned to reverse a string.
EXPERIMENT 17
AIM:
Write a program to string compare.
THEORY:
This program accepts two strings and compares them with each other, if they are equal then
display message that:* strings are equal*,
Otherwise, not equal.
We can use strcmp() function to compare two strings.
ALGORITHM:
Step 1: Take two strings from user and stored it into two character type variable s1, s2having
each size of 100 characters
Step 2: Now use if condition statement to compare two strings with the help of strcmp()
function, if strings are equal, then print output: *strings are equal*, Otherwise, not equal.
Step 3: Now print output.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100], s2[100];
printf("enter first string : \n");
scanf("%s",s1);
printf("\nenter second string : \n");
scanf("%s",s2);
if(strcmp(s1, s2) ==0)
printf("\nthe strings are equal\n");
else
printf("\nstrings are not equal\n");
return 0;
}
OUTPUT:
RESULT:
String comparison can easily be done by strcmp() function.
CONCLUSION:
I have successfully learned to compare string.
EXPERIMENT 18
AIM:
Write a program to Fibonacci series.
THEORY:
In mathematics, the Fibonacci numbers are the integers in the following sequence and are
characterized by the fact that every proceeding number is the sum of the previous two numbers.
0,1,1,2,3,5,8,13...............
ALGORITHM:
Step 1: Start.
Step 2: Declare variables i, a, b, show.
Step 3: Initialize the variables, a=0, b=1, and show=0.
Step 4: Enter the number of terms of Fibonacci series to be printed.
Step 5: Print first two terms of series.
Step 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
Step 7: End.
PROGRAM:
#include<stdio.h>
int main()
{
int n, first=0, second=1, next, c;
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
printf("%d\n", next);
}
return 0;
}
OUTPUT:
RESULT:
Fibonacci series can be easily printed in C programming using simple logic that every proceeding
term is the sum of the previous two numbers.
CONCLUSION:
I have successfully learned to find the Fibonacci series.