0% found this document useful (0 votes)
8 views47 pages

C++ Assigment

Uploaded by

Mohammed Abdi
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)
8 views47 pages

C++ Assigment

Uploaded by

Mohammed Abdi
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/ 47

AL-FALAH UNIVERSITY

(A Muslim Minority Autonomous Institution)

Department of Computer Science & Engineering

Lab File

Introduction to C

Submitted By Submitted to

Department of Computer Science and Engineering


Al-Falah School of Engineering and Technology
Dhauj Faridabad Haryana
INDEX

1. Syllabus

2. Hardware/Software Requirements

3. Practicals to be conducted in the lab

4. Programs
CSE-103F FCPC Lab

L T P Class Work : 25 Marks


0 0 2 Exam : 25 Marks
Total : 50 Marks
Duration Of Exam : 3 Hrs.
Important Note :-

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.

Samples (types) of the programming problems to be practiced :-

1. Write a program to find the largest of three numbers. (if-then-else)


2. Write a program to find the largest of ten numbers (for-statement)
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).
4. Write a program to find roots of a quadratic equation using functions and switch statements.
5. Write a program using arrays to find the largest and second largest numbers out of given 50
numbers.
6. Write a program to multiply two matrices.
7. Write a program to read a string and write it in reverse order.
8. Write a program to concatenate two strings of different lengths.
9. Represent a deck of playing cards using arrays.
10. Write a program to check that the input string is a palindrome or not.
11. Programs on file handling.
HARDWARE REQUIRED

P-IV/III PROCESSOR

HDD 40GB

RAM 128MB or above

SOFTWARE REQUIRED

Window 98/2000/ME/XP

Turbo C, C++
Practical’s to be conducted in the lab

1. Write a program to find the largest of three numbers.


2. write a program to find the largest of ten numbers (for-statement)
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).
4. Write a program to find roots of a quadratic equation using functions and switch statements.
5. Write a program using arrays to find the largest and second largest numbers out of given 50
numbers.
6. Write a program to multiply two matrices.
7. Write a program to read a string and write it in reverse order.
8. Write a program to concatenate two strings of different lengths.
9. Represent a deck of playing cards using arrays.
10. Write a program to check that the input string is a palindrome or not.
11. Programs on file handling.
12. Write a program the sum of three digits given numbers.

13. Write a program to Fibonacci series for 1 to n value


14. Write a program a prime numbers up to 1 to n
15. Write a program to find the factorial of a given integer.
16. Write a program to Switch case
17. Write a program delete n characters from a given position in a given string
18. Program that displays the position or index in the string S where the string T begins
19. Write a program to count the lines, words & characters in a given text string.
Programs

1. Write a program to find the largest of three numbers.

ALGORITHM TO FIND LARGEST OF THREE NUMBERS

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;

2. print ‘Enter any three numbers:’;

3. read a,b,c;

4. if((a>b)&&(a>c))

5. print ‘Largest of three numbers:’, a;

6. else

7. if((b>a)&&(b>c))

8. print ‘Largest of three numbers:’, b;

9. else

10.print ‘Largest of three numbers:’, c; PROGRAM TO FIND LARGEST OF THREE NUMBERS

#include<stdio.h>

#include<conio.h>

void main()

{
clrscr();

int a,b,c;

printf("Enter the values of a,b & c");

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

if(a>b && a>c)

printf("a is greater");

else if(b>a && b>c)

printf("b is geater");

else

printf("c is geater");

getch();

Output:-

Enter the values of a,b & c

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

maximum value is:45


minimum value is:22.

2 enter the array size: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 2 1 3 8

maximum value is:8


minimum value is:13.
enter the array size: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

maximum value is:9


minimum value is:-9

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()

float mavg=0, favg=0;

int mh[5], fh[5], count;

float mtot, ftot;

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

printf("\nEnter the height of %d",count+1);

printf("male");

scanf("%d",&mh[count]);

mtot+=mh[count];

mavg=mtot/5;

printf("\n");

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

printf("\n\nEnter the height of %d",count+1);

printf("female");

scanf("%d",&fh[count]);
ftot+=fh[count];

favg+=ftot/5;

printf("\nThe average male height is %f",mavg);

printf("\nThe average female height is %f",favg);

getch();

Output:-

Enter the height of 1male 25

Enter the height of 2male 27

Enter the height of 3male 21

Enter the height of 4male 19

Enter the height of 5male 20

Enter the height of 1female 24

Enter the height of 2female 23

Enter the height of 3female 18

Enter the height of 4female 19

Enter the height of 5female 17

The average male height is 22.400000

The average female height is 20.200001


4. Write a program to find roots of a quadratic equation using functions and switch
statements.
Objective: 4

To find the roots of the quadratic equation

Description:
Nature of roots of quadratic equation can be known from the quadrant = b2-4ac

If b2-4ac >0 then roots are real and unequal

If b2-4ac =0 then roots are real and equal

If b2-4ac <0 then roots are imaginary

Algorithm:
Step 1: start

Step 2: read the a,b,c value

Step 3: if b*b-4ac>0 then

Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a

Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a

Step 4: if b*b-4ac=0 then

Root1 = Root2 = -b/(2*a)

Step 5: Otherwise Print Imaginary roots. Goto step 7.

Step 6: print roots

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

2. Enter the values for equation: 2, 7, 6


Roots are real and unequal Root= -6.75
Root= -7.253. Enter the values for equation: 1, 2, 3
Roots are imaginary

Conclusion:
The program is error free

VIVA QUESATIONS:1)
1. What are various types of loop statements?
Ans : While, do- while, for loop statements

2 What is the difference betw een while and do -while statements ?


Ans: In while the condition will be checked first and then enter into a loop. But in do- while the statements will
be executed first and then finally check theCondition.

3 How to find the roots of qudratric equtations ?


Ans: Nature of roots of quadratic equation can be known from the quadrant
= b2-4ac
If b2-4ac >0
then roots are real and unequal
If b2-4ac =0
Then
roots are real and equal
If b2-4ac <0
then roots are imaginary

4) List out the C features?


Ans: Portability, flexibility, wide acceptability etc.
5. Write a program using arrays to find the largest and second largest numbers out of given
50 numbers.
Objective : 5
To find both the largest number in the out of 50 numbers.

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.

2 enter the array size: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 2 1 3 8 maximum value is:8
minimum value is:13.
enter the array size: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
maximum value is:9
minimum value is:-9

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;

case 2:printf("Input rows and columns of A matrix:");


scanf("%d%d",&m,&n);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be multiplied\n");
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B matrix\n");/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf("\n =====Matrix Multiplication=====\n");

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

Enter your choice:


1
Valid Choice
Input rows and columns of A & B Matrix:2
2
Enter elements of matrix A:2
2
2
2
Enter elements of matrix B:
2
2
2
2
=====Matrix Addition=====
44
44
MENU
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
Enter your choice:
2
Valid Choice
Input rows and columns of A matrix:2
3
Input rows and columns of B matrix:
2
2
Matrices cannot be multiplied.
************************************MENU**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
Enter your choice:2
Valid Choice
Input rows and columns of A matrix:2
2
Input rows and columns of B matrix:2
2
matrices can be multiplied resultant matrix is 2*2
Input A matrix
2
2
2
2
Input B matrix
2
2
2
2
===============Matrix Multiplication=============
Resultant of two matrices:8 8
88
Conclusion:
The program is error free
VIVA QUESATIONS

1)What is condition for performing an matric addition ?


Ans: program takes the two matrixes of same size and performs the addition
2)What is condition for performing an matric addition ?
Ans: The two matrixes of different sizes and checks for possibility of multiplicationand perform multiplication if possible
7. Write a program to read a string and write it in reverse order.
#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

clrscr();

char arr[100];

printf("Enter a string to reverse\n");

gets(arr);

strrev(arr);

printf("Reverse of entered string is \n%s\n",arr);

getch();

Output:

Enter a string to reverse

AFSET

Reverse of entered string is

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()

for (int row = 0; row <= 3; row++ )

for (int column = 0; row <= 12; column++)

deck[row][column] = 0; //slot of deck is 0

}//end column for

}//end row for

srand(time(0));//seed random number

}//end Deckofcards constructor

void Deckofcards::shuffle()

cout << "hit";

int row; //suit of card

int column; //face of card

for (int card = 1; card <=52; card++)


{

do

row = rand()%4;

column = rand()%13;

} while( deck[row][column] !=0);//end do...while

deck [row][column] = card;

}//end for

}//end shuffle

void Deckofcards::deal()

cout << "hit";

static const char *suit[4] = {"Hearts", "Diamonds", "Clovers", "Spades"};

static const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven"

, "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

for (int card = 1; card <=52; card++)

for (int row = 0; row <= 3; row++)

for (int column = 0; column <= 12; column++)

if (deck[row][column] == card)

cout << setw(5) << right << face[column] << " of " << setw(8)

<< left << suit[row] << (card %2 == 0 ? '\n' : '\t');

}//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:

1)What is m eant b y pa lind r om e ?


Ans: If the reverse of a string/number is equal to original string/ number then it is called palindrome.
2)What is th e us e of gets() fun cti on ?
Ans: To read the string at a time3 )
What is th e u se of puts () fun cti on ?
Ans: To write the string at a time
11. Programs on file handling.
# include <stdio.h>

# include <conio.h>

void main()

char c ;

FILE *fptr1 ;

clrscr() ;

printf("Enter the text to be stored in the file.\n") ;

printf("Use ^Z or F6 at the end of the text and press ENTER: \n\n") ;

fptr1 = fopen("COURSES.DAT","w") ;

while((c = getc(stdin)) != EOF)

fputc(c, fptr1) ;

fclose(fptr1) ;

printf("\nThe content of the file is : \n\n") ;

fptr1 = fopen("COURSES.DAT", "r") ;

do

c = fgetc(fptr1) ;

putchar(c) ;

} while(c != EOF) ;

fclose(fptr1) ;

getch() ;

}
Output:-

Enter the text to be stored in the file.


Use ^Z or F6 at the end of the text and press ENTER:
Computer Science & Engineering
Information Technology
Electronics & Communication Engineering
^Z
The content of the file is :
Computer Science & Engineering
Information Technology
Electronics & Communication Engineering

/* file.c: Display contents of a file on screen */

#include <stdio.h>

void main()

FILE *fopen(), *fp;

int c ;

fp = fopen( “prog.c”, “r” );

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

To find the sum of individual digits of a given number

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:

1) What is the mean of sum of the individual digits?


Ans: Sum of the individual digits means adding each digit in a number
2) What is positive integer?
Ans: if the integer value is grater than zero then it is called positive integer
3) Define preprocessor ?
Ans: Before compiling a process called preprocessing is done on the source code by a program called
the preprocessor.
13. Write a program to Fibonacci series for 1 to n value

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);

//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS

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:

The program is error free6

VIVA QUESATIONS:

1) What is Fibonacci series ?


Ans: 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

2) What are the various types of unconditional statements?

Ans: goto,Break and continue

3)What are the various types of conditional statements?

Ans: if , if else ,switch statements

4)Expand <STDIO.H >?

Ans: standard input output header file


14 .Write a program a prime numbers up to 1 to n
Objective:
To print a prime numbers up to 1 to n

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

3) What is flow chart?


Ans: A pictorial representation an algorithm is called a flow chart

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

Algorithm: main program


Step 1: start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop
Sub program:

Step 1: initialize the f


Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) to main program

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

Conclusion: the program is error free


Viva questions

1) What is the meaning of factorial number?


Ans : Factorial of a number is nothing but the multiplication of numbers from a givennumber to 1

2) What is the meaning of recusive function?


Ans: A function call it self is called recursive function
3) Define library functions?
Ans: The functions have already been written, compiled and placed in libraries and arecalled library functions.
4) Define formal parameters?
Ans: Formal parameters are the parameters given in the function declaration asfunction definition.
16. Write a program to Switch case
Objective:

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 2: Read the values of a,b and operator

Step 3: if the operator is ‘+’ then


R=a+b
Go to step 8
Break

Step 4: Else if the operator is ‘-‘ then


R=a-b
Go to step 8

Step 5: Else if the operator is ‘*‘ then


R=a*b
Go to step 8

Step 6: Else if the operator is ‘/‘ then


R=a/b
Go to step 8

Step 7: Else if the operator is ‘%‘ then


R=a%b
Go to step 8

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:

1. Enter two operands:23


Enter an operator: +
Sum of two numbers 2 3 are: 5
2. Enter two operands: 3 4
Enter an operator: -
Subtraction of two numbers 3 4 are: -1
3. Enter two operands: 3 5
Enter an operator:*
Product of two numbers 3 5 are: 15
4. Enter two operands: 5 2
Enter an operator:/quotient of two numbers 5 2 are: 2
Enter two operands: 5 2enter an operator: %
4 reminder of two numbers 5 2 is: 1

Conclusion:

The program is error free


VIVA QUESATIONS:

1) What are the various types of arithemetic operators?


Ans: addition (+), multiplication (*), subtraction (-), division (/), modulo(%).

2) What are the types of relational operators?


Ans: less than (<), grater than (>), less than or equal to (<=), equal to(==), etc..,3)

3) What are the types of logical operators?


Ans: logical AND (&&), logical OR (||), logical NOT (!)
17 . Write a program delete n characters from a given position
in a given string
Objective:
To delete n characters from a given position in a given string

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 delchar(char *x,int a, int b);

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

2. enter the string


kaliraju
Enter the position from where to delete:0
Enter the number of charcters to be deleted4
Raju

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

Conclusion: The program is error free73


VIVA QUESATIONS:

1)What is the difference betw een printf() and puts() ?


Ans: puts() is used to display the string at a time and it doesn’t take any integers values but printf() takes any values as
defined by the user
2)define pointer variable ?
Ans: pointer variables are defined as variable that contain the memory addresses of data or executable code.

3)W ha t is us e of th e s t r cmp () fu n cti on ?


Ans: This function compares two strings character by character and returns a value 0if both strings are equal and non zero
value if the strings are different.
19. Write a program to count the lines, words & characters in a given text
Objective:
To count the lines, words & characters in a given text

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

Like those whom we admire.


Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205

Conclusion:
The program is error free

VIVA QUESATIONS:

1) What is use of strlen() ?


Ans: to read a string length
2) What is th e use of g et c( ) fun ct i on ?
Ans: To read the character one by one.
3) What is the use of strstr () ?
Ans: The function strstr() searches one string for the occurrence of another. It accepts two strings as parameters
and searches the first string for an occurrence of the second

You might also like