C++ Assigment
C++ Assigment
Lab File
Introduction to C
Submitted By Submitted to
1. Syllabus
2. Hardware/Software Requirements
4. Programs
CSE-103F FCPC Lab
In addition to the experiments listed below, 5 to 10 more lab-exercises may be given by the teacher concerned to the
students for practice depending upon the progress of the students in programming capabilities. It is suggested (not
mandatory) that the institute concerned may allot more numb er of teachers in each of th e First Year Lab Classes
of this FCPC-Lab Course so that the teacher can give more and more emphasis on “personal eye-to-eye attention”
in the Lab to each and every student so that the students can truly learn How to write correct and efficient code
independently with their self-confidence. Bu ilding this confidence in the students is more important to the
eachers than the number-statistics i.e “the Total Number of experiments” finished/done by the students in this
FCPC Lab.
The Lab Teacher/Technician will introduce (show) the students in the lab the d ifferent Hardware organization of a
computer, Input/Output devices, Input/Output ports and connectors etc. on the very first day before the start of the
following experiments.
P-IV/III PROCESSOR
HDD 40GB
SOFTWARE REQUIRED
Window 98/2000/ME/XP
Turbo C, C++
Practical’s to be conducted in the lab
Objective :
To find the largest number among three numbers in a list of integers.
Description:
This program contains n number of elements, in these elements we can find the largest numbers and display
number.
Algorithm:
1. float a,b,c;
3. read a,b,c;
4. if((a>b)&&(a>c))
6. else
7. if((b>a)&&(b>c))
9. else
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("a is greater");
printf("b is geater");
else
printf("c is geater");
getch();
Output:-
45
62
12
b is greater
2. Write a program to find the largest of ten numbers (for-statement)
Objective : 2
To find both the largest and smallest number in a list of integers
Description:
This program contains n number of elements, in these elements we can find the largest and smallest numbers and
display these two numbers
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
Read a[i]
Increment i
Goto step 4
Step 5: min=a[0], max=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
If a[i]<min
Assign min=a[i]
Increment i goto Step 7
Step 8: print min,max
Step 9: sto
Program:
#include<stdio.h>
void main()
{
int a[10],i,n,min,max;
clrscr();
printf("enter the array size:");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++) // read the elements of an array
scanf("%d",&a[i]);
min=a[0];
max=a[0];
for(i=0;i<n;i++)// read the elements of an array
{
if(a[i]<min)// check the condition for minimum value
min=a[i];
if(a[i]>max)//check the condition for maximum value
max=a[i];
}
printf("maximum value is:%d\n",max);
printf("minimum value is:%d\n",min);
getch();
}
Output:
1.enter the array size:4
E n t e r t h e e l e m e n t s o f a r r a y 3 6
1 3 2 4 5
E n t e r t h e e l e m e n t s o f a r r a y -
6 9 - 9 2 5
Conclusion:
The program is error free
VIVA QUESATIONS:
What is an array?
Ans: The collection of similar elements is called array
2) How many types of arrays are there ?
Ans: Three types. They are one dimensional ,two dimensional and multi dimensional arrys
3. Write a program to find the average mail height & average female heights in the class
(input is in the form of sex code, height).
#include<stdio.h>
#include<conio.h>
void main()
for(count=0;count<5;count++)
printf("male");
scanf("%d",&mh[count]);
mtot+=mh[count];
mavg=mtot/5;
printf("\n");
for(count=0;count<5;count++)
printf("female");
scanf("%d",&fh[count]);
ftot+=fh[count];
favg+=ftot/5;
getch();
Output:-
Description:
Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
Algorithm:
Step 1: start
Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a
Step 7: stop
Program:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
/* check the condition */
if(a==0)
printf("Enter value should not be zero ");
else
{
d=b*b-4*a*c;
/* check the condition */
if(d>0)
{
r1=(-b+sqrt(d)/(2*a));
r2=(-b-sqrt(d)/(2*a));
printf("roots are real and unequal\n");
printf("%f\n%f\n",r1,r2);
}
elseif(d==0)
{
r1=-b/(2*a);
r2=-b/(2*a);
printf("roots are real and equal\n");
printf("root=%f\n",r1);
printf("root=%f\n",r2);
}
Else
printf("roots are imaginary");
}
getch();
}
Output:
Enter the values for equation: 1, 6, 9
Roots are real and equal
Root= -3.0000
Root= -3.0000
Conclusion:
The program is error free
VIVA QUESATIONS:1)
1. What are various types of loop statements?
Ans : While, do- while, for loop statements
Description:
This program contains n number of elements, in these elements we can find the largest and display these two
numbers
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
Read a[i]
Increment i
Goto step 4
Step 5: min=a[0], max=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
If a[i]<min
Assign min=a[i]
Increment i goto Step 7
Step 8: print min,max
Step 9: sto
Program:
#include<stdio.h>
void main()
{
int a[50],i,n,min,max;
clrscr();
printf("enter the array size:");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++) // read the elements of an array
scanf("%d",&a[i]);
min=a[0];
max=a[0];
for(i=0;i<n;i++)// read the elements of an array
{
if(a[i]<min)// check the condition for minimum value
min=a[i];
if(a[i]>max)//check the condition for maximum value
max=a[i];
}
printf("maximum value is:%d\n",max);
printf("minimum value is:%d\n",min);
getch();
}
Output:
1.enter the array size:4
E n t e r t h e e l e m e n t s o f a r r a y 3 6
1 3 2 4 5
maximum value is:45
minimum value is:22.
Conclusion:
The program is error free
VIVA QUESATIONS:
What is an array?
Ans: The collection of similar elements is called array
2) How many types of arrays are there ?
Ans: Three types. They are one dimensional ,two dimensional and multi dimensional arrys
6. Write a program to multiply two matrices.
Objective:6
To perform the multiply of two matrices
Description:
program takes the two matrixes of same size and performs the multiply an alsotakes the two matrixes of different
sizes and checks for possibility of multiplication and perform multiplication if possible.
Algorithm:
Step 1: start
Step 2: read the size of matrices A,B – m,n
Step 3: read the elements of matrix A
Step 4: read the elements of matrix B
Step 5: select the choice for you want. If you select case 1 then goto matricaddition. Else goto Step 7.
Step 6: print Sum of matrix A and B
Step 7: if you select case 2 then goto matrix multiplication
Step 8: check if n=p, if not print matrices can not be multiplied
Step 9: Otherwise perform the multiplication of matrices
Step 10: Print the resultant matrix
Step 11: Stop
Program:
#include<stdio.h>
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("\n\t\tMENU"); printf("\n**********************************");
printf("\n[1]ADDITION OF TWO MATRICES");
printf("\n[2]MULTIPLICATION OF TWO MATRICES");
printf("\n[0]EXIT");
printf("\n**********************************");
printf("\n\tEnter your choice:\n");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:printf("Input rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);printf("Enter elements of matrix A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
For(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");
}
break;
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]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant of two matrices:\n");
write_matrix(c,m,q);}/*end if*/
else
{
printf("Matrices cannot be multiplied.");}/*end else*/
break;
case 0:printf("\n Choice Terminated");
exit();
break;
default:printf("\n Invalid Choice")
}
getch();
}
/*Function read matrix*/
int read_matrix(int a[10][10],
int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
return 0;
}
/*Function to write the matrix*/
int write_matrix(int a[10][10],
int m,int n)
{
int i,j;for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");}return 0;
}
Output:
1.************************************
MENU
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
#include<conio.h>
#include<string.h>
main()
clrscr();
char arr[100];
gets(arr);
strrev(arr);
getch();
Output:
AFSET
TESFA
8. Write a program to concatenate two strings of different lengths.
Objective: 8
Functions to insert a sub string into given main string from a given position
Description
In this program we need to insert a string into another string from a specified position.
Algorithm:
Step 1: start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string( p)
Step 7: copy sub string into main string from position p-1
Step 8: copy temporary string into main string from position p+n-1
Step 9: print the strings
Step 10: stop
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
Output:
1. Enter first string:
Computer
2 Enter second string:
Gec
3 Enter the position where the item has to be inserted:3
comgecputer
Conclusion:
The program is error free
VIVA QUESATIONS:
1) What is string?
Ans: A string is an collection of characters
2) Which command is used to combine the two strings?
Ans: Strcat()
3) Whi ch com mand is used t o cop y t h e str ings ?
Ans: By using the strcpy() function copies one string to another
9. Represent a deck of playing cards using arrays.
//DeckOfCards.h
class Deckofcards
public:
Deckofcards();
void shuffle();
void deal();
private:
int deck[4][13];
};
//client.cpp
#include "DeckOfCards.h"
int main()
Deckofcards deckofcards;
deckofcards.shuffle();
deckofcards.deal();
return 0;
}//end main
//Deckofcards.cpp
#include <iostream>
using std::cout;
using std::left;
using std::right;
#include <iomanip>
using std::setw;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
#include "DeckOfCards.h"
Deckofcards::Deckofcards()
void Deckofcards::shuffle()
do
row = rand()%4;
column = rand()%13;
}//end for
}//end shuffle
void Deckofcards::deal()
static const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven"
if (deck[row][column] == card)
cout << setw(5) << right << face[column] << " of " << setw(8)
}//end if
}//end for
}//end for
}//end for
}//end deal
10. Write a program to check that the input string is a palindrome or not.
Objective: 10
To determine if the given string is a palindrome or not
Description:
if the reverse of a string is equal to original string then it is called palindrome
Algorithm:
Step 1:start
Step 2: read the string
Step 3: store reverse of the given string in a temporary string
Step 4: compare the two strings
Step 5: if both are equal then print palindrome
Step 6: otherwise print not palindrome
Step 7: stop
Program:
#include<stdio.h>
#include<string.h>
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
enum Boolean matched=true;
if(len==0)return 0;left=0;right=len-1;/* Compare the first and last letter,second & second last & so on */
while(left<right && matched)
{
if(string[left]!=string[right])matched=false;
else
{
left++;right--;
}
}
return matched;
}
int main()
{char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;
}
Output:
1. Enter the string:
malayalam
The given string malayalam is a palindrome
2. Enter the string:
India
The given string india is not a palindrome
Conclusion:
The program is error free
VIVA QUESATIONS:
# include <conio.h>
void main()
char c ;
FILE *fptr1 ;
clrscr() ;
fptr1 = fopen("COURSES.DAT","w") ;
fputc(c, fptr1) ;
fclose(fptr1) ;
do
c = fgetc(fptr1) ;
putchar(c) ;
} while(c != EOF) ;
fclose(fptr1) ;
getch() ;
}
Output:-
#include <stdio.h>
void main()
int c ;
c = getc( fp ) ;
while ( c != EOF )
putchar( c );
c = getc ( fp );
fclose( fp );
}
12. Write a program the sum of three digits given numbers.
Objective :12
Description:
Sum of the individual digits means adding all the digits of a number
Ex: 123 sum of digits is 1+2+3=6
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize the s=0
Step 4: if n<0 goto Step 7
Step 5: if n!=0 goto
Step 6 else goto step 7
Step 6: store n%10 value in p
Add p value to s
Assign n/10 value to n
Goto Step 5
Step 7: print the output
Step 8:stop
Program:
#include<stdio.h>
main()
{
int n,s,p;clrscr();
printf("enter the vaue for n:\n");
scanf("%d",&n);
s=0;
if(n<0)
printf("The given number is not valid");
else
{
while(n!=0) /* check the given value =0 or not */
{
p=n%10;
n=n/10;
s=s+p;
}
printf("sum of individual digits is %d",s);
}
getch();
}
Output:
1.Enter the value for n: 333
Sum of individual digits is 9
2.Enter the value for n: 4733
Sum of individual digits is 17
3. Enter the value for n: -111
The given number is not valid
Conclusion:
The program is error free
VIVA QUESATIONS:
Objective:
To print the Fibonacci series for 1 to n value
Description
A fibonacci series is defined as follows
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1,t2,…………tn be terms in fibinacci sequence
t1=0, t2=1
tn=tn-2+tn-1……where n>2
Algorithm:
Step 1: start
Step 2: initialize the a=0, b=1
Step 3: read n
Step 4: if n== 1 print a go to step 7. else goto step 5
Step 5: if n== 2 print a, b go to step 7 else print a,b
Step 6: initialize i=3i ) i f i < = n d o a s f o l l o w s . I f n o t g o t o s t e p 7
c=a+b
print c
a=b
b=c
increment I value
goto step 6(i)
Step 7: stop
Program:
#include<stdio.h>
void main()
int a,b,c,n,i;
clrscr();
printf("enter n value");
scanf("%d",&n);a=0;b=1;
if(n==1)
printf("%d",a);
elseif(n==2)
printf("%d%d",a,b);
else
printf("%d%d",a,b);
PRINTED IN ADVANCE
for(i=2;i<n;i++)
c=a+b;
printf("%d",c);
a=b;
b=c;
getch();
Output:
1. Enter n value : 5
01123
2. Enter n value : 7
01 1 2 3 5 8
3. Enter n value : -6
01
Conclusion:
VIVA QUESATIONS:
The sub sequent terms 1 found by adding the preceding two terms in the sequence Formula: let
t1=0, t2=1
tn=tn-2+tn-1……where n>2
Description:
Prime number is a number which is exactly divisible by one and itself onlyEx: 2, 3,5,7,………;
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=1,c=0
Step 4:if i<=n goto step 5
If not goto step 10
Step 5: initialize j=1
Step 6: if j<=1 do as the follow. If no goto step 7
i)if i%j==0 increment c
ii) increment j
iii) goto Step 6
Step 7: if c== 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,j;
clrscr();
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("\n %d",i);
}
getch( );
}
Output:
Enter the number : 5
235
Enter the number : 10
2357
Enter the number : 12
2 3 5 7 11
Conclusion:
The program is error free
VIVA QUESATIONS:
1) What is prime number ?
Ans: Prime number is a number which is exactly divisible by one and itself only
2) What is an algorithm?
Ans : A step by step procedure is called algorithm
4) What is program?
Ans : A collection of statements is called
15. Write a program to find the factorial of a given integer.
Objective:
Programs that use recursive function to find the factorial of a given integer.
Description:
Factorial of a number is nothing but the multiplication of numbers from a given number to 1
Program:
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
int f;if((n==0)||(n==1))
// check the condition for the n valuereturn(n);
elsef=n*fact(n-1);
//calculate the factorial of nreturn(f);
}
void main()
{
int n;clrscr();
printf("enter the number :");
scanf("%d",&n);
printf("factoria of number%d",fact(n));
getch();
}
Output:
1 Enter the number: 5
Factorial of number: 120
2 Enter the number:
3 Factorial of number: 6
3. Enter the number: 9
Factorial of number: -30336
Two integer operands and one operator form user, performs the operationand then prints the result.
(Consider the operators +,-,*, /, % and use Switch Statement)
Description:
To take the two integer operands and one operator from user to performthe some arithmetic operations by using the
following operators like
+,-,*, /, %
Ex: 2+3=5
Algorithm:
Step 1: Start
Step 8: write
R
Step 9: End
Program:
#include<stdio.h>
main()
{
char op;float a,b,c;
clrscr();
printf("enter two operands:");
scanf("%d%d",&a,&b);
printf("enter an operator:");
scanf(" %c",&op);
switch(op) // used to select particular case from the user
{
Case '+':printf("sum of two numbers %2d %2d is: %d",a,b,a+b);
Break;
case '-':printf("subtraction of two numbers %2d %2d is:%d",a,b,a-b);
break;
case '*':printf("product of two numbers %2d %2d is:%d",a,b,a*b);
break;
case '/':printf("quotient of two numbers %2d %2d is:%d",a,b,a/b);
break;
case '%':printf("reminder of two numbers %2d %2d is:%d",a,b,c);
break;
default:printf("please enter correct operator");
break;
}getch();
}
Input/Output:
Conclusion:
Description:
In this program we need to delete a string from the given string at a specified position.
Algorithm:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and ( position + number of characters to end)
Step 6: stop
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
// Function to delete n characters
void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}
Output:
1. Enter the string
Nagaraju
Enter the position from where to delete:4
Enter the number of charcters to be deleted3
Nagju
Conclusion:
The program is error free
VIVA QUESATIONS:
1) Which command is used to delete the strings ?
Ans: delstr();
2) What are the various types of string functions ?
Ans: Strcat(), strcpy(), delstr(), substr() ,strlen()etc..
18. Program that displays the position or index in the string S
where the string T begins
Objective:
Program that displays the position or index in the string S where the string T begins , or -1 if S doesn’t contain T
Algorithm:
Step 1: start
Step 2: read the string and then displayed
Step 3: read the string to be searched and then displayed
Step 4: searching the string T in string S and then perform the following stepsi . f o u n d = s t r s t r ( S , T ) ii.
if found print the second string is found in the first string at the position. If not
goto step 5
Step 5: print the -1
Step 6: stop
Pogram:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);elseprintf("-1");
getch();
}
Output:
1.enter the first string:
Kali
Enter the string to be seareched:
lisecond string is found in the first string at2
position2.
enter the first string:nagaraju
Enter the string to be seareched:
rajusecond string is found in the first string at4
position3.
enter the first string:nagarjunaEnter the string to be seareched:ma-1
Description:
In this program we have to count the no of lines, no of words and no of characters in a given program or given
text by using the string function
Algorithm:
Step 1: Start
Step 2: Read the text until an empty line
Step 3: Compare each character with newline char ‘\n’ to count no of lines
Step 4: Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count no of words
Step 5: Compare first character with NULL char ‘\0’ to find the end of text
Step 6: No of characters = length of each line of text
Step 7: Print no of lines, no of words, no of chars
Step 8: Stop
Program:
#include <stdio.h>
main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf("KEY IN THE TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");
while( end == 0)
{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}
Output
1.KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS 'RETURN'.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not77
Conclusion:
The program is error free
VIVA QUESATIONS: