C Programming Basics: Functions & Tests
C Programming Basics: Functions & Tests
02 STUDENTRESULT
03 PRIMENUMBER
04 SORTINGBYUSINGARRAY
05 STACKOPERATION
06 QUEUEMANIPULATION
07 PALINDROME
1. SUMOFINDIVIDUALDIGITS
[Link]
DATE:
AIM:
Togetsumofindividualdigitsbycprogramusethefollowing.
ALGORITHM:
STEP1:Starttheprocess.
STEP2:Getnumberbyuser.
STEP3:Getthemodulus/remainderofthenumber.
STEP4:Sumtheremainderofthenumber.
STEP5:Dividethenumberby10
STEP6:Repeatthestep2whilenumberisgreaterthan0.
STEP7:Displaytheresult.
STEP8:Stoptheprocess.
CODING:
#include<stdio.h>
int main()
{
int n, sum=0,m;
printf(“Enter a number:”);
scanf(“%d”,&n);
while(n>0);
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf(“Sum is =%d”,sum);
return 0;
}
OUTPUT:
TC-04 Displaying Actual Should Novalue Failure Move the Displaythe Success
givenvalue. Input printthe printed inputvalue value
should given to some
printalong Value variable x
withoutput
(Sum).
TC-06 Should not Onlygiven ShouldNot Printsthe Failure Create a Onlygiven Success
print the digit print digits flag to digits
middlesum should be middlesum avoidthe displayed
digits displayed digits. digits
display
TC-07 Singledigit Checking Should Input is Failure Check the Display the Success
input withinput displaythe displayed input value message
as single it is single asoutput. greaterthan regarding
digit digit 9 and then singledigit.
proceed
TC-08 Zeroas When Display Accept Failure Check the Program Success
Input input is message input input value terminated
zero regarding without iszero;ifso
program input any stop the
should message program
stop.
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
2. STUDENTRESULTS
[Link]
DATE:
AIM:
Todisplaythestudentsresultasrather“PASS”or“FAIL”.
ALGORITHM:
STEP1:Starttheprocess.
STEP2:Gettherequiredinputfromtheuser(Name&5subjectmarks).
STEP 3: Check the condition (i.e.) if all the 5 subject mark is greater than 40
Print the result as "PASS" else result as "FAIL".
STEP4:Displaythestudentresults.
STEP5:Stoptheprocess.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];int
s1,s2,s3,s4,s5;
clrscr();
printf("Enter the student name:");
scanf("%s",&name);
printf("Enter marks in five subjects \n");
printf("s1=");
scanf("%d",&s1);
printf("s2=");
scanf("%d",&s2);
printf("s3=");
scanf("%d",&s3);
printf("s4=");
scanf("%d",&s4);
printf("s5=");
scanf("%d",&s5);
if(s1<40 || s2<40 || s3<40 || s4<40 || s5<40)
printf("\n RESULT : FAIL");
else
printf("\nRESULT:PASS");
getch();
}
OUTPUT:
RESULT:PASS
RESULT:FAIL
2. TesttheCprogram:AccepttheinputsstudentName,Marksforfivesubjectsanddeclare the result as
PASS if the student gets minimum 40 in each subject otherwise declare the resultas FAIL.
TC-03 Skipamark Skippinga Should Skipsthe Failure Check each Waitsfor Success
input or input value wait for values Input value value for
Zero as by pressing inputuntil with whether it positive
input value just enter value > 0 askingfor hasvalueor value
keyorZero entered positive not.
as value values.
TC-04 Null as Checkwhat Should Does not Failure CheckInput Waitsfor Success
inputfor happens askfor askname value of name
name when null name and namebefore
given as moves to moving to
input to mark mark input.
name input
process.
TC-05 Negative Check with Shouldnot Doesnot Success ------ ----- ------
Mark as negative accept the accept
input marks as mark. the
inputvalue. negative
value
TC-06 Maximum Checkwhat Shouldnot Accepts Failure Checkeach Askfornew Success
mark happens accept thevalue Input value mark if it
when input with exceeds100
value maximum
entered mark
greaterthan
maximum
value(Max:
100)
TC-08 Skipsubject Nullvalue Shouldnot Accepts Failure Check Does not Success
name enter as accept the thename subjectname acceptthe
input for subject as null Input nullvalue
subject name as value&add as subject
name null conditionfor name
not
accepting
nullvalue
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
3. PRIMENUMBER
EXNO.:03
DATE:
AIM:
Togeneratenprimenumbers
ALGORITHM:
STEP1:Starttheprocess.
STEP2:Gettheinputvalueofnumber.
STEP3:Usingtheformula.
STEP 4: Calculation of the function.
STEP 5: Display the prime numbers.
STEP 6: Stop the process.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,n,s=0,flag=0;
clrscr();
printf("\n\n\t\t ***************************");
scanf("%d",&n);
for(i=1;i<n;i++)
flag=1;
for(j=2;j<i;j++)
if(i%j==0)
flag=0;
break;
}
}
if(flag)
printf("%d",i);
s++;
getch();
OUTPUT:
Enterthevalue:10
The prime numbers are: 1 2 3 5 7
The total prime numbers are: 5
3. TesttheCprogram:ProgramforgeneratingNprimenumbers.
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
4. SORTINGBYUSINGARRAY
[Link]
DATE:
AIM:
Tocreatetwoarraylistofintegerssortandstoretheelementsofbothoftheminthird
list.
ALGORITHAM:
STEP1:Starttheprocess.
STEP2:Declarenecessaryheaderfileanddatatypes.
STEP3:Entertheelementsforfirstarray.
STEP4:Entertheelementsforsecondarray.
STEP5:Storetheelementsoffirstandsecondarrayintothirdarray.
STEP6:Sorttheelementsinthirdarray.
STEP7:Displaythesortedarray.
STEP8:Stoptheprocess.
CODING:
#include<studio.h>
#include<conio.h>
#include<math.h>
void main()
{
intm,n,p,sum=0;
int lista[5],listb[5],listc[10]={0};
clrscr();
printf("\nEnter Elements for First List:");
for(m=0;m<5;m++)
{
scanf("%d",&lista[m]);
if(lista[m]==0)m--;
sum=sum+abs(lista[m]);
}
printf("\nEnter Elements for Second List:");
for(n=0;n<5;n++)
{
scanf("%d",&listb[n]);
if(lista[n]==0)n--;
sum=sum+abs(listb[n]);
}
p=n=m=0;
m=m-sum;
while(m<sum)
{
for(n=0;n<5;n++)
{
if(m==lista[n] || m==listb[n])
listc[p++]=m;
if(m==lista[n] && m==listb[n])
listc[p++]=m;
}
m++;
}
puts("\nMerged Sorted List :");
for(n=0;n<10;++n)
printf("%d",listc[n]);
getch();
}
OUTPUT:
EnterElementsforFirstList :154-32
EnterElementsforSecondList 915215
MergedSortedList :-31122455915
4. TesttheCprogram:Sortandstoretheelementstwoarraysofintegersintothethirdlist.
TC- Array Size Checkthe Should Doesnot Failure Check the Showsthe Success
01 valuecheck array size displaythe showthe input value message
inputwith Error and error whether
negative ask fornew >0otherwis
value value e show the
message as
“Incorrect
size”
TC- Check For Checkfor Should Executes Success ------- ------ ------
04 loop for loop execute correctly
continuous executed the loop for all
operation correctly loops
TC- While Checking Should the Showthe Success ------ ----- ------
07 statement the while message Message
execution statement for non
withinput acceptance
valuezero ofzero.
TC- Arraylimit Check Shouldnot Does not Failure Check the Showsthe Success
08 what accept anyshow arrayinput massage
happens when size message size with
whensize is greater and maximum
of array than 10. produces limit
exceeds incorrect
than its result
limit.
TC- Final list Check Should Condition Success ----- ----- -----
09 arraysize whether accept satisfied
limit both whentotal
elements of array 1
can be and2size is
storedin less or
third equalto
array. 20.
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
5. STACKOPERATION
[Link]
DATE:
AIM:
ToexperimentintheoperationofSTACKusingarrayimplementation.
ALGORITHM:
STEP1:Starttheprocess.
STEP2:Definethesizeofthestack.
STEP3:Inputthechoice,usingINSERTorDELETE.
STEP4:Ifchoice=“INSERT”thengotoSTEP5elsegotoSTEP10.
STEP 5: Check if the stack is full then display “STACK IS FULL” go
to STEP else go to STEP 6.
STEP6:InitializeTOP=0inputtheelementandassignittoTOP.
STEP 7: Increment the TOP by ONE.
STEP 8: Display the element of stack.
STEP 9: Go to STEP 13.
STEP 10: Check if the STACK IS EMPTY [no elements in stack] then
display “EMPTY STACK” else go to STEP 11.
STEP 11: Decrement the TOP by ONE.
STEP 12: Display the elements in the stack.
STEP 13: Stop the process.
CODING:
#include<stdio.h>
#include<conio.h>
void insert();
void
del();Voiddispl
ay();
int stack[50],p,s;
void main()
{
int ch;
clrscr();
printf("\n\n\n\tSTACKOPERATION");
printf("\n\n\nEnter The Stack Size Of Stack(max_len50):"); scanf("%d",&s);
if(s>=1&&s<=50)
{
while(1)
{
clrscr();
printf("\n\n\t\t STACK OPERATION"); printf("\n\n\
t\t MENU");
printf("\n\n\t [Link]\n\n\t [Link] \n\n\t [Link]\n\n\t [Link]"); printf("\
n\n\tEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case1:
insert();
break;
case2:
del();
break;
case3:
display();
break;
case4:
exit();
default:
printf("\n\n\tInvalidChoice");
}
getch();
}
}
else
printf("\n\n\tInvalid Data");
getch();
}
voidinsert()
{
if(p==s)
printf("\n\n\tSTACK IS FULL"); else
{ p+
+;
printf("\n\n\tEnter An Element:");
scanf("%d", & stack[p]);
}
}
voiddel()
{
if(p==0)
printf("\n\n\tNo Elements in Stack to Delete"); else
{
printf("\n\n\tThe Deleted Element is %d",stack[p]);
p--;
}
}
voiddisplay()
{
int i;
if(p==0)
printf("\n\n\tSTACK IS EMPTY");
else
{
for(i=1;i<=p;i++) printf("\n\n\t
%d\n",stack[i]);
}
}
OUTPUT:
STACKOPERATION
EnterTheStackSizeofStack(max_len50):30
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
EnterYourChoice:1
EnterAnElement:100
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
EnterYourChoice:1
EnterAnElement:200
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
EnterYourChoice:1
EnterAnElement:300
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
Enter Your Choice: 3
100
200
300
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
EnterYourChoice:2
TheDeletedElementis:300
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
Enter Your Choice: @
Invalid Choice
STACKOPERATION
MENU
1. Insert
2. Delete
3. Display
4. EXIT
EnterYourChoice:4
1. TesttheCprogram:ExperimenttheoperationsofSTACKusingarrayimplementation.
TC- Character Type Shouldan Doesnot Failure Place the Shows the Success
02 input to character the error showthe coding errorwhen
choice input for that it is error. which incorrect
operation incorrect checkthe input
selection value choice
input is
number
value.
TC- [Link] Should Empty Doesnot Failure Find the Showsthe Success
04 empty showthe Spacein showthe numberof numbers
position number stack numbers empty
ofempty space and
position show the
of stack numbers
while
elements
of stack
listed.
TC- Check Check Should Works Failure Make the Worksfor Success
08 Whileloop while acceptthe onlyfor necessary bothcases
for loop user “y”. changes to
continuous executed choice y work on
operation when (or) Yand bothcases.
there execute
valuefor theloop
chisyes.
TC- Checkfor Stack Shouldnot Accept Failure Check the Negative Success
09 non- should acceptthe the inputvalue valuenot
negative acceptthe elements element for non- accepted
elements positive negative
elements value.
TC- Verify all Check all Should Choice 4 Failure Make the Exitswhen success
10 userchoice theoptions work on does not necessary 4 is
withinput the basis exitsfrom coding selected
values ofchoice operation changes
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
6. QUEUEMANIPULATION
[Link]
DATE:
AIM:
TocreatemenudriveprogramtoimplementQUEUEtoperformthefollowing.
INSERTION
DELETION
MODIFICATION
LISTINGOFELEMENTSUSINGPOINTERS
ALGORITHM:
STEP1:Starttheprocess.
STEP 2: Input the choice if CHOICE =1(add) go to STEP 3 else if
CHOICE=2(del) go to STEP 5 else if CHOICE=3(modify) else go to
(list).
STEP 3: Initialize REAR= -1 & FRONT=0 check QUEUE OVERFLOW & if no
overflow occurs then get the number to be stored.
STEP4:IncrementREARbyONE&storethenumberinthereargotoSTEP8.
STEP5:IncrementFRONTbyONEgotoSTEP8.
STEP6:Getthenumbertobemodified.
STEP7:Searchthegivennumberbyusingthepointerandgetandassignit.
STEP8:DisplaytheelementintheQUEUE.
STEP9:Stoptheprocess.
CODING:
#include<stdio.h>
#include<conio.h>
#define max 4
#define ture 1
intqueue[max];
int rear=-1,front=0;
void main()
{
int
ch;voidadd
();
voiddel();
voidmodify();
void list();
while(1)
{
clrscr();
printf("\n\tQUEUE MAINPULATION");
printf("\n\t.....................");
printf("\nMAIN MENU");
printf("\n..........");
printf("\n1].ADD");
printf("\n2].DELETE");
printf("\n3].MODIFY");
printf("\n4].LIST");
printf("\n5].EXIT");
printf("\nENTERYOURCHOICE:::>");
scanf("%d",&ch);
switch(ch)
{
case 1:add();break;
case 2:del();break;case
3:modify();break;
case4:list();break;
case 5:printf("\nPROGRAM IS TERMINATED");
getch();
exit(0);
break;
}
}
}
voidadd()
{
intx;
printf("\nENTERTHENUMBERTOADD::::>");
scanf("%d",&x);
if(rear>=max-1)
printf("\nQUEUE OVERFLOW");
else
{
rear++;queue[rear]=x;
printf("\n%dISSUCCESSFULLYADDED",x);
}
getch();
}
voiddel()
{
int x;
if(front>rear)
printf("\nQUEUEUNDERFLOW");
else
{
x=queue[front];front++;
printf("\n%dISSUCCEESSFULLYDELETE",x);
}
getch();
}
voidmodify()
{
intx,y,i,flag=0;
int *p;
if(front>rear)
printf("\nQUEUE IS EMPTY");
else
{
p=&queue[front];
printf("\nENTERTHENUMBERTOMODIFY:");
scanf("%d",&x);
i=front;
while(i<=rear)
{
if(*p==x)
{
flag=1;
printf("\nENTERTHENEWNUMBERTOMODIFY:");
scanf("%d",&y);
*p=y;
printf("\nNUMBERISSUCCESSFULLYMODIFY");
break;
}
p++;
i++;
}
if(flag==0)
printf("\nNUMBERNOTFOUND");
}
getch();
}
voidlist()
{
int i;
if(front>rear)
printf("\nQUEUE IS EMPTY");
else
{
printf("\n\t%d...>rear",queue[rear]);
for(i=rear-1;i>front;i--) printf("\n\t
%d",queue[i]); printf("\n\t
%d...>front",queue[front]);
}
getch();
}
OUTPUT:
QUEUEMAINPULATION
..........................................
MAINMENU
…………......
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>1
ENTER THE NUMBER TO ADD ::::> 10
10 IS SUCCESSFULLY ADDED
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>1
ENTER THE NUMBER TO ADD ::::> 20
20 IS SUCCESSFULLY ADDED
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>1
ENTER THE NUMBER TO ADD ::::> 30
30 IS SUCCESSFULLY ADDED
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>4
30...>rear
20
10...>front
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>3
ENTER THE NUMBER TO MODIFY : 20
ENTER THE NEW NUMBER TO MODIFY : 25
NUMBER IS SUCCESSFULLY MODIFY
1].ADD
2].DELETE
3].MODIFY
4].LIST
5].EXIT
ENTERYOURCHOICE:::>5
PROGRAMISTERMINATED
6. TesttheCprogram:Menu-drivenoptionforQUEUEoperations.
TC- Character Type Shouldan Doesnot Failure Place the Shows the Success
02 input to character the error showthe coding errorwhen
choice input for that it is error. which incorrect
operation incorrect checkthe input
selection value choice
input is
number
value.
TC- [Link] Should Empty Doesnot Failure Find the Showsthe Success
04 empty showthe Spacein showthe numberof numbers
position number Queue numbers empty
ofempty space and
position show the
of queue numbers
while
elements
listed.
TC- Check Check Should Works Failure Make the Worksfor Success
08 Whileloop while acceptthe onlyfor necessary bothcases
for loop user “y”. changes to
continuous executed choice y work on
operation when (or) Yand bothvalues
there execute (Y or y).
value for the loop
chisyes.
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.
7. PALINDROME
[Link]
DATE:
AIM:
Tocreatepalindromestringcheckingprogram(UsingPointers).
ALGORITHAM:
STEP1:Starttheprocess.
STEP2:GetastringandusingPointer.
STEP3:Findthelivesofthegivenstringandstartedinptr2.
STEP4:Usingwhile()andifstatements.
STEP5:Ifptr1&ptr2aresamethenprintTheStringisPalindrome.
STEP6:ElseprintTheStringisnotPalindrome
STEP7:Stoptheprocess.
CODING:
#include<iostream.h>
#include<conio.h>
void main()
{
int con,len=0;
char*name,*p;
p=name;
clrscr();
cout<<"\n\nEnter a String:";
cin>>name;
while(*p!='\0')
{
len++; p+
+;
}
len-=1;
p=name;
while(*p!='\0')
{
if(*p==name[len])
con=1;
else
{
con=0;
break;
} p+
+;
len--;
}
if(con==1)
cout<<"\n\n The String is Palindrome";
else
cout<<"\n\nThe String is not Palindrome";
getch();
}
OUTPUT:
EnteraString:RACECAR
TheStringisPalindrome
EnteraString:KARTHICK
TheStringisnotPalindrome
7. TesttheC++Program:Palindromestringcheckingprogram.(usingPointers)
TC- Check For Checkfor Should Executes Success ------- ------ ------
04 loop for loop executethe correctly
continuous executed loop for all
operation correctly loops
TC- Arrayindex Check the By default Starts Success ------ ---- -----
09 arrayindex it should with0
starting startwith0
value
RESULT:
Thustheprogramhasbeenexecutedandverifiedsuccessfully.