You are on page 1of 54

Code 1:

1. C program to check perfect number


#include<stdio.h>
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language
#include<stdio.h>
int main(){
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;

}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6
Code 3:
3. C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
int n,i,sum;
printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
Output:
Perfect numbers are: 6 28

Definition of perfect number or What is perfect number?


Perfect number is a positive number which sum of all positive divisors excluding
that number is equal to that number. For example 6 is perfect number since divi
sor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
--------------------------------------------------------------------------------------------Code 1:
1. Warp to check a number is Armstrong
2. C program to check whether a number is Armstrong or not
3. Simple c program for Armstrong number
4. Armstrong number in c with output

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
Sample output:
Enter a number: 153
153 is an Armstrong number
The time complexity of a program that determines Armstrong number is: O (Number
of digits)
Code 2:
1. Write a c program for Armstrong number
2. C program for Armstrong number generation
3. How to find Armstrong number in c
4. Code for Armstrong number in c
#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Armstrong numbers in given range are: ");
for(num=min;num<=max;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;

}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 200
Armstrong numbers in given range are: 1 153
Code 3:
1. Armstrong number in c using for loop
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
Sample output:
Enter a number: 370
370 is an Armstrong number
Logic of Armstrong number in c
Code 4:
1. C program to print Armstrong numbers from 1 to 500
2. C program for finding Armstrong numbers
#include<stdio.h>
int main(){
int num,r,sum,temp;
for(num=1;num<=500;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
Output:
1 153 370 371 407

Code 1:
1. Write a c program to reverse a given number
2. C program to find reverse of a number
3. C program to reverse the digits of a number
4. Reverse of a number in c using while loop
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21
Code 2:
1. Reverse very large or big numbers beyond the range of long int
2. Reverse five digit number c program
Logic is we accept the number as string
#include<stdio.h>
#define MAX 1000
int main(){
char num[MAX];
int i=0,j,flag=0;
printf("Enter any positive integer: ");
scanf("%s",num);
while(num[i]){
if(num[i] < 48 || num[i] > 57){
printf("Invalid integer number");
return 0;
}

i++;
}
printf("Reverse: ");
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
printf("%c",num[j]);
flag =1;
}
return 0;
Sample output:
Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432
Code 3:
1. C program to reverse a number using for loop
2. How to find reverse of a number in c
3. Wap to reverse a number in c
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
reverse=reverse*10+r;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 123
Reversed of number: 321
Code 4:
1. C program to reverse a number using recursion
#include<stdio.h>
int main(){
int num,reverse;
printf("Enter any number: ");
scanf("%d",&num);
reverse=rev(num);
printf("Reverse of number: %d",reverse);
return 0;
}
int rev(int num){

static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else
return 0;
return sum;
}
Sample output:
Enter any number: 456
Reverse of number: 654
------------------------------------------------------------------------------------------------Code
1. C
2. C
3. C

1:
program to add digits of a number
program for sum of digits of a number
program to calculate sum of digits

#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:
Enter a number: 123
Sum of digits of number: 6
Code 2:
1. Sum of digits of a number in c using for loop
#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:

Enter a number: 567


Sum of digits of number: 18
Code 3:
1. Sum of digits in c using recursion
#include<stdio.h>
int getSum(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = getSum(num);
printf("Sum of digits of number: %d",sum);
return 0;
}
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum;
}
Sample output:
Enter a number: 45
Sum of digits of number: 9
-------------------------------------------------------------------------------How to calculate power of a number in c
How to write power in c
#include<stdio.h>
int main(){
int pow,num,i=1;
long int sum=1;
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow){
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
return 0;
}

-------------------------------------------------------------------------------Add two numbers in c without using operator


How to add two numbers without using the plus operator in c
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}

Sample output:
Enter any two integers: 5 10
Sum of two integers: 15
Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b
1 + 1
= a + b
------------------------------------------------------------------------------------------------------Write a c program or code to subtract two numbers without using subtraction oper
ator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}
Sample Output:
Enter any two integers: 5 4
Difference of two integers: 1
---------------------------------------------------------------------------------------#include<stdio.h>
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a-b>0 && a-c>0)
printf("\nGreatest is a :%d",a);
else
if(b-c>0)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
return 0;
}

-------------------------------------------------------------------------------Write a c program to find largest among three numbers using conditional operator
#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is: %d",big);
return 0;
}

If you have any suggestions on above c program to find largest or biggest of 3 n


umbers, please share us.
------------------------------------------------------------------------------------IND OUT GENERIC ROOT OF A NUMBER By C PROGRAM

C program for generic root


#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is: %ld",sum);
return 0;
}
C code for calculation of generic root in one line
#include <stdio.h>
int main(){
int num,x;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d",(x=num%9)?x:9);
return 0;
}
Sample output:
Enter any number: 731
Generic root: 2
Meaning of generic root:
It sum of digits of a number unit we don't get a single digit. For example:
Generic root of 456: 4 + 5 + 6 = 15 since 15 is two digit numbers so 1 + 5 = 6
So, generic root of 456 = 6
-----------------------------------------------------------------------------Prime factor of a number in c
#include<stdio.h>
int main(){
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){

if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
return 0;
}
------------------------------------------------------------------------------rite a c program to find out NCR factor of given number Or
C program to find the ncr value by using recursive function
#include<stdio.h>
int main(){
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n--;
}
return i;
}
Algorithm:
In the mathematics nCr has defined as
nCr = n! /((n-r)!r!)
------------------------------------------------------------------------------------------------How to convert string to int without using library functions in c programming la
nguage
#include<stdio.h>
int stringToInt(char[] );
int main(){
char str[10];
int intValue;
printf("Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;

}
int stringToInt(char str[]){
int i=0,sum=0;
while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}
Sample output:
Enter any integer as a string: 123
Equivalent integer value: 123
-----------------------------------------------------------------------#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}
Output:
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

-----------------------------------------------------------------------------Code for swapping in c


#include<stdio.h>

int main(){
int a,b,temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
C program for swapping of two numbers using pointers
#include<stdio.h>
int main(){
int a,b;
int *ptra,*ptrb;
int *temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
ptra = &a;
ptrb = &b;
temp = ptra;
*ptra = *ptrb;
*ptrb = *temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
Sample output:
Enter any two integers: 5 10
Before swapping: a = 5, b=10
After swapping: a = 10, b=10
Swapping program in c using function
#include<stdio.h>
void swap(int *,int *);
int main(){
int a,b;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);

swap(&a,&b);
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
void swap(int *a,int *b){
int *temp;
temp = a;
*a=*b;
*b=*temp;
}
Sample output:
Enter any two integers: 3 6
Before swapping: a = 3, b=6
After swapping: a = 6, b=6
---------------------------------------------------------------------------------------Simple program of c find the largest number
#include<stdio.h>
int main(){
int n,num,i;
int big;
printf("Enter the values of n: ");
scanf("%d",&n);
printf("Number %d",1);
scanf("%d",&big);
for(i=2;i<=n;i++){
printf("Number %d: ",i);
scanf("%d",&num);
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
Sample Output:
Enter the values of n:
Number 1: 12
Number 2: 32
Number 3: 35
Largest number is: 35
========================================================================
Extract digits from integer in c language
#include<stdio.h>
int main(){

int num,temp,factor=1;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: ");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
return 0;
}
Sample output:
Enter a number: 123
Each digits of given number are: 1 2 3
================================================================================
=
Code 1:
Count the number of digits in c programming language
#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
num=num/10;
count++;
}
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 23
Total digits is: 2
Code 2:
C code to count the total number of digit using for loop
#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);

for(;num!=0;num=num/10)
count++;
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 456
Total digits is: 3
Code 3:
Count the digits of a given number in c language using recursion
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count;
}
Sample output:
Enter a number: 1234567
Total digits is: 7
================================================================================
==
Swapping in c without temporary variable
Swap 2 numbers without using third variable in c
How to swap two numbers in c without using third variable
#include<stdio.h>
int main(){
int a=5,b=10;

//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}
=====================================================
Write a c program for swapping of two arrays
#include<stdio.h>
int main(){
int a[10],b[10],c[10],i;
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];

a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
return 0;
}
=======================================================================
Swapping of strings using c programming language
#include<stdio.h>
int main(){
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0'){
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0'){
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("After swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}
===========================================================================
LCM program in c with two numbers :
#include<stdio.h>
int main(){
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);

x=n1,y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}
LCM program in c with two numbers (Other logic) :

#include<stdio.h>
int lcm(int,int);
int main(){

int a,b,l;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
printf("LCM of two integers is %d",l);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
LCM program in c with multiple numbers :

#include<stdio.h>
int lcm(int,int);

int main(){
int a,b=1;
printf("Enter positive integers. To quit press zero.");
while(1){
scanf("%d",&a);
if(a<1)
break;
else if(a>b)
b = lcm(a,b);
else
b = lcm(b,a);
}
printf("LCM is %d",b);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}

================================================================================
============
Definition of HCF (Highest common factor):
HFC is also called greatest common divisor (gcd). HCF of two numbers is a larges
t positive numbers which can divide both numbers without any remainder. For exa
mple HCF of two numbers 4 and 8 is 2 since 2 is the largest positive number whic
h can dived 4 as well as 8 without a remainder.
Logic of HCF or GCD of any two numbers:
In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10
Logic for writing program:
It is clear that any number is not divisible by greater than number itself. In c
ase of more than one numbers, a possible maximum number which can divide all of

the numbers must be minimum of all of that numbers.


For example: 10, 20, and 30
Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop
which will start form min of the numbers and will stop the loop when it became o
ne, since all numbers are divisible by one. Inside for loop we will write one if
conditions which will check divisibility of both the numbers.
Program:
Write a c program for finding gcd (greatest common divisor) of two given numbers
#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}
Other logic : HCF (Highest common factor) program with two numbers in c

#include<stdio.h>
int main(){
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2){
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
return 0;
}
HCF program with multiple numbers in c

#include<stdio.h>
int main(){
int x,y=-1;
printf("Insert numbers. To exit insert zero: ");
while(1){
scanf("%d",&x);
if(x<1)
break;
else if(y==-1)
y=x;
else if (x<y)
y=gcd(x,y);
else
y=gcd(y,x);
}
printf("GCD is %d",y);
return 0;
}
int gcd(int x,int y){
int i;
for(i=x;i>=1;i--){
if(x%i==0&&y%i==0){
break;
}
}
return i;
}
================================================================================
======
Decimal to binary conversion in c programming language. C source code for decima
l to binary conversion:
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;

quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}
Sample output:
Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010
Algorithm:
Binary number system: It is base 2 number system which uses the digits from 0 an
d 1.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Convert from decimal to binary algorithm:
Following steps describe how to convert decimal to binary
Step 1: Divide the original decimal number by 2
Step 2: Divide the quotient by 2
Step 3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Decimal to binary conversion with example:
For example we want to convert decimal number 25 in the binary.
Step
Step
Step
Step
Step

1: 25 / 2 Remainder :
2: 12 / 2 Remainder :
3: 6 / 2 Remainder :
4: 3 / 2 Remainder :
5: 1 / 2 Remainder :

1
0
0
1
1

,
,
,
,
,

Quotient
Quotient
Quotient
Quotient
Quotient

:
:
:
:
:

12
6
3
1
0

So equivalent binary number is: 11001


That is (25)10 = (11001)2
================================================================================
==================
C code for decimal to octal converter

#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int octalNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
octalNumber[i++]= quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",octalNumber[j]);
return 0;
}
Sample output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62
2. Easy way to convert decimal number to octal number in c
#include<stdio.h>
int main(){
long int decimalNumber;
printf("Enter any decimal number : ");
scanf("%d",&decimalNumber);
printf("Equivalent octal number is: %o",decimalNumber);
return 0;
}
Sample output:
Enter any decimal number: 25
Equivalent octal number is: 31
Octal number system: It is base 8 number system which uses the digits from 0 to
7.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Decimal to octal conversion method:
Step 1: Divide the original decimal number by 8
Step 2: Divide the quotient by 8

Step3: Repeat the step 2 until we get quotient equal to zero.


Result octal number would be remainders of each step in the reverse order.
Decimal to octal conversion with example:
For example we want to convert decimal number 525 in the octal.
Step
Step
Step
Step

1: 525 / 8 Remainder
2: 65 / 8 Remainder
3:
8 / 8 Remainder
4:
1 / 8 Remainder

:
:
:
:

5
1
0
1

,
,
,
,

Quotient
Quotient
Quotient
Quotient

:
:
:
:

65
8
1
0

So equivalent octal number is: 1015


That is (525)10 = (1015)8
================================================================================
========================
1. C code to convert decimal to hexadecimal
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);
return 0;
}
Sample output:
Enter any decimal number: 45
Equivalent hexadecimal value of decimal number 45: 2D
2. Easy way to convert decimal number to hexadecimal number:
#include<stdio.h>
int main(){

long int decimalNumber;


printf("Enter any decimal number: ");
scanf("%d",&decimalNumber);
printf("Equivalent hexadecimal number is: %X",decimalNumber);
return 0;
}
Sample output:
Enter any decimal number: 45
Equivalent hexadecimal number is: 2D
Hexadecimal number system: It is base 16 number system which uses the digits fro
m 0 to 9 and A, B, C, D, E, F.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Decimal to hexadecimal conversion method:
Following steps describe how to convert decimal to hexadecimal
Step 1: Divide the original decimal number by 16
Step 2: Divide the quotient by 16
Step 3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Decimal to hexadecimal conversion example:
For example we want to convert decimal number 900 in the hexadecimal.
Step 1: 900 / 16 Remainder : 4 , Quotient : 56
Step 2: 56 / 16 Remainder : 8 , Quotient : 3
Step 3:
3 / 16 Remainder : 3 , Quotient : 0
So equivalent hexadecimal number is: 384
That is (900)10 = (384)16
================================================================================
==================================
C program to convert binary to octal
#include<stdio.h>
#define MAX 1000
int main(){
char octalNumber[MAX];
long int i=0;
printf("Enter any octal number: ");
scanf("%s",octalNumber);

printf("Equivalent binary value: ");


while(octalNumber[i]){
switch(octalNumber[i]){
case '0': printf("000"); break;
case '1': printf("001"); break;
case '2': printf("010"); break;
case '3': printf("011"); break;
case '4': printf("100"); break;
case '5': printf("101"); break;
case '6': printf("110"); break;
case '7': printf("111"); break;
default: printf("\nInvalid octal digit %c ",octalNumber[i]); retur
n 0;
}
i++;
}
return 0;
}
Sample output:
Enter any octal number: 123
Equivalent binary value: 001010011
Algorithm:
Octal to binary conversion method:
To convert or change the octal number to binary number replace the each octal di
gits by a binary number using octal to binary chart.
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111
Octal to binary table
Octal to binary conversion examples:
For example we want to convert or change octal number 65201 to decimal. For this
we will replace each octal digit to binary values using the above table:
Octal number: 6 5 2 0 1
Binary values: 110 101 010 000 001
So (65201)8 = (110101010000001)2

================================================================================
====
code to convert octal number to decimal number
#include<stdio.h>
#include<math.h>
int main(){
long int octal,decimal =0;
int i=0;
printf("Enter any octal number: ");
scanf("%ld",&octal);
while(octal!=0){
decimal = decimal + (octal % 10) * pow(8,i++);
octal = octal/10;
}
printf("Equivalent decimal value: %ld",decimal);
return 0;
}
Sample output:
Enter any octal number: 346
Equivalent decimal value: 230
C program to change octal to decimal
#include<stdio.h>
int main(){
long int octalNumber;
printf("Enter any octal number: ");
scanf("%o",&octalNumber);
printf("Equivalent decimal number is: %d",octalNumber);
return 0;
}
Sample output:
Enter any octal number: 17
Equivalent decimal number is: 15
Algorithm:
Octal to decimal conversion method:
To convert an octal number to decimal number multiply each digits separately of
octal number from right side by 8^0,8^1,8^2,8^3
respectively and then find out
the sum of them.
Octal to decimal conversion examples:

For example we want to convert octal number 3401 to decimal number.


Step
Step
Step
Step

1:
2:
3:
4:

1
0
4
3

*
*
*
*

8
8
8
8

^0
^1
^2
^3

=
=
=
=

1*1 =1
0*8 =0
4*64 =256
3*512 =1536

Sum= 1 + 0 + 256 + 1536 = 1793


So, (3401)8 = (1793)10
================================================================================
========
C program c program to convert binary to octal
#include<stdio.h>
int main(){
long int binaryNumber,octalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
octalNumber=octalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent octal value: %lo",octalNumber);
return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15
C code for how to convert large binary to octal
#include<stdio.h>
#define MAX 1000
int main(){
char binaryNumber[MAX],octalNumber[MAX];
long int i=0,j=0;
printf("Enter any number any binary number: ");
scanf("%s",binaryNumber);
while(binaryNumber[i]){
binaryNumber[i] = binaryNumber[i] -48;
++i;
}

--i;
while(i-2>=0){
octalNumber[j++] = binaryNumber[i-2] *4 + binaryNumber[i-1] *2 + binaryNumb
er[i] ;
i=i-3;
}
if(i ==1)
octalNumber[j] = binaryNumber[i-1] *2 + binaryNumber[i] ;
else if(i==0)
octalNumber[j] = binaryNumber[i] ;
else
--j;
printf("Equivalent octal value: ");
while(j>=0){
printf("%d",octalNumber[j--]);
}
return 0;
}
Sample output:
Enter any number any binary number: 1111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
11111111
Equivalent octal value: 3777777777777777777777777777777
7777777777777777777777777777777777777777777777777777
Alogrithm:
Binary to octal conversion method:
Step1: Arrange the binary number in the group 3 from right side.
Step 2: Replace the each group with following values:
Binary number
Octal values
000
0
001
1
010
2
011
3
100
4
101
5
110
6
111
7

Binary to octal chart


Binary to octal conversion examples:
For example we want to convert binary number 1011010101001101 to octal.
Step 1: 001 011 010 101 001 101
Step 2: 1 3 2 5 1 5
So (1011010101001101)2 = (132515)8
================================================================================
====
C code for binary to decimal conversion:

#include<stdio.h>
int main(){
long int binaryNumber,decimalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent decimal value: %ld",decimalNumber);
return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent decimal value: 13
Algorithm:
Binary number system: It is base 2 number system which uses the digits from 0 an
d 1.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Convert from binary to decimal algorithm:

For this we multiply each digit separately from right side by 1, 2, 4, 8, 16


pectively then add them.

res

Binary number to decimal conversion with example:


For example
Step1: 1 *
Step2: 1 *
Step3: 1 *
Step4: 1 *
Step5: 0 *
Step6: 1 *

we want to convert binary number 101111 to decimal:


1 = 1
2 = 2
4 = 4
8 = 8
16 = 0
32 = 32

Its decimal value: 1 + 2 + 4+ 8+ 0+ 32 = 47


That is (101111)2 = (47)10
===================================================================
C code for sum of two binary numbers:
#include<stdio.h>
int main(){
long int binary1,binary2;
int i=0,remainder = 0,sum[20];
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while(i>=0)
printf("%d",sum[i--]);
return 0;
}
Sample output:
Enter any first binary number: 1100011
Enter any second binary number: 1101
Sum of two binary numbers: 1110000
Alogrithm:
Rule of binary addition:
0 + 0 = 0
1 + 0 = 1

0 + 1 = 1
1 + 1 = 1 and carry = 1
Q1. What is the sum of the binary numbers 1101 and 1110?
Answer: 1101 + 1110 = 11011
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
vvv
#include<stdio.h>
int binaryAddition(int,int);
int main(){
long int binary1,binary2,multiply=0;
int digit,factor=1;
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary2!=0){
digit = binary2 %10;
if(digit ==1){
binary1=binary1*factor;
multiply = binaryAddition(binary1,multiply);
}
else
binary1=binary1*factor;
binary2 = binary2/10;
factor = 10;
}
printf("Product of two binary numbers: %ld",multiply);
return 0;
}
int binaryAddition(int binary1,int binary2){
int i=0,remainder = 0,sum[20];
int binarySum=0;
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
while(i>=0)
binarySum = binarySum*10 + sum[i--];
return binarySum;

}
Sample output:
Enter any first binary number: 1101
Enter any second binary number: 11
Product of two binary numbers: 100111
Algorithm:
Rule of binary multiplication:
0
1
0
1

*
*
*
*

0
0
1
1

=
=
=
=

0
0
0
1

Q. what is the product of the binary numbers 0110 and 0011?


Answer: 0110 * 0011 = 10010
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ccccccccccccccccccc

Tricky c questions and answers

Tricky c programs question for interview and answers with explanation. These que
stions are for experienced persons.
C advanced interview questions and answers
(1) What will be output if you will compile and execute the following c code?
struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf("%d %d %d",s.p,s.c,s.m);
}
(a)
(b)
(c)
(d)
(e)

2 -6 5
2 -6 1
2 2 1
Compiler error
None of these

Answer: (c)
Explanation:
Binary value
Binary value
Binary value
(Select last

of 2: 00000010 (Select three two bit)


of 6: 00000110
of -6: 11111001+1=11111010
three bit)

Binary value of 5: 00000101 (Select last two bit)


Complete memory representation:
(2) What will be output if you will compile and execute the following c code?
void main(){
int huge*p=(int huge*)0XC0563331;
int huge*q=(int huge*)0xC2551341;
*p=200;
printf("%d",*q);
}
(a)0
(b)Garbage value
(c)null
(d) 200
(e)Compiler error
Answer: (d)
Explanation:
Physical address of huge pointer p
Huge address: 0XC0563331
Offset address: 0x3331
Segment address: 0XC056
Physical address= Segment address * 0X10 + Offset address
=0XC056 * 0X10 +0X3331
=0XC0560 + 0X3331
=0XC3891
Physical address of huge pointer q
Huge address: 0XC2551341
Offset address: 0x1341
Segment address: 0XC255
Physical address= Segment address * 0X10 + Offset address
=0XC255 * 0X10 +0X1341
=0XC2550 + 0X1341
=0XC3891
Since both huge pointers p and q are pointing same physical address so content o
f q will also same as content of q.
(3) Write c program which display mouse pointer and position of pointer.(In x co
ordinate, y coordinate)?
Answer:
#include dos.h
#include stdio.h
void main()
{
union REGS i,o;
int x,y,k;
//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit key in the key board
{
i.x.ax=3; //get mouse position
x=o.x.cx;
y=o.x.dx;
clrscr();

printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
getch();
}
(4) Write a c program to create dos command: dir.
Answer:
Step 1: Write following code.
#include stdio.h
#include dos.h
void main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
}
Step 2: Save the as list.c (You can give any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of Window XP operating system and select prop
erties.
Step 5: Select Advanced -> Environment Variables
Step 6: You will find following window:
Click on new button (Button inside the red box)

Step 7: Write following:


Variable name: path
Variable value: c:\tc\bin\list.c (Path where you have saved)
Step 8: Open command prompt and write list and press enter.
Command line argument tutorial.
(6) What will be output if you will compile and execute the following c code?
void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");

else
printf("Less than");
}
(a)
(b)
(c)
(d)
(e)

Equal
Greater than
Less than
Compiler error
None of above

Answer: (d)
Explanation:
static variables are load time entity while auto variables are run time entity.
We can not initialize any load time variable by the run time variable.
In this example i is run time variable while x is load time variable.
Properties of static variables.
Properties of auto variables.
(7) What will be output if you will compile and execute the following c code?
void main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);
}
(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 -80 32
(d)102 102 -90 64
(e)Compiler error
Answer: (d)
Explanation:
In c float data type is four byte data type while char pointer ptr can point one
byte of memory at a time.
Memory representation of float a=5.2

ptr pointer will point first fourth byte then third byte then second byte then f
irst byte.
Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102
Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
Content of second byte:
Binary value=10100110
Decimal value=-128+32+4+2=-90
Content of first byte:
Binary value=01000000
Decimal value=64
Note: Character pointer treats MSB bit of each byte i.e. left most bit of above

figure as sign bit.


(8) What will be output if you will compile and execute the following c code?
void main(){
int i;
double a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=7;i++)
printf("%d ",*ptr++);
}
(a)
(b)
(c)
(d)
(e)

-51 -52 -52 -52 -52 -52 20 64


51 52 52 52 52 52 20 64
Eight garbage values.
Compiler error
None of these

Answer: (a)
Explanation:
In c double data type is eight byte data type while char pointer ptr can point o
ne byte of memory at a time.
Memory representation of double a=5.2

ptr pointer will point first eighth byte then seventh byte then sixth byte then
fifth byte then fourth byte then third byte then second byte then first byte as
shown in above figure.
Content of eighth byte:
Binary value=11001101
Decimal value= -128+64+8+4+1=-51
Content of seventh byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of sixth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of fifth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of fourth byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of third byte:
Binary value=11001100
Decimal value= -128+64+8+4=-52
Content of second byte:
Binary value=000010100
Decimal value=16+4=20
Content of first byte:
Binary value=01000000
Decimal value=64
Note: Character pointer treats MSB bit of each byte i.e. left most bit of above
figure as sign bit.
(9) What will be output if you will compile and execute the following c code?

void main(){
printf("%s","c" "question" "bank");
}
(a)
(b)
(c)
(d)
(e)

c question bank
c
bank
cquestionbank
Compiler error

Answer: (d)
Explanation:
In c string constant
String tutorial.

xy

is same as x

(10) What will be output if you will compile and execute the following c code?
void main(){
printf("%s",__DATE__);
}
(a)
(b)
(c)
(d)
(e)

Current system date


Current system date with time
null
Compiler error
None of these

Answer: (a)
Explanation:
__DATE__ is global identifier which returns current system date.
(11) What will be output if you will compile and execute the following c code?
void main(){
char *str="c-pointer";
printf("%*.*s",10,7,str);
}
(a)
(b)
(c)
(d)
(e)

c-pointer
c-pointer
c-point
cpointer null null
c-point

Answer: (e)
Explanation:
Meaning of %*.*s in the printf function:
First * indicates the width i.e. how many spaces will take to print the string a
nd second * indicates how many characters will print of any string.
Following figure illustrates output of above code:
Properties of printf function.
(12) What will be output if you will compile and execute the following c code?
void start();
void end();

#pragma startup start


#pragma exit end
int static i;
void main(){
printf("\nmain function: %d",++i);
}
void start(){
clrscr();
printf("\nstart function: %d",++i);
}
void end(){
printf("\nend function: %d",++i);
getch();
}
(a)
main function: 2
start function: 1
end function:3
(b)
start function: 1
main function: 2
end function:3
(c)
main function: 2
end function:3
start function: 1
(d) Compiler error
(e) None of these
Answer: (b)
Explanation:
Every c program start with main function and terminate with null statement. But
#pragma startup can call function just before main function and #pragma exit
What is pragma directive?
Preprocessor tutorial.
(13) What will be output if you will compile and execute the following c code?
void main(){
int a=-12;
a=a>>3;
printf("%d",a);
}
(a)
(b)
(c)
(d)
(e)

-4
-3
-2
-96
Compiler error

Answer :( c)
Explanation:
Binary value of 12 is: 00000000 00001100
Binary value of -12 wills 2 s complement of 12 i.e.

So binary value of -12 is: 11111111 11110100

Right shifting rule:


Rule 1: If number is positive the fill vacant spaces in the left side by 0.
Rule 2: If number is negative the fill vacant spaces in the left side by 1.
In this case number is negative. So right shift all the binary digits by three s
pace and fill vacant space by 1 as shown following figure:

Since it is negative number so output will also a negative number but its 2 s comp
lement.

Hence final out put will be:

And its decimal value is: 2


Hence output will be:-2
(14) What will be output if you will compile and execute the following c code?
#include "string.h"
void main(){
clrscr();
printf("%d%d",sizeof("string"),strlen("string"));
getch();
}
(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these
Answer: (d)
Explanation:
Sizeof operator returns the size of string including null character while strlen
function returns length of a string excluding null character.
(15) What will be output if you will compile and execute the following c code?
void main(){
static main;
int x;
x=call(main);
clrscr();
printf("%d ",x);
getch();
}
int call(int address){
address++;
return address;
}

(a)
(b)
(c)
(d)
(e)

0
1
Garbage value
Compiler error
None of these

Answer: (b)
Explanation:
As we know main is not keyword of c but is special type of function. Word main c
an be name variable in the main and other functions.
What is main function in c?
(16) What will be output if you will compile and execute the following c code?
void main(){
int a,b;
a=1,3,15;
b=(2,4,6);
clrscr();
printf("%d ",a+b);
getch();
}
(a) 3
(b) 21
(c) 17
(d) 7
(e) Compiler error
Answer: (d)
Explanation:
In c comma behaves as separator as well as operator.
a=1, 3, 15;
b= (2, 4, 6);
In the above two statements comma is working as operator. Comma enjoys least pre
cedence and associative is left to right.
Assigning the priority of each operator in the first statement:

Hence 1 will assign to a.


Assigning the priority of each operator in the second statement:

(17) What will be output if you will compile and execute the following c code?
int extern x;
void main()
printf("%d",x);
x=2;
getch();
}
int x=23;
(a)
(b)
(c)
(d)
(e)

0
2
23
Compiler error
None of these

Answer: (c)
Explanation:
extern variables can search the declaration of variable any where in the program
.
(18) What will be output if you will compile and execute the following c code?
void main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf("%d",i);
}
else
printf("equal");
}
(a)
(b)
(c)
(d)
(e)

5
3
1
equal
None of above

Answer: (c)
Explanation:
(19) What will be output if you will compile and execute the following c code?
void main(){
int a=25;
clrscr();
printf("%o %x",a,a);
getch();
}
(a)
(b)
(c)
(d)
(e)

25 25
025 0x25
12 42
31 19
None of these

Answer: (d)
Explanation:
%o is used to print the number in octal number format.
%x is used to print the number in hexadecimal number format.
Note: In c octal number starts with 0 and hexadecimal number starts with 0x.
(20) What will be output if you will compile and execute the following c code?
#define message "union is\
power of c"
void main(){
clrscr();
printf("%s",message);
getch();
}
(a) union is power of c
(b) union ispower of c

(c) union is
Power of c
(d) Compiler error
(e) None of these
Answer: (b)
Explanation:
If you want to write macro constant in new line the end with the character \.
(21) What will be output if you will compile and execute the following c code?
#define call(x) #x
void main(){
printf("%s",call(c/c++));
}
(a)c
(b)c++
(c)#c/c++
(d)c/c++
(e)Compiler error
Answer: (d)
Explanation:
# is string operator. It converts the macro function call argument in the string
. First see the intermediate file:
test.c 1:
test.c 2: void main(){
test.c 3: printf("%s","c/c++");
test.c 4: }
test.c 5:
It is clear macro call is replaced by its argument in the string format.
(22) What will be output if you will compile and execute the following c code?
void main(){
if(printf("cquestionbank"))
printf("I know c");
else
printf("I know c++");
}
(a)
(b)
(c)
(d)
(e)

I know c
I know c++
cquestionbankI know c
cquestionbankI know c++
Compiler error

Answer: (c)
Explanation:
Return type of printf function is integer which returns number of character it p
rints including blank spaces. So printf function inside if condition will return
13. In if condition any non- zero number means true so else part will not execu
te.
If you have any doubt in above Tricky questions with explanation you can ask thr
ough comment section.
-----------------------------------------------------------------------------------------------------------------------

Top 50 HTML Interview Questions


Download PDF Download PDF
1) What is HTML?
HTML is short for HyperText Markup Language, and is the language of the World Wi
de Web. It is the standard text formatting language used for creating and displa
ying pages on the Web. HTML documents are made up of two things: the content and
the tags that formats it for proper display on pages.
2) What are tags?
Content is placed in between HTML tags 0in order to properly format it. It makes
use of the less than symbol (<) and the greater than symbol (>). A slash symbol
is also used as a closing tag. For example:
1
<strong>sample</strong>
3) Do all HTML tags come in pair?
No, there are single HTML tags that does not need a closing tag. Examples are th
e <img> tag and <br> tags.
4) What are some of the common lists that can be used when designing a page?
You can insert any or a combination of the following list types:
ordered list
unordered list
definition list
menu list
directory list
Each of this list types makes use of a different tag set to compose
5) How do you insert a comment in html?
html interview Questions
Comments in html begins with

<!

nd ends with

> . For example:

1
<!-- A SAMPLE COMMENT -->
6) Do all character entities display properly on all systems?

No, there are some character entities that cannot be displayed when the operatin
g system that the browser is running on does not support the characters. When th
at happens, these characters are displayed as boxes.
7) What is image map?
Image map lets you link to many different web pages using a single image. You ca
n define shapes in images that you want to make part of an image mapping.
8 ) What is the advantage of collapsing white space?
White spaces are blank sequence of space characters, which is actually treated a
s a single space character in html. Because the browser collapses multiple space
into a single space, you can indent lines of text without worrying about multip
le spaces. This enables you to organize the html code into a much more readable
format.
9) Can attribute values be set to anything or are there specific values that the
y accept?
Some attribute values can be set to only predefined values. Other attributes can
accept any numerical value that represents the number of pixels for a size.
10) How do you insert a copyright symbol on a browser page?
To insert the copyright symbol, you need to type &copy; or & #169; in an HTML fi
le.
11) How do you create links to sections within the same page?
Links can be created using the <a> tag, with referencing through the use of the
number (#) symbol. For example, you can have one line as <a href= #topmost >BACK TO
TOP</a>, which would result in the words BACK TO TOP appearing on the webpage and
links to a bookmark named topmost. You then create a separate tag command like <
a name= topmost > somewhere on the top of the same webpage so that the user will be
linked to that spot when he clicked on BACK TO TOP .
12) Is there any way to keep list elements straight in an html file?
By using indents, you can keep the list elements straight. If you indent each su
bnested list in further than the parent list that contains it, you can at a glan
ce determine the various lists and the elements that it contains.
13) If you see a web address on a magazine, to which web page does it point?
Every web page on the web can have a separate web address. Most of these address
es are relative to the top-most web page. The published web address that appears
within magazines typically points this top-most page. From this top level page,
you can access all other pages within the web site.
14) What is the use of using alternative text in image mapping?
When you use image maps, it can easily become confusing and difficult to determi
ne which hotspots corresponds with which links. Using alternative text lets you
put a descriptive text on each hotspot link.
15) Do older html files work on newer browsers?
Yes, older html files are compliant to the HTML standard. Most older files work
on the newer browsers, though some features may not work.

16) Does a hyperlink apply to text only?


No, hyperlinks can be used on text as well as images. That means you can convert
an image into a link that will allow user to link to another page when clicked.
Just surround the image within the <a href=
> </a> tag combinations.
17) If the user s operating system does not support the needed character, how can
the symbol be represented?
In cases wherein their operating system does not support a particular character,
it is still possible to display that character by showing it as an image instea
d.
18) How do you change the number type in the middle of a list?
The <li> tag includes two attributes
type and value. The type attribute can be u
sed to change the numbering type for any list item. The value attribute can chan
ge the number index.
19) What are style sheets?
Style sheets enable you to build consistent, transportable, and well-defined sty
le templates. These templates can be linked to several different web pages, maki
ng it easy to maintain and change the look and feel of all the web pages within
a site.
20) What bullet types are available?
With ordered lists, you can select to use a number of different list types inclu
ding alphabetical and Roman numerals. The type attribute for unordered lists can
be set to disc, square, or circle.
21) How do you create multicolored text in a webpage?
To create text with different colors, use the <font color= color > </font> tags for ev
ery character that you want to apply a color. You can use this tag combination a
s many times as needed, surrounding a single character or an entire word.
22) Why are there both numerical and named character entity values?
The numerical values are taken from the ASCII values for the various characters,
but these can be difficult to remember. Because of this, named character entity
values were created to make it easier for web page designers to use.
23) Write a HTML table tag sequence that outputs the following:
50 pcs 100 500
10 pcs 5 50
Answer:
1
2
3
4
5
6
7
8

9
10
11
12
<table>
<tr>
<td>50 pcs</td>
<td>100</td>
<td>500</td>
</tr>
<tr>
<td>10 pcs</td>
<td>5</td>
<td>50</td>
</tr>
</table>
24) What is the advantage of grouping several checkboxes together?
Although checkboxes don t affect one another, grouping checkboxes together helps t
o organize them. Checkbox buttons can have their own name and do not need to bel
ong to a group. A single web page can have many different groups of checkboxes.
25) What will happen if you overlap sets of tags?

If two sets of html tags are overlapped, only the first tag will be recognized.
You will recognize this problem when the text does not display properly on the b
rowser screen.
26) What are applets?
Applets are small programs that can be embedded within web pages to perform some
specific functionality, such as computations, animations, and information proce
ssing. Applets are written using the Java language.
27) What if there is no text between the tags or if a text was omitted by mistak
e? Will it affect the display of the html file?
If there is no text between the tags, then there is nothing to format, so no for
matting will appear. Some tags, especially tags without a closing tag like the <
img> tag, do not require any text between them.
28) Is it possible to set specific colors for table borders?
You can specify a border color using style sheets, but the colors for a table th
at does not use style sheets will be the same as the text color.
29) How do you create a link that will connect to another web page when clicked?
To create hyperlinks, or links that connect to another web page, use the href ta
g. The general format for this is: <a href= site >text</a>
Replace site with the actual page url that is supposed to be linked to when the te
xt is clicked.
30) What other ways can be used to align images and wrap text?
Tables can be used to position text and images. Another useful way to wrap text
around an image is to use style sheets.

31) Can a single text link point to two different web pages?
No. The <a> tag can accept only a single href attribute, and it can point to onl
y a single web page.
32) What is the difference between the directory and menu lists and the unordere
d list?
The key differences is that the directory and menu lists do not include attribut
es for changing the bullet style.
33) Can you change the color of bullets?
The bullet color is always the same as that of the first character in the list l
item. If you surround the <li> and the first character with a set of <font> tags
with the color attribute set, the bullet color and the first character will be
a different color from the text.
34) What are the limits of the text field size?
The default size for a text field is around 13 characters, but if you include th
e size attribute, you can set the size value to be as low as 1. The maximum size
value will be determined by the browser width. If the size attribute is set to
0, the size will be set to the default size of 13 characters.
35) Do <th> tags always need to come at the start of a row or column?
Any <tr> tag can be changed to a <th> tag. This causes the text contained within
the <th> tag to be displayed as bold in the browser. Although <th> tags are mai
nly used for headings, they do not need to be used exclusively for headings.
36) What is the relationship between the border and rule attributes?
Default cell borders, with a thickness of 1 pixel, are automatically added betwe
en cells if the border attribute is set to a nonzero value. Likewise, If the bor
der attribute is not included, a default 1-pixel border appears if the rules att
ribute is added to the <table> tag.
37) What is a marquee?
A marquee allows you to put a scrolling text in a web page. To do this, place wh
atever text you want to appear scrolling within the <marquee> and </marquee> tag
s.
38) How do you create a text on a webpage that will allow you to send an email w
hen clicked?
To change a text into a clickable link to send email, use the mailto command wit
hin the href tag. The format is as follows:
1
<A HREF= mailto:youremailaddress >text to be clicked</A>
39) Are <br> tags the only way to separate sections of text?
No. The <br> tag is only one way to separate lines of text. Other tags, like the
<p> tag and <blockquote> tag, also separate sections of text.

40) Are there instances where text will appear outside of the browser?
By default, the text is wrapped to appear within the browser window. However, if
the text is part of a table cell with a defined width, the text could extend be
yond the browser window.
41) How are active links different from normal links?
The default color for normal and active links is blue. Some browsers recognize a
n active link when the mouse cursor is placed over that link; others recognize a
ctive links when the link has the focus. Those that don t have a mouse cursor over
that link is considered a normal link.
42) Do style sheets limit the number of new style definitions that can be includ
ed within the brackets?
Style sheets do not limit the number of style definitions that can be included w
ithin the brackets for a given selector. Every new style definition, however, mu
st be separated from the others by a semicolon symbol.
43) Can I specify fractional weight values such as 670 or 973 for font weight?
Implementation largely depends on the browser, but the standard does not support
fractional weight values. Acceptable values must end with two zeroes.
44) What is the hierarchy that is being followed when it comes to style sheets?
If a single selector includes three different style definitions, the definition
that is closest to the actual tag takes precedence. Inline style takes priority
over embedded style sheets, which takes priority over external style sheets.
45) Can several selectors with class names be grouped together?
You can define several selectors with the same style definition by separating th
em with commas. This same technique also works for selectors with class names.
46) What happens if you open the external CSS file in a browser?
If you try to open the external CSS file in a browser, the browser cannot open t
he file, because the file has a different extension. The only way to use an exte
rnal CSS file is to reference it using <link/> tag within another html document.
47) How do you make a picture into a background image of a web page?
To do this, place a tag code after the </head> tag as follows:
1
<body background = image.gif >
Replace image.gif with the name of your image file. This will take the picture a
nd make it the background image of your web page.
48) What happens if the list-style-type property is used on a non-list element l
ike a paragraph?
If the list-style-type property is used on a non-list element like a paragraph,
the property will be ignored and have no effect on the paragraph.
49) When is it appropriate to use frames?

Frames can make navigating a site much easier. If the main links to the site are
located in a frame that appears at the top or along the edge of the browser, th
e content for those links can be displayed in the remainder of the browser windo
w.
50) What happens if the number of values in the rows or cols attribute doesn t add
up to 100 percent?
The browser sizes the frames relative to the total sum of the values. If the col
s attribute is set to 100%, 200%, the browser displays two vertical frames with
the second being twice as big as the first.
-----------------------------------------------------------------------------------------------------------------------------------------------What is HTML?
Answer1:
HTML, or HyperText Markup Language, is a Universal language which allows an indi
vidual using special code to create web pages to be viewed on the Internet.
Answer2:
HTML ( H yper T ext M arkup L anguage) is the language used to write Web pages.
You are looking at a Web page right now.
You can view HTML pages in two ways:
* One view is their appearance on a Web browser, just like this page -- colors,
different text sizes, graphics.
* The other view is called "HTML Code" -- this is the code that tells the browse
r what to do.
What is a tag?
In HTML, a tag tells the browser what to do. When you write an HTML page, you en
ter tags for many reasons -- to change the appearance of text, to show a graphic
, or to make a link to another page.
What is the simplest HTML page?
HTML Code:
<HTML>
<HEAD>
<TITLE>This is my page title! </TITLE>
</HEAD>
<BODY>
This is my message to the world!
</BODY>
</HTML>
Browser Display:
This is my message to the world!
How do I create frames? What is a frameset?
Frames allow an author to divide a browser window into multiple (rectangular) re
gions. Multiple documents can be displayed in a single window, each within its o
wn frame. Graphical browsers allow these frames to be scrolled independently of
each other, and links can update the document displayed in one frame without aff
ecting the others.
You can't just "add frames" to an existing document. Rather, you must create a f
rameset document that defines a particular combination of frames, and then displ
ay your content documents inside those frames. The frameset document should also
include alternative non-framed content in a NOFRAMES element.
The HTML 4 frames model has significant design flaws that cause usability proble
ms for web users. Frames should be used only with great care.
How can I include comments in HTML?

Technically, since HTML is an SGML application, HTML uses SGML comment syntax. H
owever, the full syntax is complex, and browsers don't support it in its entiret
y anyway. Therefore, use the following simplified rule to create HTML comments t
hat both have valid syntax and work in browsers:
An HTML comment begins with "<!--", ends with "-->", and does not contain "--" o
r ">" anywhere in the comment.
The following are examples of HTML comments:
* <!-- This is a comment. -->
* <!-- This is another comment,
and it continues onto a second line. -->
* <!---->
Do not put comments inside tags (i.e., between "<" and ">") in HTML markup.
What is a Hypertext link?
A hypertext link is a special tag that links one page to another page or resourc
e. If you click the link, the browser jumps to the link's destination.
How comfortable are you with writing HTML entirely by hand?
Very. I don t usually use WYSIWYG. The only occasions when I do use Dreamweaver ar
e when I want to draw something to see what it looks like, and then I ll usually e
ither take that design and hand-modify it or build it all over again from scratc
h in code. I have actually written my own desktop HTML IDE for Windows (it s calle
d Less Than Slash) with the intention of deploying it for use in web development
training. If has built-in reference features, and will autocomplete code by par
sing the DTD you specify in the file. That is to say, the program doesn t know any
thing about HTML until after it parses the HTML DTD you specified. This should g
ive you some idea of my skill level with HTML.
What is everyone using to write HTML?
Everyone has a different preference for which tool works best for them. Keep in
mind that typically the less HTML the tool requires you to know, the worse the o
utput of the HTML. In other words, you can always do it better by hand if you ta
ke the time to learn a little HTML.
What is a DOCTYPE? Which one do I use?
According to HTML standards, each HTML document begins with a DOCTYPE declaratio
n that specifies which version of HTML the document uses. Originally, the DOCTYP
E declaration was used only by SGML-based tools like HTML validators, which need
ed to determine which version of HTML a document used (or claimed to use).
Today, many browsers use the document's DOCTYPE declaration to determine whether
to use a stricter, more standards-oriented layout mode, or to use a "quirks" la
yout mode that attempts to emulate older, buggy browsers.
Can I nest tables within tables?
Yes, a table can be embedded inside a cell in another table. Here's a simple exa
mple:
<table>
<tr>
<td>this is the first cell of the outer table</td>
<td>this is the second cell of the outer table,
with the inner table embedded in it
<table>
<tr>
<td>this is the first cell of the inner table</td>

<td>this is the second cell of the inner table</td>


</tr>
</table>
</td>
</tr>
</table>
The main caveat about nested tables is that older versions of Netscape Navigator
have problems with them if you don't explicitly close your TR, TD, and TH eleme
nts. To avoid problems, include every </tr>, </td>, and </th> tag, even though t
he HTML specifications don't require them. Also, older versions of Netscape Navi
gator have problems with tables that are nested extremely deeply (e.g., tables n
ested ten deep). To avoid problems, avoid nesting tables more than a few deep. Y
ou may be able to use the ROWSPAN and COLSPAN attributes to minimize table nesti
ng. Finally, be especially sure to validate your markup whenever you use nested
tables.
---------------------------------------------------------------------------------------------------------

You might also like