Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Tuesday, October 16, 2012

Program To Find Sum Of Digit


Some Basic C program frequently asked in Interview.

/* Program to find sum of digit of a number*/ 


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
while(a>0)
{
b=a%10;
s=s+b;
a=a/10;
}
printf("Sum of digit is %d",s);
getch();
}

Program To Convert a Number into Binary



Some Basic C program frequently asked in Interview.

/*Program to convert a number into Binary*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
printf("\t");                  //'\t' is for tab 
while(a>0)
{
b=a%2;
printf("%d",b);
printf("\b\b");         // '\b' is for backspace
a=a/2;
}
getch();
}

Program to check a prime number.


Some Basic C program frequently asked in Interview.

/* Program to check whether a number is prime or not*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
b=2;
while(a%b!=0)
{
b++;
}
if(a==b)
{
printf("\n%d is prime number",a);
}
else printf("\n\t%d is not prime number",a);
getch();
}

Program to Calculate power of a number


Some Basic C program frequently asked in Interview.

/* Power of a Number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter a number,and its power\t");
scanf("%d%d",&a,&b);
c=1;
while(b>=1)
{
c=c*a;
b--;
}
printf("%d",c);
getch();
}



Program To check a Number is Pallindrome & Armstrong


Some Basic C program frequently asked in Interview.

/*To check a number is pallindrome or not */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
c=a;
//the number is reversed inside the while loop.
while(a>0)
{
b=a%10;
s=(s*10)+b;
a=a/10;
}
//Here the reversed number is compared with the given number.
if(s==c)
{
printf("The no. %d is a pallindrome",c);
}
else
printf("The no. %d is not a pallindrome",c);
getch();
}




/*To check a number is armstrong or not */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,s=0;
clrscr();
printf("Enter a number:\t");
scanf("%d",&a);
c=a;
//Sum of cube of each digit of given  number is claculated inside the while loop.
while(a>0)
{
b=a%10;
s=s+b*b*b;
a=a/10;
}
//Sum is compared with the given number.
if(s==c)
{
printf("The no. %d is a armstrong no.",c);
}
else
printf("The no. %d is not a armstrong no.",c);
getch();
}

C program to replace multiple spaces with a single space


Program to replace multiple spaces between two words in a string by a single space


#include<stdio.h>

#include<conio.h>

void main()

{
char s[50];
char *str=s;
int i,j;
clrscr();
printf("Enter the String:\n");
gets(s);
for(i=0;*(str+i)!='\0';i++)
   {
   if(*(str+i)==' '&*(str+i+1)==' ')
   {

   for(j=i;*(str+j)!='\0';j++)
     {
 *(str+j)=*(str+j+1);
 }
 i--;
   }

   }
     printf("\n%s",str);
   getch();
}

Count number of words in a string


Program to count number of words in a string enter by a user 


#include<stdio.h>

#include<conio.h>

#include<string.h>
void askstring()
{
char str[50];
int i,words=0;
printf("\nEnter a string\n");
scanf("%[^\n]c",str);
//printf("\nThe String is: %s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
words++;

}
printf("\n Number of words in the stirng :%d",words+1);
}
void main()
{
clrscr();
askstring();
getch();
}

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