CP Formatted Manual 26-02-2018
CP Formatted Manual 26-02-2018
500100.
Website: www.mrec.ac.in
OBJECTIVES:
LIST OF EXPERIMENTS
1.
a. Practice various Internal and External DOS Commands.
b. Implement various programs logics using algorithms and flowcharts.
c. Write sample examples of C programs to implement basic operations.
2.
a. Write a C program to find smallest and largest of given three numbers.
b. Write a C program to find the roots of a quadratic equation.
3
a. Write a C program to find the sum of individual digits of a positive integer.
b. A Fibonacci sequence is defined as follows: the first and second terms in the sequence
are 0 and 1. Subsequent terms are found by adding the preceding two terms in the
sequence.
c. Write a C program to generate the first n terms of the sequence.
4.
a. Write a C program to find whether the given number is palindrome, perfect, Armstrong
or strong.
b. Write a C program to generate all the prime numbers between n1 and n2, where n1 and
n2 are values supplied by the user.
5.
Write C programs that use both recursive and non-recursive functions
a. To find the factorial of a given integer.
b. To find the GCD (greatest common divisor) of two given integers.
6.
a. Write a C program to find both the largest and smallest number in a list of integers.
b. Write a C program that uses functions to perform the following:
i. Addition of Two Matrices
ii. Multiplication of Two Matrices
7.
a. Write a C program that uses functions to perform the following operations:
i. To insert a sub-string into given main string from a given position.
ii. To delete n characters from a given position in a given string.
b. Write a C program to determine if the given string is a palindrome or not
c. Write a C program to find substring in a given string.
d. Write a C program to count the lines, words and characters in a given text.
8.
a. Write a C program to implement functions arguments with different returns values.
b. Write a C program to implement call by value and call by reference using functions.
9.
a. Write a C program to find grades of a student’s using structures.
b. Write a C program to implement nested structures.
10.
a. Write a C program which copies one file to another.
b. Write a C program to command line arguments.
11.
a. Write a C program that uses non-recursive function to search for a Key value in a given
list of integers using linear search.
b. Write a C program that uses recursive and non -function to search for a Key value in a
given sorted list of integers using Binary search.
12.
a. Write a C program that implements the Selection sort method to sort a given array of
integers in ascending order.
b. Write a C program that implements the Bubble sort method to sort a given list of names
in ascending order.
INDEX
a. Write a C program to find both the largest and smallest number in a list 29
6 of integers.
b. Write a C program that uses functions to perform the following:
i. Addition of Two Matrices 30
ii. Multiplication of Two Matrices
1.
a. Practice various DOS internal and external commands.
Fundamentals of DOS
Internal Commands
Syntax:
CLS
Running the CLS command at the command prompt would clear your
screen of all previous text and only return the prompt.
COPY CON:
An internal command for creating a quick batch file in DOS or
Windows. For example, to create the WRITE batch file, type copy
con write.bat
After pressing Enter, you'll get a blank line. Type your text
and press Enter to end the line. When done, press F6 (ctrl-Z),
then press Enter.
Copy Con works a line at a time. You cannot go back and change
lines, but you can use backspace to delete characters on the
same line.
TYPE: Displays the contents of a text file.
Syntax:
TYPE [drive:][path]filename
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
Syntax:
CHDIR [drive:][path]
CHDIR[..]
CD [dri]
External Commands
SORT: Sorts the input and displays the output to the screen.
Syntax:
Sorts input and writes results to the screen, a file, or another
device
SORT [/R] [/+n] [[drive1:][path1]filename1] [>
[drive2:][path2]filename2]
[command |] SORT [/R] [/+n] [> [drive2:][path2]filename2]
FIND: Allows you to search for text within a file. Although MS-
DOS itself is not case sensitive, when typing in the string that
you are looking for with the find command, it is case sensitive.
Syntax:
FIND [/V] [/C] [/N] [/I] "string" [[drive:][path]filename[ ...]]
Algorithm:
Step 1: start
Step 2: Go to stationary shop
Flowchart:
Program :
#include<stdio.h>
main()
{
int a, b, c;
clrscr();
printf("Enter two numbers to add\n");
scanf ("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
getch();
}
Output:
Description:
Perform all the basic arithmetic operations i.e. addition,
subtraction, multiplication and division operations using switch
case.
Program:
#include<stdio.h>
#include<conio.h>
main ()
{
MREC (A) Page 11
DEPARTMENT OF CSE COMPUTER PROGRAMMING LAB
int a,b;
char choice;
clrscr();
printf("Press'+'for addition\n'-' for subtraction\n'/'for
divide\n");
printf("'*'for multiply\n'%' for remainder value\n");
printf("Enter your choice:\n");
scanf("%c", &choice);
printf("Enter two numbers:\n");
scanf("%d %d", &a, &b);
switch(choice)
{
case '+':
printf ("addition=%d\n", a + b);
break;
case '-':
printf("sub=%d\n",a-b);
break;
case '/':
printf("division=%d\n",a/b);
break;
case '*':
printf("multiply=%d\n",a*b);
break;
case ‘%’:
printf(“remainder=%d\n”,a%b);
break;
default :
printf("invalid choice\n");
break;
}
getch();
}
Output:
Description:
Compares the given three numbers and finds the smallest and
largest number among the given three numbers.
Algorithm:
Step 1: start
Step 2: read a,b,c
Step 3: If ((a >b)&&(a>c))
largest = a
Else if(b>c)
largest = b
else
largest=c
Step 4: if( (a < b) && (b < c) )
smallest= a
else if(b < c)
smallest=b
else
smallest=c
Step 5: Stop.
Flow chart:
Program:
#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n\n\t ENTER THREE NUMBERS a,b,c…(separated by commas):
“);
scanf(“%d,%d,%d”, &a, &b, &c);
printf(“\n\n\t THE BIGGEST NUMBER IS…: “);
if( (a > b) && (a > c) )
printf(“%d”, a);
else if(b > c)
printf(“%d”, b);
else
printf(“%d”, c);
printf(“\n\n\t THE SMALLEST NUMBER IS…: “);
if( (a < b) && (b < c) )
printf(“%d”, a);
else if(b < c)
printf(“%d”, b);
else
printf(“%d”,c);
getch();
}
Output:
Description:
Determines the discriminant of the given quadratic equation and
depending on the value of the discriminant, it finds the roots.
FLOWCHART:
Program:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float a,b,c,d,r1,r2,real,imag;
clrscr();
printf("enter a,b,c values for quadratic equation");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf(“Roots are real and unequal\n”);
printf("Roots are:%f\t\t%f",r1,r2);
}
else if(d==0)
{
r1=r2=-b/(2*a);
printf(“Roots are real and equal\n”);
printf("Roots are:%f\t\t%f",r1,r2);
}
else
{
real=(-b/(2*a));
imag=sqrt(-d)/(2*a);
printf(“Roots are imaginary\n”);
printf("Roots are:%f+%fi\t%f-%fi",real,imag,real,imag);
}
getch();
}
Output:
Description:
Program:
#include <stdio.h>
void main ( )
{
int num, sum = 0, rem=0;
clrscr( );
printf("Enter a number to calculate sum of its individual
digits \n");
scanf("%d",&num);
while(num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
}
getch();
}
Output :
Description:
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,b=1,c=0,i=0;
clrscr();
printf("Enter the no of elements in the fibonacci series \n");
scanf("%d",&n);
printf("The fibonacci series is : \n");
printf("%d \t %d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf("\t %d",c);
a=b;
b=c;
}
getch();
}
Output :
Description:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,r,s,t,temp,i;
clrscr();
do
{
printf("\nEnter the choice!\n");
printf("\nEnter 1 for checking Armstrong number!\nEnter 2 for
checking Palindrome number!\nEnter 3 for checking Perfect
number!\nEnter 0 to exit!\n");
printf("\nEnter your choice:");
scanf("%d", &c);
if(c==0)
{
printf("\nClosing the program!!");
getch();
exit(0);
}
printf("\nEnter the number for checking:");
scanf("%d", &n);
switch(c)
{
case 1:
{
t=n;
s=0;
while(t!=0)
{
r=t%10;
t=t/10;
s=s+r*r*r;
}
if(s==n)
printf("\nEntered number is an Armstrong number!");
else
printf("\nEntered number is not an Armstrong number!");
break;
}
case 2:
{
s=0;
temp=n;
while(n)
{
r=n%10;
n=n/10;
s=s*10+r;
}
if(temp==s)
printf("%d is a Palindrome",temp);
else
printf("%d is not a Palindrome",temp);
break;
}
case 3:
{
s=0;
for(i=1;i<=n/2;i++)
{
if(n%i==0)
{
s=s+i;
}
}
if(s==n)
printf("\nThe number is perfect!!");
else
printf("\nThe number is not perfect!!");
break;
}
default:
printf("\nWrong choice entered!!");
break;
}
}while(n!=0);
getch();
}
Output:
Description:
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,count=0,i,j, primecount=0;
clrscr();
printf("Enter a range to print prime numbers in between that
range \n");
scanf("%d%d",&a,&b);
for(i=a;i<=b;i++)
{
count=0;
for(j=2;j< i ;j++)
{
if(i%j == 0)
{
count++;
break;
}
}
if(count == 0)
{
printf("%d \t",i);
primecount++;
}
}
printf("\n The total prime numbers in the given range %d and
%d is %d",a,b,primecount);
getch();
}
Output:
Description:
Description:
Program:
#include<stdio.h>
#include<conio.h>
long int fact (long int);
void main()
{
long int n,f;
clrscr();
printf(“Enter a number to find the factorial”);
scanf(“%d”, &n);
f=fact(n);
printf(“\n The factorial of given number %d is %d”,n,f);
getch();
}
long int fact(long int n)
{
long int f=1;
if(n==0||n==1)
return f;
else
f=n*fact(n-1);
return f;
}
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rem;
clrscr();
printf("Enter two numbers to find their GCD : \n");
scanf("%d%d",&m,&n);
while(n%m !=0)
{
rem=n%m;
n=m;
m=rem;
}
printf("The gcd of given numbers is %d", m);
getch();
}
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
int find_gcd(int,int);
void main()
{
int n1,n2,gcd;
printf(“\n Enter two numbers to find their gcd”);
scanf(“%d%d,“,n1,n2);
gcd=find_gcd(n1,n2);
printf("The gcd of given numbers is %d",gcd);
getch();
}
int find_gcd (int x,int y)
{
while(x!=y)
{
if(x>y)
return find_gcd(x-y,y);
else
return find_gcd(x,y-x);
}
return x;
}
Output:
Description:
Compares the given list of integers and finds both the largest
and smallest number from that given list.
Program:
#include <stdio.h>
void main()
{
int a[10],i,max,min;
clrscr();
printf("\n Enter the elements in array ");
for (i=0;i<10;i++)
scanf("%d",&a[i]);
max = a[0];
min=a[0];
for(i=1;i<10;i++)
{
if (max < a[i])
max = a[i];
if (min>a[i])
min=a[i];
}
printf("\n The largest element is %d",max);
printf("\n The least element is %d",min);
getch();
}
Output:
6.
b. Write a C program that performs using functions:
i. Addition of Two Matrices
ii. Multiplication of Two Matrices
Description:
Program:
#include<stdio.h>
#include<conio.h>
void read_arr(int a[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("Enter Element %d %d : ",i,j);
scanf("%d",&a[i][j]);
}
}
}
void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int
row,int col)
{
int i,j;
printf("Sum of entered matrices:\n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j] = (m1[i][j] + m2[i][j]);
}
}
}
void mul_arr(int m1[10][10],int m2[10][10],int m3[10][10],int
row,int col)
{
int i,j,k;
printf("Multiplication of entered matrices:\n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j]=0;
for (k=1;k<=row;k++)
{
m3[i][j] = m3[i][j] + (m1[i][k] * m2[k][j]);
}
}
}
}
Output:
7.a)
i.Write a C program to insert a sub-string in to given main
string from a given position using function.
Description:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[40],s2[40],s[40];
int i,j,c,ind=0;
clrscr();
printf("Enter org string and substring");
scanf("%s %s",s1,s2);
printf("\n Enter position where to be inserted");
scanf("%d",&ind);
for(i=0,c=0;s1[i]!='\0';i++,c++)
{
if(i==ind)
for(j=0;s2[j]!='\0';j++,c++)
s[c]=s2[j];
s[c]=s1[i];
}
s[c]='\0';
printf("String now is :");
printf("\n %s \n",s);
getch();
}
Output:
7.a)
ii. Write a C program to delete n Characters from a given
position in a given string using function.
Description:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void del(char[],int,int);
void main()
{
char str[50];
int n,pos;
clrscr();
puts("Enter the string");
gets(str);
printf("Enter the position");
scanf("%d",&pos);
puts("Enter no of characters to be deleted");
scanf("%d",&n);
del(str,pos,n);
getch();
}
void del(char s[],int a,int b)
{
int i;
for(i=a-1;s[i]!='\0';i++)
{
s[i]=s[a+b-1];
a++;
}
Puts(“The resultant string after deleting the specified
characters is :”);
puts(s);
}
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],strrev[50];
int i,n=0,F=0;
clrscr();
i=0;
while(n>0)
{
strrev[i]=str[n-1];
n--,i++;
}
strrev[i]='\0';
/*to check the two string*/;
for(i=0;str[i]!='\0';i++)
{
if(str[i]!=strrev[i])
{
F=1;
break;
}
}
if(F==0)
printf("Given string %s is a palindrome",str);
else
printf("Not palindrome");
getch();
}
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[90],str2[10],r[40];
int i,j;
clrscr();
puts("enter string");
gets(str1);
puts("enter sec string");
gets(str2);
for(i=0;str1[i]!='\0';i++)
{
for(j=0;str2[j]!='\0';j++)
{
if(str1[i]==str2[j])
r[j]=str2[j];
} }
r[i]='\0';
if(strcmp(r,str2)==NULL)
printf("%s is substr found",r);
else
printf("%s is not sub",r);
getch();
}
Output:
Description:
Program:
#include <stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char line[100], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
clrscr();
printf("ENTER YOUR TEXT, WHEN COMPLETED, PRESS
'RETURN'.\n\n\n");
while( end == 0)
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void sum(); // function Declaration
void main()
{
sum();
getch();
}
void sum()
MREC (A) Page 40
DEPARTMENT OF CSE COMPUTER PROGRAMMING LAB
{
int val,a,b;
val = a+b ;
Output:
Output:
Program:
#include<stdio.h>
#include<conio.h>
void sum(int,int); // function Declaration
void main()
{
int a,b;
printf("\nEnter the Two numbers to be added : ");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
void sum(int a,int b)
{
int val;
val=a+b;
printf("Sum of two numbers= %d",val);
Output:
Program:
#include<stdio.h>
#include<conio.h>
int sum(int,int); // function Declaration
void main()
{
int a,b,val;
printf("\nEnter the Two numbers to be added : ");
scanf("%d%d",&a,&b);
val=sum(a,b);
printf("The sum of two numbers: %d",val);
getch();
}
int sum(int a,int b)
{
return(a+b);
}
Output:
Description:
Function call by value is the default way of calling a function
in C programming. Before we discuss function call by value, lets
understand the terminologies that we will use while explaining
this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function
declarations.
Program:
#include <stdio.h>
#include<conio.h>
/* function declaration */
Output:
Description:
The call by reference method of passing arguments to a function
copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual
argument used in the call. It means the changes made to the
parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to
the functions just like any other value. So accordingly you need
to declare the function parameters as pointer types as in the
Program:
#include <stdio.h>
#include<conio.h>
/* function declaration */
void swap(int *x, int *y);
void main () {
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
getch();
}
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
struct stud
{
char nam[20];
int obtain_mark;
int per;
char grad[5];
};
struct stud s[5];
int i;
void main()
{
clrscr();
for(i=1; i<=5; i++)
{
printf("Enter %d student name : ",i);
scanf("%s",&s[i].nam);
printf("Enter %d student obtained marks = ",i);
scanf("%d",&s[i].obtain_mark);
fflush(stdin);
}
for(i=1; i<=5; i++)
s[i].per=s[i].obtain_mark/5;
for(i=1; i<=5; i++)
{
if(s[i].per>=80)
strcpy(s[i].grad,"A");
else if(s[i].per>=60)
strcpy(s[i].grad,"B");
else if(s[i].per>=50)
strcpy(s[i].grad,"C");
else if(s[i].per>=40)
strcpy(s[i].grad,"D");
else
strcpy(s[i].grad,"F");
}
Output:
Description:
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data;
void main()
{
struct student_detail stu_data = {1, "Teja", 90.5, 1234,
"Malla Reddy Engineering
College"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
Output:
Description:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *f1,*f2;
char s,c;
clrscr();
f1=fopen("sham.txt","w");
puts("Enter data to sham file");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("sham.txt","r");
f2=fopen("a.txt","w");
puts("data copied");
while((s=getc(f1))!=EOF)
{
putc(s,f2);
fprintf(stdout,"%c",s);
}
fclose(f1);
fclose(f2);
getch();
}
Output:
Description:
It is possible to pass some values from the command line to your
C programs when they are executed. These values are called
command line arguments and many times they are important for
your program especially when you want to control your program
from outside instead of hard coding those values inside the
code.
Program:
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
Output:
flag = 1;
break;
}
}
if(flag == 1)
printf("The key elements is found at location %d", i + 1);
else
printf("The key element is not found in the array");
getch();
}
Output:
Program:
#include<stdio.h>
#include<stdlib.h>
int binsearch(int[], int, int, int);
int main()
{
int num, i, key, position;
int low, high, list[10];
printf("\nEnter the total number of elements");
scanf("%d", &num);
printf("\nEnter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("\nEnter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("\nNumber present at %d", (position + 1));
}
else
printf("\n The number is not present in the list");
getch();
}
// Binary Search function
int binsearch(int a[], int x, int low, int high)
{
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid])
{
return (mid);
}
else if (x < a[mid])
{
binsearch(a, x, low, mid - 1);
}
else
{
binsearch(a, x, mid + 1, high);
}
}
Output:
{
min = i;
for(j = i + 1; j < n; j++)
{
if(a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("The sorted array is\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output: