Beruflich Dokumente
Kultur Dokumente
CProgramsfortechnicalinterviews
Google Search...
Home
WP Plugins
WordPress
Linux
Academic
Internet
Projects
Downloads
Home Academic Programming C & C++ C & C++ Sample Prorgams 10 C programs you must know before appearing for technical interviews
Sponsors
22 comments
AdvertiseHere
SRM B.Tech
Admission 2015
Software Testing
Courses
srmuniv.ac.in/Admissions
shiksha.com/Software-Testing
AdvertiseHere
AdvertiseHere
AdvertiseHere
AdvertiseHere
If you havent opened Turbo C++ for more than a year and the campus
placements are not very far away then you are at the correct place. The
interviewers have a thing for C programs and theres a great chance you
might be bombarded with them on the interview day. Ive compiled 10
programs that are very common and has been asked numerous times in
the interviews. Most importantly if you practice these programs you get
to revise the whole syllabus. I tried to include as many concepts as
possible keeping in mind the difficulty level matched the level of the interview questions. If you dont
get any of these questions in the interview feel free to sue this websites admin..
Powered By
//Sortingwithoutusingtemporaryvariables
#include
#include
intmain(){
inta,b;
printf("Beforeswapping:\n");
printf("Entervalueofa:");
scanf("%d",&a);
printf("Entervalueofb:");
scanf("%d",&b);
//Sortingusing
a=a^b;//usesxoroperator
b=a^b;//toswapthevalues
a=a^b;//ofaandb
printf("Afterswapping:\n");
printf("Valueofa:%d\nValueofb:%d",a,b);
getch();
}
Follow up questions:
Follow
+1
+ 34
Like 865peoplelikethis.
RecommendonGoogle
Follow@swashata
1,312followers
Enteryouremail
Subscribe
1.
RSSFeed
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
GooglePlus
1/11
1/7/2015
CProgramsfortechnicalinterviews
DownloadFreebies
2.
Recent Projects
Often you might make a very simple mistake which the interviewer was actually looking for. Here well
WP Simple Event Management Plugin by iTg Manage college fests and events
1
2
3
4
5
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
//Printingpattern
#include
#include
intmain(){
intn,i,j;
printf("Enternooflines:");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<i;j++){//forprintingspaces
printf("");
}
for(j=0;j<ni;j++){//forprintingtheleftside
printf("%c",'A'+j);//thevalueofjisaddedto'A'(asciivalue=65)
}
for(j=ni2;j>=0;j){//forprintingtherightside
printf("%c",'A'+j);
}
printf("\n");
}
getch();
}
plugin by iTg
?
Wordpress Stuff
Add dynamic Login Logout n Site Admin button
to Wordpress using WP API
The concept behind making an Interactive
Wordpress Custom Page
Follow up questions:
1.
2.
i. The function must have a recursive definition or in simple words can be expressed in its own form.
ii. There is a terminating value for which we know the return value of the function.
We have to make a function fibonacci in a recursive form. Well make a function fibonacci() and make
them follow the above rules:
i. fibonacci(x) = fibonacci(x-1) + fibonacci(x-2) (recursive definition)
ii. if x==0 or x==1 then return 1 (terminating step)
Now we are good to write the code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
#include
#include
inty;
fibonacci(intx){
if(x==1||x==0)//terminatingstep
returnx;
y=fibonacci(x1)+fibonacci(x2);//recursivedefinition
returny;
}
intmain(){
inta,r;
printf("Entertheposition:");
scanf("%d",&a);
r=fibonacci(a);
printf("Thenumberatposition%dis%d",a,r);
getch();
return0;
}
Follow up questions:
1.
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
2/11
1/7/2015
CProgramsfortechnicalinterviews
n digits and the sum of the nth power of its digits is the number itself then the number is an
Armstrong number. Surely you got confused. Here are some examples to help you out.
214
Like
4
Tweet
98
1.
2.
Dont scratch your head if you cant do it yourself. The code is given below. Feel free to peek.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
#include
voidcheckArmstrong(inttemp){
intsum=0,remainder,num=temp,noOfDigits=0;
while(temp!=0){//countsnoofdigits
noOfDigits++;
temp=temp/10;
}
temp=num;
while(temp!=0){//calculatesthesumofthedigits
remainder=temp%10;//tothepowernoOfDigits
sum=sum+pow(remainder,noOfDigits);
temp=temp/10;
}
if(num==sum)//checksifthenumberisanarmstrongno.ornot
printf("%disanarmstrongnumber.",num);
else
printf("%disnotanarmstrongnumber.",num);
}
intmain(){
intn;
printf("Enteranumber:");
scanf("%d",&n);
checkArmstrong(n);
getch();
return0;
}
Navigate (TOC)
1.
2.
1.
2. What if we pass
number to the fibo
1. Is 0 an Armstron
number?
2. How many 2 dig
numbers are Arms
numbers?
3. Why aren't there
follow up question
program?
1.
2. What if we don't
end of the string?
Follow up Questions:
1. Is 0 an Armstrong number?
1. What is malloc?
2. What are the oth
functions used in d
memory allocation
allocation?
3. Why using a link
do this program be
option?
1. Can we write of
without using recu
We are so used to using strcat() when it comes to concatenate two strings but how can we do that
without using strcat()? To answer that you have to know what is string in C and how it is printed.
Strings in C are represented by character arrays. The end of the string is marked by a special character
known as the null character(). So to concatenate two strings you just have to add the characters of the
second character array after the first and place a null character at the end.
The code is as follows:
Hadoop,BigDataSession
knowbigdata.com
FreeIntroductiontoHadoop&BigDataon7Jan.SignUpNow!
01
02
03
04
05
06
07
08
09
10
11
#include
#include
#include
voidconcatenate(chara[],charb[]){
charc[strlen(a)+strlen(b)];//sizeofcissumofaandb
inti=0,j=0;
while(i<strlen(a))//addsthefirststringtoc
c[i++]=a[i];
while(j<strlen(b))//addsthesecondstringtoc
c[i++]=b[j++];
c[i]='\0';//finallyaddthenullcharacter
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
3/11
1/7/2015
CProgramsfortechnicalinterviews
12
13
14
15
16
17
18
19
20
21
22
23
24
printf("Afterconcatenation:\n");
printf("Value=%s",c);
}
intmain(){
chara[30],b[30];
printf("Enterthefirststring:");
gets(a);
printf("Enterthesecondstring:");
gets(b);
concatenate(a,b);
getch();
return0;
}
Follow up Questions:
1.
#include
?
#include
intmain()
{
intarr[20],n,i,j,pos,temp;
printf("Enternumberofelements(max20):");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enterno%d:",i+1);
scanf("%d",(arr+i));//(arr+i)isthepointertotheindividualelementsofthearray
}
for(i=0;i<(n1);i++){//selectionsortingisdone
pos=i;
for(j=i+1;j<n;j++){if(*(arr+pos)>*(arr+j))//checksifarr[pos]isgreaterthanarr[j]
pos=j;
}
if(pos!=i){//ifvalueofposchangestheswappingisdone
temp=*(arr+i);
*(arr+i)=*(arr+pos);
*(arr+pos)=temp;
}
}
printf("Sortedlistinascendingorder:\n");
for(i=0;i<n;i++)
printf("%d",*(arr+i));
getch();
return0;
}
Follow up Questions:
1. What is the bubble sort technique?
#include
#include
typedefstruct{//structureofemp
charname[30];
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
4/11
1/7/2015
CProgramsfortechnicalinterviews
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
intage;
floatsalary;
}emp;
intmain(){
intn,i;
emp*employee;
printf("Enternoofemployees:");
scanf("%d",&n);
employee=(emp*)malloc(n*sizeof(emp));//dynamicmemoryallocationusingmalloc()
for(i=0;i<n;i++){
printf("\n\nEnterdetailsofemployee%d\n",i+1);
printf("Entername:");
scanf("%s",employee[i].name);
printf("Enterage:");
scanf("%d",&employee[i].age);
printf("Entersalary:");
scanf("%f",&employee[i].salary);
}
printf("\nPrintingdetailsofalltheemployees:\n");
for(i=0;i<n;i++){
printf("\n\nDetailsofemployee%d\n",i+1);
printf("\nName:%s",employee[i].name);
printf("\nAge:%d",employee[i].age);
printf("\nSalary:%.2f",employee[i].salary);
}
getch();
return0;
}
Follow up Questions:
1. What is malloc?
2.
3.
#include
#include
voidtowers(intn,charfrompeg,chartopeg,charauxpeg){
//Ifonly1disk,makethemoveandreturn
if(n==1){
printf("\nMovedisk1frompeg%ctopeg%c",frompeg,topeg);
return;
}
//Movetopn1disksfromAtoB,usingCasauxiliary
towers(n1,frompeg,auxpeg,topeg);
//MoveremainingdisksfromAtoC
printf("\nMovedisk%dfrompeg%ctopeg%c",n,frompeg,topeg);
//Moven1disksfromBtoCusingAasauxiliary
towers(n1,auxpeg,topeg,frompeg);
}
intmain(){
intn;
printf("Enterthenumberofdisks:");
scanf("%d",&n);
printf("TheTowerofHanoiinvolvesthemoves:\n\n");
towers(n,'A','C','B');
getch();
return0;
}
Follow up Question:
1. Can we write of Hanoi without using recursion?
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
5/11
1/7/2015
CProgramsfortechnicalinterviews
using
Command
line
Questions on command line arguments are very often asked in the interviews. This program will
explain most of the details about command line arguments. The main() must have two parameters- int
argc and char *argv[]. argc is the argument count and argv[] will store the arguments passed by the
user. argv[0] is the name of the program. In this program well have to add all the arguments passed
starting from the argv[1] to argv[argc].
The code is given below:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include
#include
#include
intmain(intargc,char*argv[])
{
intsum=0,i;
//Compareifpropernumberofargumentshavebeenentered
if(argc<3)
{
printf("Insufficientnumberofarguments...\n");
getch();
return0;
}
//Addallthenumbersenteredusingatoifunction
for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}
//printthesum
printf("Ans=%d",sum);
getch();
}
Follow up Questions:
1. How can i run this program?
#include
#include
intmain(intargc,char*argv[])
{
FILE*fp;
argv[0]=strcat(argv[0],".c");//toadd.ctothenameofthefile
fp=fopen(argv[0],"r");//opensthefileinreadmode
charch;
while(!feof(fp))//tillitreachestheendoffile
{
fscanf(fp,"%c",&ch);
printf("%c",ch);
}//endwhile
getch();
return0;
}//endmain
Follow up Questions:
1. What are the modes used in fopen?
2. What is feof?
We have reached the end of this long tutorial. Hopefully you enjoyed reading this and good luck for
your campus placements. If you have any doubts please use the comments. Thank you and visit our
academic section for more tutorials. You can also find 10 Java concepts for interviews by clicking on
this link.
Related Articles
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
6/11
1/7/2015
CProgramsfortechnicalinterviews
C Program: Replacing a
substring from a string
C Program: Adding
two polynomials using
Linked List
EnterYourEmail...
Go
Search
COMMENTS
PINGS
TOP
Comments Feed
22 Responses
Comment Now
nice work some pointers on pointers ,memory management issues are very helpful for
campus-sing enthusiasts for e litmus ,netscape, tallys c rounds.well done,keep posting.
bandya says:
October 5, 2013 at 7:56 pm
ekdum kadak bhaijaanjumma ki mubarak ho aapko
reply
reply
reply
reply
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
7/11
1/7/2015
CProgramsfortechnicalinterviews
Doma says:
January 17, 2013 at 10:59 am
Hi
Can you help me in the below situation
I have an array a={1,2,3,4,-1,-1,-1}. Am considering this -1 as junk values. I have to
remove these junk values and return an array containing b={1,2,3,4}.
I have tried different ways with no help.
Please share your ideas so that it will help me.
arnab says:
January 18, 2013 at 2:03 pm
1. Find out how many non junk values are present.
2. Then use malloc to generate an array of appropriate size
3. Copy the non junk values into the array
reply
reply
Sayeed says:
January 21, 2013 at 3:59 am
reply
krishna says:
January 31, 2013 at 5:59 pm
hi arnab..can u tell me how to print anything on output screen using pointers and
without using any i/o function??
this was asked to me in an interview..a got a hint from someone that u shold put the
adress of the output in pointer and write that text in that memory.something like
thatcan u help??
arnab says:
January 31, 2013 at 7:34 pm
I didnt know the answer. I googled it for you. Check out this link
http://wiki.osdev.org/Printing_To_Screen
krishna says:
February 3, 2013 at 3:38 am
thanx a lot arnab.:)
reply
reply
reply
Durga says:
May 14, 2013 at 6:47 pm
thank u
good job
reply
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
8/11
1/7/2015
CProgramsfortechnicalinterviews
reply
roopa says:
May 17, 2013 at 9:59 am
I am M.tech fresher , looking for career in Embed domain , I am doing well in C pls send
me some interview questions to my mail id
g.roopa043@gmail.com
thank you.
harouma says:
July 9, 2014 at 1:13 pm
hi sir i want c,c++,java interview coding programs and questions also
reply
reply
Siva says:
June 19, 2013 at 9:12 pm
reply
thankss a lott.
reply
sachin says:
August 5, 2013 at 7:40 pm
reply
Good job arnab..@ i have 1 suggestion ..can u please put some quetions related to data
structures using c,like Array or linked list implementation.. that quetions which
generally asked in interview from data structures. Thanx
arnab says:
February 15, 2014 at 9:25 am
Ok Ill do that
reply
reply
Abhishek says:
February 10, 2014 at 7:36 pm
In the #7 program you should use -> operator instead of . operator since its a
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
9/11
1/7/2015
CProgramsfortechnicalinterviews
pointer.
arnab says:
February 15, 2014 at 9:24 am
Yes sure you can do that
reply
prabhu says:
July 8, 2014 at 2:42 pm
reply
jayasri says:
October 15, 2014 at 12:25 pm
reply
Leave a Reply
Enteryourcommenthere...
Popular Posts
How to: Configure Modem & LAN
settings for your BSNL Broadband
Connection
Drop In
Name
Email
Message
About Us!
8+2=
InTechgrity.com,
formerly known as
greenTechspot,
was
started in 2009 by
Swashata Ghosh who
is
an
engineering
student by the day
and a web developer by the night. His friend
Pyrotechnicpixie is the creative force behind
the blog and also contributes to the lifestyle
and web designing related posts on iTg.They
are also the founders of itgwebtoolz and
itgdesignbox. Connect with them on twitter or
Facebook
Categories
SelectCategory
Automatically insert Current Date and
Time in MySQL table #Part 2.2
Speed up BSNL Broadband using Google
DNS & Open DNS - FREE DNS
alternatives
Write a love letter in C programming
language
Archive
Privacy
Disclosure
Top
Log in
PHP, MySQL, AJAX, jQuery, CSS, XHTML, Technology and much more...
Proudly Powered by WordPress | Designed by Elegant Themes
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
10/11
1/7/2015
CProgramsfortechnicalinterviews
http://www.intechgrity.com/10cprogramsyoumustknowbeforeappearingfortechnicalinterview/#
11/11