Tuesday, October 16, 2012

Frequently asked C programs

Some Basics C program frequently asked in interviews

1.Find factorial of a number using recursive function.


#include<stdio.h>

#include<conio.h>

int fact(int);
void main()
{
int a,b;
printf("Enter no.");
scanf("%d",&a);
clrscr();
b=fact(a);
printf("%d",b);
getch();
}
int fact(int x)
{
int r=1;
if(x==1) return 1;
else r=x*fact(x-1);
return r;
}

2. Getting a multiple word string having spaces from user using scanf function.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
printf("Enter name:\n");
scanf("%[^\n]s",&name);              /* notice the format specifier used here i.e 
%[^\n]s

*/
printf("%s",name);                          
getch();
}

No comments: