Wednesday, December 9, 2009

c programming questions and answers











31. SUM OF SQUARES OF THE SERIES 12+22+32+--------+n2
void main()
{
long int r;
clrscr();
printf("\nEnter the range: ");
scanf("%ld",&r);
printf("\nSum of the squares of the series is: %ld",((r*(r+1))*(2*r+1))/6);
getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+---------+n3
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);

Thursday, November 26, 2009

Write a C program to print the given alphabet pyramid -2


A B C D E F G F E D C B A
A B C D E F E D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A


#include<stdio.h>
#incldue<conio.h>
int main( )
{
int r,c,askey,sp;
clrscr( );

for( r=7; r>=1; r-- )
{
for(sp=6; sp>=r; sp--)
printf(" "); //2 spaces

askey=65;
for(c=1; c<=r; c++ )
printf("%2c", askey++ );

for(c=r-1; c>=1; c-- )
printf("%2c", --askey);

printf("\n");
}
getch();
return 0;
}

Output:-
A B C D E F G F E D C B A
A B C D E F E D C B A
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A

Write a C program to print the given alphabet pyramid.

A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
-----------------------------------

#include<stdio.h>
#include<conio.h>
int main( )
{
int r,c,askey;
clrscr( );

for( r=7; r>=1; r-- )
{
askey=65;
for(c=1; c<=r; c++ )
printf("%2c", askey++ );
    askey--; 
    for(c=r; c>=1; c-- )
printf("%2c", askey--);

printf("\n");
}
getch();
return 0;
}

Output:-
A B C D E F G G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

Note:- 
'r' and 'c' means rows and columns .
'askey' variable is for disp. values.

Friday, November 20, 2009

C question and answer with explanation









31. SUM OF SQUARES OF THE SERIES 12+22+32+--------+n2
void main()
{
long int r;
clrscr();
printf("\nEnter the range: ");
scanf("%ld",&r);
printf("\nSum of the squares of the series is: %ld",((r*(r+1))*(2*r+1))/6);
getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+---------+n3
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);
printf

C questions and answer with explanation




1. PERFECT NUMBER.
void main()
{
int n,i=1,sum=0;
clrscr();
printf("\nEnter a number:-");
scanf("%d",&n);
while(i {
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\nThe no %d is a perfect number",i);
else
printf("\nThe no %d is not a perfect number",i);
getch();
}
2. ARMSTRONG NUMBER.
void main()
{
int

C question and answer




16. PRINTING ASCII VALUE
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf("%d -> %c ",i,i);
delay(10);
}
getch();
}
17. CHECKING LEAP YEAR
void main()
{
int year;
clrscr();
printf("Enter any year->");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf

C questions with answer

Variable naming rule in c programming language:
(q) What will be output of the following program?
void main()
{
char * emp name=�raja�;
printf("%s",emp name);
getch();
}
Output: Compilation error
Explanation:
Error: Invalid variable name. Except underscore there should not be any special character in name of variable event blank space.
(q) What will be output of the following program?
void main()

C questions and answer








(51) 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) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these






Answer: (c)












.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px

SWAPPING OF STRINGS USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Swapping
of strings using c programming language




#include

int main(){


int i=0,

WAP in c to write an array into the file




#include<>stdio.h>void main(){ FILE *p; int i,a[10]; if((p=fopen("myfile.dat","wb"))==NULL) { printf("\nUnable to open file myfile.dat"); exit(1); } printf("\nEnter ten values, one value on each line\n"); for(i=0;i<10;i++) scanf("%d",&a[i]); fwrite(a,sizeof(a),1,p); fclose(p); getch();}

C program to read text from file

#includevoid main(){ char str[70]; FILE *p; clrscr(); if((p=fopen("string.txt","r"))==NULL) { printf("\nUnable t open file string.txt"); exit(1); } while(fgets(str,70,p)!=NULL) puts(str); fclose(p);}

Write a c program to write text in the file

#includevoid main(){ FILE *p; char str[70]; if((p=fopen("string.txt","w"))==NULL) { printf("\nUnable to open file string.txt"); exit(1); } else printf("\nEnter a set of strings,Press just enter key to finish\n: "); while(strlen(gets(str))>0) { fputs(str,p); fputs("\n",p); } fclose(p); getch();}

How to pass an array in function in c


#include "stdio.h"

#define N 5

void fstore1D(int a[], int a_size);

void fretrieve1D(int a[], int a_size);

void fedit1D(int a[], int a_size);

int main()

{

int a[N];

printf("Input data into the matrix:\n");

fstore1D(a, N);

fretrieve1D(a, N);

fedit1D(a, N);

fretrieve1D(a, N);

return 0;

}

void fstore1D(int a[], int n)

{

int i;

for ( i = 0; i < n; ++i )

STRING QUESTIONS AND ANSWER IN C


(1) Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?Answer:void main(){if(printf("HELLO WORLD")){}}(2)What will be output of following code?void main(){char a[5];a[0]='q';a[1]='u';a[2]='e';clrscr();printf("%s",a);getch();}Output: garbageExplanation: %s is used for string but a is not a string it is only array of character since its last character is not

C PROGRAMMING TUTORIAL



INDEXChapter 1 Memory mapChapter 2 Data typeChapter 3 Variables in c questions aChapter 4 Operators and expressionChapter 5 Control structure if else and switch caseChapter 6 Looping in c for, while, do whileChapter 7 ArrayChapter 8 PointerChapter 9 StringChapter 10 FunctionsChapter 11 C PreprocessorChapter 12 StructuresChapter 13 UnionChapter 14 File handlingChapter 15 Input

Write a c program to find out the last date of modification.






#include



#include



#include



void main(){

struct stat status;

FILE *fp;

fp=fopen("test.txt","r");

fstat(fileno(fp),&status);

clrscr();

printf("Last date of modification : %s",ctime(&status.st_ctime));

getch();



}





Explanation:



Function int fstat(char *, struct stat *) store the information of open file in

What is difference between file opening mode r+ and w+ in c?






Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of file as well it doesn�t create a new file if such file doesn�t exits while in w+ truncate the content of file as well as create a new file if such file doesn�t exists.

WRITE A C PROGRAM TO FIND SIZE OF A FILE




C program to get total file size

#include





#include



#include



void main()

{

struct stat status;

FILE *fp;

fp=fopen("test.txt","r");

fstat(fileno(fp),&status);

clrscr();

printf("Size of file : %d",status.st_size);

printf("Drive name : %c",65+status.st_dev);

getch();



}





Explanation:



Function int

WHAT IS FILE?




File is named location of stream of bits. It may be stored at singe place or different places but it represents a single stream.


DESCRIBE TURBO C COMPILER

DESCRIBE TURBO C COMPILER

HEXADECIMAL NUMBER REPERSENTATION

HEXADECIMAL NUMBER REPERSENTATION

PHYSICAL ADDRESS OF PROGRAM


Address range which can be represented in 20 bit binary data.

Difference between .exe and .com file


Difference between .com program and .exe program.

C LANGUAGE COMPILERS


Name of C programming language compiler.

STRUCTURE TUTORIAL IN C LANGUAGE

STRUCTURE TUTORIAL IN C LANGUAGE

Tuesday, September 8, 2009

PREPROCESSOR TUTORIAL IN C


INDEX
Topic 1 Good questions of preprocessorTopic 2 What is preprocessor?Topic 3 Why preprocessor in c programming language?Topic 4 List all preprocessor directives.Topic 5 Explain macro substitution directive?Topic 6 File inclusion directive or #include directive?Topic 8 # and ## operatorTopic 9 #pragmaTopic 10 #pragma inlineTopic 11 #pragma warnTopic 12 #pragma startup and #pragma

STRING TUTORIAL IN C


Topic 1 Good questions on String Topic 2 Function prototype Topic 3 Function retun type Topic 4 pascal and cdecl keyword Topic 5 Function recursion Topic 6 Passing and returning array to function Topic 7 Passing and returning structure to function Topic 8 Passing and returning

CONTROL STATEMENT IN C


Topic 1 Good questions on Control structure
Topic 2 if � else Topic 3 Nested if �else Topic 4 Hanging if Topic 5 Switch case

DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION USING C


Write a program (wap) to delete
an element at desired position from an array in c language



#include

int main(){



int a[50],i,pos,size;



printf("\nEnter
size of the array: ");



scanf("%d",&size);





printf("\nEnter
%d elements in to the array: ",size);



for(i=0;i



scanf("%d",&a[i]);





printf("\nEnter
position where to delete: ");



VARIABLES IN C


Topic 1 Good questions on Variables
Topic 2 Variable naming rulesTopic 3 L-vales and R-valesTopic 4 Escape characterTopic 5 Decimal, Octal and Hexadecimal number systemTopic 6 Global identifierTopic 7 Storage classTopic 7 Ellipsis
Index Big Index

GREATEST AMONG 3 NUMBERS USING BINARY MINUS IN C

void main(){int a,b,c;clrscr();printf("\nEnter 3 numbers: ");scanf("%d %d %d",&a,&b,&c);if(a-b>0 && a-c>0)printf("\nGreatest is a :%d",a);elseif(b-c>0)printf("\nGreatest is b :%d",b);elseprintf("\nGreatest is c :%d",c);getch();}

A 5 DIGIT NUMBER IS INPUT THROUGH THE KEY BOARD. OUTPUT IS A NEW NUMBER ADDING 1 TO EACH OF ITS DIGITS. IN C


A 5 digit number is input through the key board. Output is a new
number adding 1 to each of its digits in c programming



#include

int add(long int);



int main(){

long int num;

int add(long int);




printf("\nEnter a 5 digit number:
");


scanf("%ld",&num);


add(num);



return 0;

}

add(long int num){

long int r;

if(num){

r=num%10;


r=r+1;

PRINT GIVEN STRING FROM FIRST OCCURRENCE OF GIVEN CHARACTER USING C PROGRAM

#include#includevoid main(){ char *p; char s[20],s1[1]; clrscr(); printf("\nEnter a string: "); scanf("%[^\n]",s); fflush(stdin); printf("\nEnter character: "); gets(s1); p=strpbrk(s,s1); printf("\nThe string from the given character is: %s",p); getch();}

ASSEMBLY LANGUAGE PROGRAMMING BY C


Assembly language programming by c programming
language using asm keyword questions, answers and explanation



(1) What will be
output of following c program?





#include

int main(){





asm{



mov ax,61;



mov bx,10;



add bx,ax;



}



printf("\n%d",_BX);





return 0;



}





Output: 71



Explanation:





ax and bx is
general purpose register.

COUNT THE NUMBER OF OCCURRENCES OF ANY TWO VOWELS IN SUCCESSION IN A LINE OF TEXT USING C

#include int isvowel(char chk);int main(){ char text[1000], chk; int count; count = 0; while( (text[count] = getchar()) != '\n' ) count++; text[count] = '\0'; count = 0; while ( (chk = text[count]) != '\0' ) { if ( isvowel(chk) ) { if ( (chk = text[++count]) && isvowel(chk) ) {

Print prime numbers between 1-300 using break and continue in c




Print
prime numbers between 1-300 using break and continue in c



#include

#include

main(){

int i, j;

i = 2;

while ( i < 300 ){

j = 2;

while ( j < sqrt(i) ){

if ( i % j == 0 )

break;

else{

++j;

continue;

}

}

if ( j > sqrt(i) )

printf("%d\t", i);

++i;

C PROGRAM TO CALCULATE AREA OF A CIRCLE





C
program for area of circle



#include

#define PI 3.141

int main(){

float r, a;

printf("Radius: ");

scanf("%f", &r);

a = PI * r * r;

printf("%f\n", a);

return 0;

}



Mathematical
formula for area of circle:

























Here
Pie is constant which is equal to



Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...







FILE HANDLING TUTORIAL IN C





What is file?


What is stream?


What is FILE pointer?


What is buffer?


How can we know size and drive where file has stored of any given file?


Find out the last date of modification of any given file?


How can we know read/write permission any given file?


How can we know, given file is regular file, character special or it is directory?


What is difference between file opening

MEMORY MAP TUTORIAL IN C


1.Good questions on Memory map
knowledge before staring c programming language
2. List the five c compiler?3. Describe turbo c compiler?4. What is hexadecimal number system?5. What will be address range which can be represented in 20 bit?6. What is difference between TSR and TSO program?7. Why there are so many c compilers?8. What is difference between .com program and .exe program?9. How many

DATA TYPE TUTORIAL IN C




Topic 1 Types of data Topic 2 What is qualifier or modifier Topic 3 How can you say typedef is storage class Topic 4 Size of data type Topic 5 const and volatile modifierTopic 6 Memory repersentation of char data typeTopic 7 Endianness of microprocessorTopic 8 Memory repersentation of int data typeTopic 9 Memory representation of long data typeTopic 10 Memory representation of floatTopic

DRAW THE FOLLOWING PYRAMID IN C

1
0 1
1 0 1
0 1 0 1




void main(){ int i,j; clrscr(); for(i=1;i<10;i++) { for(j=i;j>=1;j--) { printf("%d",j%2); } printf("\n"); } getch();}

AMICABLE PAIRS PROGRAM IN C


Two numbers are said to be amicable if sum of proper divisors of 1st is equal to the 2nd and vice versa. It is assumed that these two numbers shoud be distinct, e.g. 3 is equal to the sum of its proper divisors 1 and 2 but can not form an amicable pair.





#include

int main()

{

long int range, test, chk, div, sum, n1, n2;

printf("Input range to check amicable numbers: ");

COUNT NO OF OCCURRENCES OF A CHARACTER IN A STRING USING getchar() AND putchar() IN C

#includemain(){ char name[20]; int i,count=0; clrscr(); for(i=0;i<20;i++) { name[i]=getchar(); putchar(name[i]); if(name[i]=='\n') break; else if(name[i]=='a') count++; } printf("\nThe number of a's in name :%d",count); getch(); return 0;}

CONCATENATE MANY FILES AND STORE THEM IN A FILE IN C


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Concatenate
many files and store them in a file in c programming language




#include

C PROGRAM FOR INSERTION SORT


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Source code of simple insertion
sort implementation using array in ascending order in c

c questions paper

c questions paper

Monday, August 3, 2009

Write a C program to print all Armstrong numbers between 1 to 1000.

#include(stdio.h)
#include(conio.h)
int main( )
{
int no, temp, rem, sum;
clrscr( );
printf("Armstrong numbers between 1 and 1000 are:\n");
for(no=1; no<=1000; no++)
{
temp=no;
sum=0;
while(temp>0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp=temp/10;
}
if(no==sum)
printf("\n%d", no);
}
getch( );
return 0;
}

Output:
Armstrong numbers between 1 and 1000 are:
1
153
370
371
407

Thursday, July 30, 2009

Write a C program to delete an element from the array.

#include< stdio.h>
#include< conio.h>
int main( )
{
int a[20], len, i, n, item, j;
clrscr( );
printf("Enter how many elements you want to enter:");
scanf("%d", &len);

printf("\nEnter the array elements:");
for(i=0; i<'len' ; i++) // remove the single quotes in for loop
scanf("%d", &a[ i]);

printf("\nEnter the location, where you want to delete:");
scanf("%d", &n);

item= a[n-1];
printf("\nDeleted value is : %d", item);

for( j= n-1; j<'len-1; j++)

{ a[ j]= a[ j+1]; }

len=len-1;
printf("\nThe new element list :");
for(i=0; i<'len; i++)

printf("%5d", a[i]);

getch( );
return 0;
}

Output:-
Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40

Wednesday, July 29, 2009

Write a C program to Insert an element at the 'n' th position

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], len, i, j, x, n ;
clrscr( );
printf("Enter the array length:");
scanf("%d", &len);
printf("\nEnter the array elements:");
for(i=0; i<'len'; i++) //remove the single quotes in for loop
scanf("%d", &a[i]);
printf("\nEnter the 'n' th position:");
scanf("%d", &n);
printf("\nEnter the array element you want to insert:");
scanf("%d", &x);
for( j= len; j>=n; j--)
{ a[ j+1]= a[ j]; }
a[ j]= x;
len=len+1;
printf("\nThe New List is :\n");
for( i=0; i<'len'; i++) //remove the single quotes in for loop
printf("%5d", a[i] );
getch( );
return 0;
}
Output:-
Enter the array length : 4
Enter the array elements: 5
8
1
9
Enter the 'n' position: 4
Enter the array element you want to insert: 10
The new List is:
5
8
1
10
9
Note:- Here 'x' is for to store the 'n' position value

Write a C program to print all Combinations of characters A, B, C

#include(stdio.h)
#include(conio.h)
int main( )
{
char ch1, ch2, ch3;
clrscr( );
for(ch1='A' ; ch1<='C' ; ++ch1)
{
for(ch2='A' ; ch2<='C' ; ++ch2)
for(ch3='A' ; ch3<='C' ; ++ch3)
printf(" %c %c %c", ch1, ch2, ch3);
}
getch( );
return 0;
}

Output:-
AAA AAB AAC ABA ABB ABC ACA ACB ACC
BAA BAB BAC BBA BBB BBC BCA BCB BCC
CAA CAB CAC CBA CBB CBC CCA CCB CCC

Write a C Program to find HCF (GCD) between two numbers using Goto statement

#include< stdio.h>
#include< conio.h>
int main( )
{
int a, b, temp, rem,
clrscr( );
printf("Enter the required two input values:");
scanf(" %d%d",&a, &b);
if(a==0)
{ printf("\nHCF of number is : %d", b );
goto last;
}
if(b==0)
{ printf("\nHCF of number is : %d", a );
goto last;
}

// Here i'm taking a empty 'for' loop
// means no initial, final and incre/decrement

for( ; ; )
{
rem=a %b;
if(rem==0) break;
a= b;
b= rem;
}
printf("\nHCF of Number is : %d", b);

last:
getch( );
return 0;
}

Output:-
Enter the required two input values: 49
47
HCF of number is: 1

Write a C program to find the roots of a Quadratic equation

#include(stdio.h) //note use '<' & '>' in place of '(' & ')'
#include(conio.h)
#include(math.h)
int main( )
{
int a , b, c, d;
float x1, x2;
ab:
printf("Enter the value of a, b, c:");
scanf("%d%d%d", &a, &b, &c);
if(a==0)
{
printf("\nIt is a Linear Equation");
prinft("\nRe-Enter the data again.");
goto ab;
}
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\nRoots are real and equal");
x1= x2=(-b)/(2*a);
printf("\nRoots are: %f \t %f ", x1, x2);
}
else
{
if(d<0)
printf("\nRoots are imaginary");
else
{
x1=((-b)+sqrt(d))/(2*a);
x2=((-b)-sqrt(d))/(2*a);
printf("\nRoots are real");
printf("\nRoots are : %f \t %f", x1, x2);
}
}
getch( );
}
Output:-
Enter the values of a, b,c : 5
2
4
Roots are imaginary.

Write a C program to Convert any Decimal number into Binary coded decimal

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], n, ct=0, rem , i=0;
clrscr( );
printf("Enter the required decimal number:");
scanf("%d", &n);
printf("\nBinary Number is :");
while(n>=1)
{
rem= n%2;
a[i]= rem;
i++; // to increase the array location
ct++; // to count the no. of bits storing in an array
n=n/2;
}
for(i=ct ; i>=1; i-- )
printf("%3d", a[i] );
getch( );
return 0;
}

Output:
Enter the required decimal number: 5
Binary Number is : 1 0 1

Write a C program to Calculate the sum of the series sum=1+1/x+1/x2+1/x3+...........+1/xn

#include(stdio.h)
#include(conio.h)
#include(math.h)
int main( )
{
int i, x, n;
float sum, p;
clrscr( );
printf("Enter the base value 'x':");
scanf("%d", &x);
printf("\nEnter the power value 'n':");
scanf(" %d", &n);
sum=1;
for( i=1; i<=n ; i++)
{
p=pow(x, i);
sum=sum+(1/p);
}
printf("\nSum of the series: %f", sum);
getch( );
return 0;
}

Note:- if u have any douts regarding this program, please feel free to contact.

Tuesday, July 21, 2009

Write a c program to obtain the Sum of the first and last digit of the number, if a four digit number is inputted through the keyboard

/* Program to obtain the Sum of first and last digit of the number without using loops*/
#include(stdio.h)
#include(conio.h)
int main( )
{
int no, sum, first, last;
clrscr( );
printf("Enter the required four digit number");
scanf("%d", &no);
first = no/1000;
last= no%10;
sum= first + last;
printf("\n Sum of first and last digit : %d", sum);
getch( );
return 0;
}

Monday, July 20, 2009

Write a C program to convert Fahrenheit degrees into Centigrade and Vice-Versa

#include(stdio.h)
#include(conio.h)
int main( )
{
float f, c;
clrscr( );
printf("Enter the fahrenheit degrees:");
scanf("%f", &f);
c= (f-32)/1.8 ;
printf("\nAfter Fahrenheit degrees, Centigrade Degrees = %f", c);
f= 1.8*c + 32.0;
printf("\nAfter Centigrade degrees, Fahrenheit degrees = %f", f);
getch( );
return 0;
}

Tuesday, July 14, 2009

Write a C program to check whether the given number is positive, negative or zero

#include(stdio.h) // place '><' in place of '(' & ')'
#include(conio.h)
int main( )
{
int no;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
if(no>0)
printf("\n %d is positive number",no);
else if(no<0)
printf("\n%d is negative number",no);
else
printf("\nEntered Number is ZERO");
getch( );
return 0;
}

Note:- If u have any doubts regarding this program or my website programs, just contact through my email-id.

Friday, June 19, 2009

Write a C program to find the greatest number between four numbers using if-else ladder

#include(stdio.h)
#include(conio.h)
int main( )
{
int a, b, c, d;
clrscr( );
printf("Enter the four different numbers:")l
scanf(" %d %d %d %d", &a, &b, &c, &d);
if(a>b && a>c && a>d)
printf("\n%d is greatest", a);
else if(b>a && b>c && b>d)
printf("\n%d is greatest", b);
else if(c>a && c>b && c>d)
printf("\n%d is greatest", c);
else
printf("\n %d is greatest", d);
getch( );
}
Note:- if u have any doubts regarding this program, feel free to contact my email id

Tuesday, June 16, 2009

Write a 'C' program to perform the selected arithmetic operation by taking two integer values using Switch Statement

#include(stdio.h)
#include(conio.h)
int main( )
{
int x,y;
char ch;
clrscr( );
printf("Enter the two integer values:);
scanf("%d%d",&x,&y);
printf("\nEnter the required arithmetic operator(+,-,*,/):");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '+' : printf("\nAddition: %d", x+y); break;
case '-' : printf("\nSubstraction : %d", x-y); break;
case '*' : printf("\nMultiplication ; %d', x*y); break;
case '/': printf("\nDivision: %d", x/y); break;
default: printf("\nInvalid Arithmetic operator.");
}
getch( );
return 0;
}
Output:- Enter the two integer values: 30
20
Enter the required arithmetic operator(+,-,*,/): +
Addition: 50

Thursday, June 11, 2009

Write a Program to print a Histogram showing the frequencies of different word length

"stdio.h"
"ctype.h"
"string.h" // used header files
int display(int );
void main( )
{
char st[200];
int i, count;
clrscr( );
printf("Enter the required sentence( Press Ctrl + Z to stop the sentence:");
for(i=0; (st[i]=getchar())!=EOF; i++);
printf("\n\n<---------------------Histogram fequencies--------------->");
printf("\n\n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ..................");
for(i=0, count=0; ; i++)
{
if(st[i]==EOF)
break;
if(isspace(st[i])
count=display(count);
else
count++;
}
count= display(count);
printf("\n<----------------------------End---------------------->");
getch( );
}
int display(int ct)
{
int i;
printf("\n");
for(i=0; i<(ct*2); i++) // (ct*2) is applied becoz 1 alpha= 2 ascii syb
{
putchar(177);
}
printf("(%d)",ct);
return 0;
}

Friday, May 22, 2009

Write a C program to find the factors of a given integer.

#include(stdio.h>
#include(conio.h)
void main( )
{
int no,x;
clrscr( );
printf("Enter the required number:");
scanf("%d",&no);
printf("\nThe factors are:");
for(x=1; x<=no; x++)
{
if(no%x==0)
printf("\n%d",x);
}
getch( );
}

Output:-
Enter the required number: 27
The factors are:
1
3
9
27

Sunday, February 22, 2009

Write a C program to generate the Fibonocci series using Recursion.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fibno(int); // function declaration.
void main( )
{
int ct, no, disp;
clrscr( );
printf("Enter the no. of terms:");
scanf("%d", &no);
printf("\n The Fibonocci series:\n");
for( ct=0; ct<=n-1; ct++) {
disp= fibno(ct); //calling function.
printf("%5d", disp);
}
getch( );
}

int fibno( int n)
{
int x, y;
if(n==0)
return 0;
else if(n==1)
return 1;
else
{
x= fibno( n-1);
y= fibno( n-2);
return (x+y);
}
}

Here the 'main' function variables
'ct' is to count the no. of values displayed in output.
'no' is for total no.of terms.
'disp' is for display the fibonocci numbers.

Here the loop 'ct<= n-1' displays the values when 'ct' equals to 'n-1'.
Suppose, Let us assume n=10 (ie., no. of terms )
Then the loop terminates, when ct==9.

Output:- Enter the no. of terms: 10

The Fibonocci series:
0 1 1 2 3 5 8 13 21 34

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Friday, February 20, 2009

Write a C program to determine the given integer is Palindrome or not.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, rev=0, temp;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
temp=no;
while(temp>0)
{
rem=temp%10;
rev=(10*rev)+rem;
temp=temp/10;
}
if(no==rev)
printf("\n%d is Palindrome number", no);
else
printf("\n%d is not a palindrome string", no);
getch();
}

Output:-
Enter the required number: 141
141 is palindrome number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to calculate the sum of factors of a number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, sum=0, x;
clrscr( );
printf("Enter the required number :");
scanf("%d", &no);
for( x=1; x<=no; x++) {
if(no%x==0) sum=sum+x;
}
printf("\nSum of the factors of %d is: %d", no, sum);
getch( );
}

Output:-
Enter the required number: 10

Sum of the factors of 10 is: 18.
ie., 1+2+5+10 =18

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Monday, February 16, 2009

Write a C program to detect whether the given integer is Armstrong number or not.

Armstrong number:- Sum of the cubes of a individual number is known as Armstrong number.

Example:- Suppose take 153,
153= 1 + 125 + 27
ie., 153 = 1 cube + 5 cube + 3 cube
153 = 153, Then we say '153' is Armstrong number

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, sum=0, n, temp;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
temp= n;
while(n>0)
{
rem=n%10;
sum = sum + (rem * rem * rem);
n=n/10;
}
if(temp == sum)
printf("\nArmstrong Number.");
else
printf("\nNot a Armstrong number.");
getch( );
}

Output:-
Enter the required number: 153
Armstrong Number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to check whether the given integer is Magic number or not.

Magic number:- A number which is divisible by 9 and also after reverse if it is divisible by 9, then it is said to Magic number.

Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, rev=0, n;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=(10*rev)+ rem;
n=n/10;
}
if(rev%9== 0)
printf("\nMagic Number.");
else
printf("\nNot a Magic number.");
getch( );
}

Output:-
Enter the required number: 27
Magic Number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to determine the given integer is Perfect number or not.

Perfect number:- Sum of the factorials of a individual digit is known as Perfect number
Ex:- 145 = 1! + 4! + 5!
=> 145 = 1 + 24 + 120
==> 145 = 145

So '145' is a Perfect number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, sum=0, temp, fact;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
temp=no;
while(no>0)
{
fact=1;
for( rem= no%10 ; rem>=1; rem--) { fact=fact*rem; }
sum = sum+ fact;
no = no/10;
}
if(temp == sum)
printf("\n%d is Perfect number", temp);
else
printf("\n%d is not a Perfect number", temp);
getch();
}

Output:-
Enter the required number: 145

145 is Perfect number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Friday, February 13, 2009

Write a C program to check whether the given integer Prime number or not.

Method: 1

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, flag=0, x=2;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no/2) {
if(no%x==0)
{
flag=1;
break;
}
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}

Output:-
Enter the required number: 23
Prime number.

Method: 2 // Prime number or not

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int no, ct=0 , x=1;
clrscr( );
printf("Enter the required number to check:");
scanf("%d", &no);
while(x<=no)
{

if(no%x==0)
{ ct++; }
x++;
}
if(flag==0)
printf("\nPrime number.");
else
printf("\nNot a Prime number.");
getch( );
return o;
}

Output:-
Enter the required number: 21
Not a Prime number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Wednesday, February 11, 2009

Write a C program to solve the Towers of Hanoi problem using Recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(math.h)
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}

Output:-
Enter the number of disks you want to play with: 3

The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Monday, February 9, 2009

Write a C program to find the GCD (greatest common divisor) of two given integers using Recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
res= gcd(a, b); // calling function.
printf("\nGCD of %d and %d is: %d", a, b, res);
getch( );
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Saturday, February 7, 2009

Write a C program to find the GCD (greatest common divisor) of two given integers without using recusive function

(a). Without using Recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
printf("\nGiven numbers are: %d and %d", a, b);
gcd(a, b); // calling function.
getch( );
}
void gcd( int x, int y) //called function.
{
int z;
for( ; ; ) //This is empty for loop.
{
z= x%y;
if( z==0) break;
x=y;
y=z;
}
printf("\nThe GCD is: %d", y);
}

Note:- Take the values as 23 and 3 , then GCd is 3.
This logic works, when a>b. (ie., 23>3 )
I'm not mentioned the logic when b>a. ( for that, we need if-else statement)

Explanation:-
Here i'm taking empty for loop becoz i dont have initial value , condition and increment or decrement.
This for loop will ends, when 'z' becomes zero. ie.,(z==0)

Write a C program to delete n Characters from a given position in a given string.

//without using functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
clrscr();
printf("Enter the string:");
gets(st);
printf("\nEntre the index position:");
scanf("%d",&pos);
printf("\nEnter the no.of characters:");
scanf("%d",&n);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
//to delete the 'n' char and after restore the temp in st.
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}

st[i]='\0';
}
}
printf("\n\nAfter deletion the string: %s",st);
}
else
printf("\nSorry, we cannot delete from that position.");
getch();
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C program to insert a sub-string in to given main string from a given position.

//Without using Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main( )
{
char st[20],sub[10],temp[10];
int pos, i, j;
clrscr( );
printf("Enter the main string:");
gets(st);
printf("\nEnter the substring to insert:");
gets(sub);
printf("Enter the index position:");
scanf("%d",&pos);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' to 'temp' from given position.
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.
{
st[i]=sub[j];
i++;
}
for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.
{
st[i]=temp[j];
i++;
}
st[i]='\0';
}}
printf("\nAfter adding the sub-string: %s",st);
}
else
printf("\nSorry, it is not possible to insert a substring in that position.");
getch();
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Thursday, February 5, 2009

Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+����.+xn

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(math.h)
int main( )
{
int x, n, sum,power;
clrscr( );
printf("Enter the values of x and n:");
scanf("%d%d", &x, &n);
if(n<0 style="font-weight: bold; color: rgb(255, 0, 0);">
{
printf("\nSorry, the formula does not make sense for negative exponents & values");
printf("Enter the values of x and n:");
scanf("%d%d", &x, &n);
sum=1;
for(power=1; power <=n; power++) {
sum=sum+ pow(x,power); }
printf("X value is: %d \nN value is: %d", x, n);
printf("\nSum of the given geometric progression: %d", sum);
}
else
{
sum=1;
for(power=1; power <=n; power++) {
sum=sum+ pow(x,power); }
printf("X value is: %d \nN value is: %d", x, n);
printf("\nSum of the given geometric progression: %d", sum);
}
getch( );
return 0;
}

Output:-
case 1: Suppose x=5, n=3
X value is: 5
N value is: 3
Sum of the given geometric progression: 156
ie., 1+ 5 + 25 + 125 = 156

case 2: Suppose n=-2 or x=-5 then the following message will be displays and it ask for another new values,

Sorry, the formula does not make sense for negative exponents & values.
Enter the values of x and n:5
3
X value is: 5
N value is: 3
Sum of the given geometric progression: 156

Write C programs that use both recursive and non-recursive functions to find the factorial of a given integer.

(i). to find the factorial of a given integer
(a).without using recursive function
.


#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
int fact(int n)
{
int ft,
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}

Output:- 4 Factorial is : 24

(a).using recursive function.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}

int fact(int n)
{
int ft,
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}

Output:- 4 Factorial is : 24

Write a C program to construct a pyramid of numbers.

/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");
scanf("%d", &len);
for( r=1; r< =len; r++ ) {
printf("\n");
for(c=1 ; c<=r; c++) {
printf("%2d", num);
num++; }
}
getch( );
return 0;
}
Explanation:-
Here 'r' indicates row,
'c' indicates column and 'len' indicates length of the pyramid.
And lastly 'num=1' for generating pyramid of numbers.
This 'c<=r' is required becoz when row is incremented, column value is also incremented.
Note:- If U have any doubts regarding this program, contact with me through my email-id.

Friday, January 30, 2009

Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
char st[10], t[10];
int i, flag=0, count=0, j, pos;
clrscr();
printf("Enter the main string:");
gets(st);
printf("Enter the sub string:");
gets(t);
for(i=0; st[i]!='\0'; i++)
{
if(st[i]==t[0])
{
pos=i;
for(j=0; t[j]!='\0'; j++)
{
if(st[i]==t[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1 && count==strlen(t))
printf("\nPosition: %d, And T is a substring of S", pos+1);
else if(flag==1 && count < strlen(t))
printf("\nPosition: %d, And T is not a substring of S", pos+1);
else
printf("\nIndex: -1");
getch();
}