## 11.25.2011

### Series Display,Sum

Q. Write a C program to display the given number series and calculate sum of that series.

1  2  4  7  11  16  22 . . . . . . . . .n

Ans.

/*program to display the given series and calculating sum*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
int sum=0,ser=1;
printf("Enter total elements in the series:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf(" %d",ser);
sum=sum+ser;
ser=ser+i;
}
printf("\nSum of series = %d",sum);
getch();
return 0;
}

Output:-
Enter number of elements in the series : 7

1  2  4  7  11  16  22
Sum of series = 63

## 11.16.2011

### Concatenate two strings

Q. Write a program to concatenate two strings without using the strcat() function.

Ans.

/*program to concatenate two strings without using the strcat() function*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int x=0,y=0,z=0;
char str1[20],str2[20];
printf("Enter first string : ");
gets(str1);
printf("\nEnter second string : ");
gets(str2);
while(str1[x]!=NULL)
{
x++;
z++;
}
while(str2[y]!=NULL)
{
y++;
z++;
}
getch();
return 0;
}

/***************Output******************

Enter first string : Input
Enter second string : Device
InputDevice

*****************************************/

## 11.09.2011

### Character triangle

Q.  Write a C program to print the following triangle.
 A A B A A B C B A A B C D C B A A B C D E D C B A

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
char ch,r,c;
int sp;
printf("Enter character : ");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
ch=ch-32;
for(r='A'ch>=r; r++)
{
for(sp=ch-r; sp>=1; sp--)
printf(" ");
for(c='A'r>=c; c++)
printf("%c",c);
for(c=r-1; c>='A'; c--)
printf("%c",c);
printf("\n");
}
getch();
return 0;
}

/*************** OUTPUT ******************
Enter character : E

 A A B A A B C B A A B C D C B A A B C D E D C B A
*************************************/

## 11.06.2011

Q.  Write a program in C to convert a decimal number to its hexadecimal equivalent.

Ans.

/*c program for convert decimal value to hexadecimal value*/
#include<stdio.h>
#include<conio.h>
void decTohex(int num);
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
decTohex(n);
getch();
return 0;
}
void decTohex(int num)
{
int i=0,j=0,rem[5];
for( ; num>15; num=num/16)
{
rem[i]=num%16;
i++;
j++;
}
rem[i]=num;
for(i=j; i>=0; --i)
{
if(rem[i]==10)
printf("A");
else if(rem[i]==11)
printf("B");

else if(rem[i]==12)
printf("C");

else if(rem[i]==13)
printf("D");

else if(rem[i]==14)
printf("E");

else if(rem[i]==15)
printf("F");

else
printf("%d",rem[i]);
}
}

/*****************OUTPUT****************

Enter number : 1785
6F9

**************************************/

## 11.03.2011

### Algorithm

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed.

Rules for constructing an Algorithm

When you are going to create an algorithms, keep following point in mind as:
• Input: There should be zero or more values which are to be supplied.
• Output: At least one result is to be produced.
• Definiteness: Each step must be clear and unambiguous.
• Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.
• Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.
• Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ]
The demonstration of for loop algorithm through calculate factorial number C program/algorithm:
 Figure: Algorithm of demonstration of how to write "for loop" in algorithm factorial program

Let's understand to algorithm by example:

Q. Write algorithm to calculate the sum and average of two numbers.

Ans.

[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop
[End of procedure for calculate sum and average of two numbers]

Q. Write an algorithm to convert a decimal number into binary.

Ans.

[procedure for convert decimal to binary number]
Step1 : Start
Step3 : Set x=1
Step4 : B(x)= num MOD 2
Step5 : num=num/2
Step6 : If num is equal to 0 then goto step8
Step7 : x=x+1
Step8 : goto step3
Step9 : Print B(x)
Step10 : x=x-1
Step11 : If x is greater than 0 then goto step8
step12 : Stop
[end of procedure for convert decimal to binary number]

Related programs:

## 11.02.2011

### Decimal to Octal

Q. Write a program in C to convert a decimal number to its octal equivalent.

Ans.
Coding 4 decimal to octal#include<stdio.h>
#include<conio.h>
void convert(int num, int rem);
int main()
{
int n,m;
printf("Enter number to change in octal: ");
scanf("%d",&n);
convert(n,m);
return 0;
}
void convert(int num, int rem)
{
if(rem==0)
return;
rem=num%8;
num=num/8;
convert(num,rem);
if(rem!=0)
printf("%d",rem);
}
/******************Output************
Enter number to change in octal : 786
1422

Hence, (786)10=(1422)8
**********************************/

## 11.01.2011

### Type-cast operator

The type cast operator is very important in C. Cast operator uses in convert one data type to another data types. Type casting may be two types:
1. Implicit type cast
2. Explicit type cast
Implicit type cast

In C, implicit type cast are automatically handled by compiler i.e. when two or more data types are getting execution then the final data-type will be that data type as it is declared, i.e it is not depend on conversion of data type.
It is clear understand by example as:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
float f;
double d;
i=d*f+f*j;
}

what you think, what will be data type of i?
it is double!! No, right answer is int. You see in program that double has high priority or  precedence of float and int, so result of data type will be comes in double but when result is assign in i, it will be convert in int because i is declared as int. It is implicit type casting.

Explicit type cast

An explicit type cast is a cast that we should specify invoke with either the cast. The compiler does not automatically invoke to resolve the data type conversion.
Let's understand explicit with example:

/*A student marks of three subject as m1,m2,m3 and calculate percentage(per)*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m1=70,m2=70,m3=100,total;
float per;
total=m1+m2+m3;
per=total/300*100;
printf("%f",per);
}
output:- 0.000000

Surprise, let's explain to me, it is a common type cast mistake, look at per=total/300*100; statement. In this statement first of all total/300 will be solve so total(240) and 300 are int hence 240/300 will be produce a float value so result is 0.000000. These mistake may me solved by three ways as:

1. We mention type cast in above program as:
/*demonstration of type casting*/

#include<stdio.h>
#include<conio.h>
void main()
{
int m1=70,m2=70,m3=100,total;
float per;
total=m1+m2+m3;
per=(float)total/300*100;
printf("%f",per);
}

output:- 80.000000

2. We used total as float variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int m1=70,m2=70,m3=100;
float per,total;
total=m1+m2+m3;
per=total/300*100;
printf("%f",per);
}

output:- 80.000000

3. we convert int to float to the 300 by adding decimal portion.
#include<stdio.h>
#include<conio.h>
void main()
{
int m1=70,m2=70,m3=100,total;
float per;
total=m1+m2+m3;
per=total/300.0*100;
printf("%f",per);
}

output:- 80.000000

Q. Write a program in C to display the following output:
 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 2 1 2 1

Ans.
/*c program for quadrilateral number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num=4,r,c,z,sp;
for(r=1; num>=r; r++)
{
for(sp=num-r; sp>=1; sp--)
printf(" ");
for(c=r; c>=1; c--)
printf("%d",c);
for(z=2; z<=r; z++)
printf("%d",z);
printf("\n");
}
for(r=1; num>=r; r++)
{
for(sp=r; sp>=1; sp--)
printf(" ");
for(c=num-r; c>=1; c--)
printf("%d",c);
for(z=2; z<=num-r; z++)
printf("%d",z);
printf("\n");
}
getch();
return 0;
}

/******************Output*****************

1

212

32123
4321234

32123

212

1

***************************************/