Programming question and answers in C Language - Test your C skills

C is interviewer’s favorite language. If you have good command on C language you can get through Technical Interview easily. Most of the interviewers ask basic questions in C. Some times they may ask you to write small programs like reversal of a string, determining whether the given number is a palindrome or not and finding factorial of a given number using recursion etc. You should be able to write these programs quickly.

 Most favorite question for interviewers is interchanging two variables with out using the third variable. Another favorite topic for interviewers is Pointers. Please go through pointers once before going to the interview.

 Many interviewers look for logic in your program rather than syntax. Once you write the program you should be able to explain the logic to the interviewer with the help of flow-chart or algorithm. If there are more than one logic for a single program, choose the logic that is simple and easy to understand at the same time it should efficient one.

Below are around 100 FAQ in C language. Try to answer these questions on your own. I believe that if you are able to answer these questions, you can easily get through the technical interview.

1.

What will be the output of the following code?

void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf (“%d",a[i]) ;
}
Ans: The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

-------------------------------------------------------------------------------------------------
2.

Why doesn't the following code give the desired result?

int x = 3000, y = 2000 ;
long int z = x * y ;
Ans: Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below:

long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.

-------------------------------------------------------------------------------------------------
3.

Why doesn't the following statement work?

char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
Ans: The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, "!" ) ;

-------------------------------------------------------------------------------------------------
4.

How do I know how many elements an array can hold?

Ans: The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
-------------------------------------------------------------------------------------------------

5.

How do I write code that reads data at memory location specified by segment and offset?

Ans: Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function.

#include <stdio.h>
#include <dos.h>

main( )
{

char far *scr = 0xB8000000 ;
FILE *fp ;
int offset ;
char ch ;

if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL )
{

printf ( "\nUnable to open file" ) ;
exit( ) ;

}

// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
fprintf ( fp, "%c", peekb ( scr, offset ) ) ;
fclose ( fp ) ;

if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL )
{

printf ( "\nUnable to open file" ) ;
exit( ) ;

}

// reads and writes to file
for ( offset = 0 ; offset < 160 ; offset++ )
{

fscanf ( fp, "%c", &ch ) ;
printf ( "%c", ch ) ;

}

fclose ( fp ) ;

}

6.
How do I compare character data stored at two different memory locations?

Ans: Sometimes in a program we require to compare memory ranges containing strings. In such a situation we can use functions like memcmp( ) or memicmp( ). The basic difference between two functions is that memcmp( ) does a case-sensitive comparison whereas memicmp( ) ignores case of characters. Following program illustrates the use of both the functions.

#include <mem.h>

main( )
{
char *arr1 = "Kicit" ;
char *arr2 = "kicitNagpur" ;

int c ;

c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ;

else
printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical"
) ;

c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" )
;
else
printf ( "\nStrings arr1 and arr2 compared using memicmp are not
identical" ) ;
}

-------------------------------------------------------------------------------------------------
7.
Fixed-size objects are more appropriate as compared to variable size data objects. Using variable-size data objects saves very little space. Variable size data objects usually have some overhead. Manipulation of fixed-size data objects is usually faster and easier. Use fixed size when maximum size is clearly bounded and close to average. And use variable-size data objects when a few of the data items are bigger than the average size. For example,

char *num[10] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

Instead of using the above, use

char num[10][6] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

The first form uses variable-size data objects. It allocates 10 pointers, which are pointing to 10 string constants of variable size. Assuming each pointer is of 4 bytes, it requires 90 bytes. On the other hand, the second form uses fixed size data objects. It allocates 10 arrays of 6 characters each. It requires only 60 bytes of space. So, the variable-size in this case does not offer any advantage over fixed size.
-------------------------------------------------------------------------------------------------
8.

The Spawnl( ) function...

DOS is a single tasking operating system, thus only one program runs at a time. The Spawnl( ) function provides us with the capability of starting the execution of one program from within another program. The first program is called the parent process and the second program that gets called from within the first program is called a child process. Once the second program starts execution, the first is put on hold until the second program completes execution. The first program is then restarted. The following program demonstrates use of spawnl( ) function.

/* Mult.c */

int main ( int argc, char* argv[ ] )
{
int a[3], i, ret ;
if ( argc < 3 || argc > 3 )
{
printf ( "Too many or Too few arguments..." ) ;
exit ( 0 ) ;
}

for ( i = 1 ; i < argc ; i++ )
a[i] = atoi ( argv[i] ) ;
ret = a[1] * a[2] ;
return ret ;
}

/* Spawn.c */
#include <process.h>
#include <stdio.h>

main( )
{
int val ;
val = spawnl ( P_WAIT, "C:\\Mult.exe", "3", "10",
"20", NULL ) ;
printf ( "\nReturned value is: %d", val ) ;
}

Here, there are two programs. The program 'Mult.exe' works as a child process whereas 'Spawn.exe' works as a parent process. On execution of 'Spawn.exe' it invokes 'Mult.exe' and passes the command-line arguments to it. 'Mult.exe' in turn on execution, calculates the product of 10 and 20 and returns the value to val in 'Spawn.exe'. In our call to spawnl( ) function, we have passed 6 parameters, P_WAIT as the mode of execution, path of '.exe' file to run as child process, total number of arguments to be passed to the child process, list of command line arguments and NULL. P_WAIT will cause our application to freeze execution until the child process has completed its execution. This parameter needs to be passed as the default parameter if you are working under DOS. under other operating systems that support multitasking, this parameter can be P_NOWAIT or P_OVERLAY. P_NOWAIT will cause the parent process to execute along with the child process, P_OVERLAY will load the child process on top of the parent process in the memory.
-------------------------------------------------------------------------------------------------
9.

Are the following two statements identical?

char str[6] = "Kicit" ;
char *str = "Kicit" ;
Ans: No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer on the other hand, is a reference to any data element (of a particular type) located anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned any time. The array declaration char str[6] ; requests that space for 6 characters be set aside, to be known
by name str. In other words there is a location named str at which six characters are stored. The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to be known by the name str. This pointer can point almost anywhere to any char, to any contiguous array of chars, or nowhere.
-------------------------------------------------------------------------------------------------
10.

Is the following code fragment correct?

const int x = 10 ;
int arr[x] ;
Ans: No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.

11.

How do I write code to retrieve current date and time from the system and display it as a string?

Ans: Use time( ) function to get current date and time and then ctime( ) function to display it as a string. This is shown in following code snippet.

#include <sys\types.h>

void main( )
{
time_t curtime ;
char ctm[50] ;

time ( &curtime ) ; //retrieves current time &
stores in curtime
printf ( "\nCurrent Date & Time: %s", ctime (
&curtime ) ) ;
}

-------------------------------------------------------------------------------------------------
12.

How do I change the type of cursor and hide a cursor?

Ans: We can change the cursor type by using function _setcursortype( ). This function can change the cursor type to solid cursor and can even hide a cursor. Following code shows how to change the cursor type and hide cursor.

#include <conio.h>
main( )
{
/* Hide cursor */
_setcursortype ( _NOCURSOR ) ;

/* Change cursor to a solid cursor */
_setcursortype ( _SOLIDCURSOR ) ;

/* Change back to the normal cursor */
_setcursortype ( _NORMALCURSOR ) ;
}
-------------------------------------------------------------------------------------------------
13.

How do I write code that would get error number and display error message if any standard error occurs?

Ans: Following code demonstrates this.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

main( )
{
char *errmsg ;
FILE *fp ;
fp = fopen ( "C:\file.txt", "r" ) ;
if ( fp == NULL )
{
errmsg = strerror ( errno ) ;
printf ( "\n%s", errmsg ) ;
}
}
Here, we are trying to open 'file.txt' file. However, if the file does not exist, then it would cause an error. As a result, a value (in this case 2) related to the error generated would get set in errno. errno is an external int variable declared in 'stdlib.h' and also in 'errno.h'. Next, we have called sterror( ) function which takes an error number and returns a pointer to standard error message related to the given error number.
-------------------------------------------------------------------------------------------------
14.

How do I write code to get the current drive as well as set the current drive?

Ans: The function getdisk( ) returns the drive number of current drive. The drive number 0 indicates 'A' as the current drive, 1 as 'B' and so on. The Setdisk( ) function sets the current drive. This function takes one argument which is an integer indicating the drive to be set. Following program demonstrates use of both the functions.

#include <dir.h>

main( )
{
int dno, maxdr ;

dno = getdisk( ) ;
printf ( "\nThe current drive is: %c\n", 65 + dno
) ;

maxdr = setdisk ( 3 ) ;
dno = getdisk( ) ;
printf ( "\nNow the current drive is: %c\n", 65 +
dno ) ;
}
-------------------------------------------------------------------------------------------------
15.

The functions memcmp( ) and memicmp( )

The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of memory or strings. However, memcmp( ) performs comparison as unsigned chars whereas memicmp( ) performs comparison as chars but ignores case (i.e. upper or lower case). Both the functions return an integer value where 0 indicates that two memory buffers compared are identical. If the value returned is greater than 0 then it indicates that the first buffer is bigger than the second one. The value less than 0 indicate that the first buffer is less than the second buffer. The following code snippet demonstrates use of both

#include <stdio.h>
#include <mem.h>

main( )
{
char str1[] = "This string contains some
characters" ;
char str2[] = "this string contains" ;
int result ;

result = memcmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using
memcmp( )" ) ;
show ( result ) ;

result = memicmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using
memicmp( )" ) ;
show ( result ) ;
}

show ( int r )
{
if ( r == 0 )
printf ( "\nThe buffer str1 and str2 hold
identical data" ) ;
if ( r > 0 )
printf ( "\nThe buffer str1 is bigger than buffer
str2" ) ;
if ( r < 0 )
printf ( "\nThe buffer str1 is less than buffer
str2" ) ;
}
-------------------------------------------------------------------------------------------------

16.

How do I write code to find an amount of free disk space available on current drive?

Ans: Use getdfree( ) function as shown in follow code.

#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <dos.h>

main( )
{
int dr ; struct dfree disk ;
long freesp ;

dr = getdisk( ) ;
getdfree ( dr + 1 , &disk ) ;

if ( disk.df_sclus == 0xFFFF )
{
printf ( "\ngetdfree( ) function failed\n");
exit ( 1 ) ;
}

freesp = ( long ) disk.df_avail
* ( long ) disk.df_bsec
* ( long ) disk.df_sclus ;
printf ( "\nThe current drive %c: has %ld bytes
available as free space\n", 'A' + dr, freesp ) ;
}

17.

Use of array indices...
If we wish to store a character in a char variable ch and the character to be stored depends on the value of another variable say color (of type int), then the code would be as shown below:

switch ( color )
{
case 0 :
ch = 'R' ;
break ;
case 1 :
ch = 'G' ;
break ;
case 2 :
ch = 'B' ;
break ;
}
In place of switch-case we can make use of the value in color as an index for a character array. How to do this is shown in following code snippet.

char *str = "RGB' ;
char ch ;
int color ;
// code
ch = str[ color ] ;
-------------------------------------------------------------------------------------------------
18.

Function atexit( ) recevies parameter as the address of function of the type void fun ( void ). The function whose address is passed to atexit( ) gets called before the termination of program. If atexit( ) is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.

#include <stdio.h>
#include <stdlib.h>
void fun1( )
{
printf("Inside fun1\n");
}

void fun2( )
{
printf("Inside fun2\n");
}
main( )
{
atexit ( fun1 ) ;
/* some code */
atexit ( fun2 ) ;
printf ( "This is the last statement of
program?\n" );
}

-------------------------------------------------------------------------------------------------

19.

How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?

Ans: The function is as shown below:

Compress ( char str1[], char str2[] )
{
int i, j, k ;

for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ )
{
for ( j = 0 ; str2[j] != ‘\0’ && str2[j] !=
str1[i] ; j++ )
;
if ( str2[j] == ‘\0’ )
str1[k++] = str1[I] ;
}
str1[k] = ‘\0’
}
-------------------------------------------------------------------------------------------------

20.

How does free( ) know how many bytes to free?

Ans: The malloc( ) / free( ) implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing.
-------------------------------------------------------------------------------------------------
21.

What is the use of randomize( ) and srand( ) function?

Ans: While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.

-------------------------------------------------------------------------------------------------
22.

How do I determine amount of memory currently available for allocating?

Ans: We can use function coreleft( ) to get the amount of memory available for allocation. However, this function does not give an exact amount of unused memory. If, we are using a small memory model, coreleft( ) returns the amount of unused memory between the top of the heap and stack. If we are using a larger model, this function returns the amount of memory between the highest allocated memory and the end of conventional memory. The function returns amount of memory in terms of bytes.

-------------------------------------------------------------------------------------------------
23.

How does a C program come to know about command line arguments?

Ans: When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file table, environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.


Comments (54)

dinesh
Said this on 11-3-2009 At 05:28 pm

question papers

raj
Said this on 6-29-2010 At 10:23 am

hi send me qustion of c

 

mpraveen
Said this on 11-4-2009 At 10:24 am

hi this s praveen kumar. i need c faq's for my interviews.

pawan kumar
Said this on 11-4-2009 At 07:01 pm

superb,

This knowledge is not only for interview prepration it is also for

sharp the basic knowledge of c.

Narendran
Said this on 12-16-2009 At 05:26 pm

Really it's an more useful artilce

Said this on 12-23-2009 At 07:15 pm

Really, excellent.Tnx u had given a chance to improve the c-language.we are all tnxful to u.

shefali
Said this on 1-4-2010 At 04:26 pm

its really useful to improve ones C skill and also ables to enhance ones knowledge........

amin
Said this on 3-19-2010 At 01:36 pm

hi

I want allocate an array for an image in C but I cant do it i know some information about read_image functions but not completely please guid me  thanks alot.

dabeer
Said this on 3-23-2010 At 11:45 am

simply superb, even i was not knowing about these functions.

really awsome

jeff
Said this on 3-29-2010 At 02:17 pm

i need a while statement program that displays:

*****

 ****

  ***

   **

    *

   **

  ***

 ****

*****

Said this on 5-10-2011 At 10:41 am

hi jeff...... this s Ranjith...

the followin code s the ans for ur qustn ..

void main()
{
 int i,j,k,m,n;
 clrscr();
 printf("Enter the Limit:\t");
 scanf("%d",&m);
 n=m;
 while(n>0)
 {
  k=n;
  while(k>0)
  {
   printf("*");
   k--;
  }
  printf("\n");
  n--;

 }
 i=0;
 while(i<m)
 {
  j=0;
  while(j<=i)
  {
   printf("*");
   j++;
  }
  i++;
  printf("\n");
 }

 getch();
}

Reyaz wani
Said this on 4-10-2010 At 07:18 pm

this is good for

understand' c 'language

snehali
Said this on 5-5-2010 At 02:57 pm

really  fantastic.....thanks

Nitesh
Said this on 7-2-2010 At 11:03 pm

These are very good questions and the best thing is that, that u have provided answers for each problem....

it's really good....

rajesh
Said this on 7-18-2010 At 08:42 pm

hello sir/ madam

iam fresh btech ece graduate iam going attend cdac entrance

so i need "c aptitude bits" to prepare

pls send me 'c ' aptitude bits

                            rajesh

admin
Said this on 7-19-2010 At 04:36 am
UMESH KESHRI
Said this on 8-21-2010 At 03:58 pm

I am MCA student of Alard Institute of Management Science,Pune

Realy some Questions r good but some advance skill i want 2 know.

Subhash Patil
Said this on 9-9-2010 At 11:55 am

Superb....site...I was just never came across some of these functions though I was handling C language..it really helps to enhance ur C skills..head off to the persone who designed this course.....

If u could pelase send me C aptitude test paper..to my gmail id

Pratik
Said this on 9-12-2010 At 08:05 pm

i want to know how to print the nos including 0 to 9, from 4 digits to 8 digits???? i mean to say nos between 4 to 8 digits including 0 to 9 nos.

please reply me on kukku_ricky@yahoo.co.in

Said this on 9-14-2010 At 10:51 am
I am MCA student in kota
Realy some Questions r good but some advance skill i want 2 know.
Said this on 9-18-2010 At 10:42 am
#include <mem.h>

main( )
{
char *arr1 = "Kicit" ;
char *arr2 = "kicitNagpur" ;

int c ;

c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ;

else
printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical"
) ;

c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" )
;
else
printf ( "\nStrings arr1 and arr2 compared using memicmp are not
identical" ) ;
}


Read more: http://www.ittestpapers.com/articles/programming-question-and-answers-in-c-language---test-your-c-skills.html#ixzz0zr3ltVMp

Said this on 9-18-2010 At 10:47 am
16.

How do I write code to find an amount of free disk space available on current drive?

Ans: Use getdfree( ) function as shown in follow code.

#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <dos.h>

main( )
{
int dr ; struct dfree disk ;
long freesp ;

dr = getdisk( ) ;
getdfree ( dr + 1 , &disk ) ;

if ( disk.df_sclus == 0xFFFF )
{
printf ( "\ngetdfree( ) function failed\n");
exit ( 1 ) ;
}

freesp = ( long ) disk.df_avail
* ( long ) disk.df_bsec
* ( long ) disk.df_sclus ;
printf ( "\nThe current drive %c: has %ld bytes
available as free space\n", 'A' + dr, freesp ) ;
}


Read more: http://www.ittestpapers.com/articles/programming-question-and-answers-in-c-language---test-your-c-skills.html#ixzz0zr5QvE6v
Amarish
Said this on 10-23-2010 At 02:59 pm

It's d good bt 

make it better.....

yooy
Said this on 10-2-2010 At 08:18 am

 its useful but some programs output r not correct and also the explanation..... so whoever applys jus try out the programs and proceed....

majid
Said this on 10-10-2010 At 09:31 am

Hye:

i am a student of Computer Science... i want a program written in C which display seven segment display , when i press 1 it shows 1 in out put and 2 , 3,..... upto 9,A,B,C,D,E,F....

Please if anyone know, do help me.

Thanks

pavithra
Said this on 10-20-2010 At 06:39 pm

1.why is it is given as &gd ,&gm in initgraph in graphics mode instead of just gd,gm.....ie.without address of (&) operator.....if it was to pass address of constants , then pls can u pls explain me in detail about that...

2.why is 0x33 used in int86() or is there any problem if 51 is used instead of 0x33?

3.what does i.x.ax mean? does it use pointers concept?if so pls explain

Saranya.M
Said this on 10-22-2010 At 10:55 am

#include <stdio.h>

main()

{

int a,b,c;

float ad,s,m,d;

clrscr();

 

printf("enter a value:");

printf("enter b value:");

 

ad=a+b;

s=a-b;

m=a*b;

d=a/b;

 

printf("%f\n%f\n%f\n%f\n",ad,s,m,d);

 

getch();

}

 

jagan
Said this on 12-3-2010 At 11:26 pm

very nice program

 

chakri
Said this on 3-4-2012 At 11:14 pm

it gives garbage values , why because you take input values from enduser, but you canot read the data (that is you didnt take scanf() function)

Said this on 10-24-2010 At 11:40 am

PLZ ANS ME DESE QUES. AS SOON AS POSSIBLE........I HV 2 SUBMIT BY 26-OCT-2010.........THANX

 PART A

Q1. Write a program to count the negative elements from the list using Arrays.

 

Q2. How can you differentiate a lower level array from the higher level array?

 

Q3. Write a program that accepts an amount in figures and print that in words.    

       [Hint: - for an amount of Rs. 15200.50 it should output the string Rs.  Fifteen Thousand Two Hundred and Paisa Fifty only]

 

Q4. How will you initialize a three-dimensional array thread [3][2][3]? How will you refer the first and last element in this array?  

PART-B

Q5. Can an array of pointers to strings be used to collect strings from the keyboard? If not, why not? How, if yes?

 

Q6. Give an example of a structure declaration without the use of a tag? What are its advantages & disadvantages?

 

Q7.Create a structure to specify data on students given below:

            Roll_no, name, deptt, course, year_of_passing

         Assume that there are not more than 200 students in the college.

(a)    Write a function to print names of all students who joined in a particular year.

(b)   Write a function to print the data of a student whose roll number is given.

 

nagendra
Said this on 9-13-2011 At 08:23 pm

if any one send the answers send to me

 

bhavesh jain
Said this on 10-26-2010 At 01:18 am

send me questions of C lang. 

Said this on 10-28-2010 At 12:06 pm
How do you write a c program to shift the entered number by 3 bits left and also 5 bits right and display the result?
maria
Said this on 11-7-2010 At 11:58 pm

I need a program that reads a file of data in a directory  with the name of the function " filename_prompt_user()"
and then to calculate the histogram of the data and save it to another directory.
Thank you for your time in advance!!!
How could be a program that reads a file of data in a directory  with the name of the function " filename_prompt_user()"and then to calculate the histogram of the data and save it to another directory?

Said this on 11-8-2010 At 12:17 pm

hi, 

i need a program to dispaly the integer values in cyclical order...

i.e if i enter 4 then its ouput should be of 4*4 in cyclical order

 

eg: enter any number :4

output :

 1     2    3    4

 12  13  14    5

         11  16  15    6

         10   9    8     7 

 

please help me out...

shanthi
Said this on 12-1-2010 At 03:25 pm

I need more explanation about towers  of hanoi...

Md Iftekhar Uddin
Said this on 1-15-2011 At 11:40 pm

I m much eager to know about 'c' and always wants some new technique to learn in this language if u have any idea to please give me some trick and if u have any notes on c please send me on my email. iftekhar06@rediffmail.com,iftekhar06@gmail.com,iftekharuddin786@gmail.com

nagaraju
Said this on 2-18-2011 At 07:39 pm

i want c code for comparision of two numbers without relational operations

Suresh
Said this on 4-10-2011 At 09:50 am

good bosssssssss

NAEEM BANGASH
Said this on 4-26-2011 At 01:20 am

some one help me

  i need a programe to display ,if i pess a capital character then result in capital if i press small character then result in small ,

e.g

i press a

result=a

i press A

result A

Bharat
Said this on 7-1-2011 At 04:51 am

#include<conio.h>

#include<tdio.h>

void main ()

{

 char a;

printf("Enter a char:");

a=getche();

printf("You have entred %c",a);

getch();

}

manikandan
Said this on 7-10-2011 At 07:59 pm

useful to all...

DILWAR
Said this on 7-24-2011 At 11:08 am

Hiiiiiiiiii I am DILWAR from EIILM University

sahe
Said this on 9-4-2011 At 12:12 pm

pls help me to write a program using string functions.. which converts "a" to "an" in a given para.. and too the para must get from the user.. pls do help.. 

sonita mohanty
Said this on 9-8-2011 At 12:19 pm

thanks for giving tips abt c

plz send more tips abt c & c++ to my mail

farida hazem
Said this on 9-21-2011 At 06:08 pm

can someone help me find the c++ program or code for the towers of hanoi game using this algorthim :

Towers (N, Source , Target , Aux)

      {

            if N = 1 move disk 1 from Source to Target directly

            else

            {

                        Call Towers to move N-1 disks from Source to Aux via Target

                        Move disk N from Source to Target directly

                        Call Towers to move N-1 disks from Aux to Target via Source

            }

      }

and this implementation :

Develop a program using the above algorithm to simulate the Towers of Hanoi game. Number the disks 1,2,3,…,N in ascending order of their size. Use a stack for each peg to represent its disk content at any moment. Display the stack to see each move until all disks have been moved from peg A to peg C.

Said this on 9-30-2011 At 09:51 pm

what is dangling object

Said this on 11-2-2011 At 11:08 pm

#include<stdio.h>
#include<conio.h>
void Board();
void PlayerP();
void PlayerM();
void Player_win();
void check();
int win=0,wrong_P=0,wrong_M=0,chk=0;
char name_P[30];
char name_M[30];
int pos_for_P[3][3];
int pos_for_M[3][3];
int pos_marked[3][3];
void main()
{
   int i,ch,j;
   char ans;
/* clrscr();
 printf("\n\t\t\t\tTIC TAC TOE");
 printf("\n\t\t\t\t");
 for(i=1;i<=11;i++)
 {
  delay(10000);
  printf("*");
 }*/
      do
      {
  clrscr();
  printf("\n\t\t\t\tTIC TAC TOE");
  printf("\n\t\t\t\t");
  for(i=1;i<=6;i++)
   {
    delay(1000);
    printf("*-");
    }
    printf("\n1.Start The Game");
    printf("\n2.Quit The Game");
    printf("\nEnter your choice(1-2) : ");
    scanf("%d",&ch);
   switch(ch)
    {
     case 1:
  chk=0;
  win=0;
  for(i=1;i<=3;i++)
   {
  for(j=1;j<=3;j++)
   {
   pos_for_P[i][j]=0;
   pos_for_M[i][j]=0;
   pos_marked[i][j]=0;
   }
  }
  printf("\n\n");
  clrscr();
  printf("\nEnter the name of the player playing for \'P\': ");
  fflush(stdin);
  gets(name_P);
  printf("\nEnter the name of the player playing for\'M\': ");
  fflush(stdin);
  gets(name_M);
  Board();
  for(;;)
   {
  if(win==1)
  break;
  check();
  if(chk==9)
  {
  printf("\n\t\t\tMATCH DRAWS!!");
  printf("\nPress any key....");
  break;
  }
  else
  chk=0;
  printf("\nTURN FOR %s:",name_P);
  PlayerP();
   do
    {
    if(wrong_P!=1)
    break;
    wrong_P=0;
    printf("\nTURN FOR %s:",name_P);
    PlayerP();
    }
    while(wrong_P==1);
    check();
    if(chk==9)
    {
    printf("\n\t\t\tMATCH DRAWS");
    printf("\nPress any key....");
    break;
    }
    else
    chk=0;
    printf("\nTURN FOR %s:",name_M);
    PlayerM();
    do
    {
    if(wrong_M!=1)
    break;
    wrong_M=0;
    printf("\nTURN FOR %s:",name_M);
    PlayerM();
    }
    while(wrong_M==1);
      }
    Board();
    if(win!=1)
    {
    printf("\n\t\t\tMATCH DRAWS!!");
    printf("\nPress any key.......");
     }
     getch();
     break;
    case 2:
   printf("\n\n\n\t\t\tThank You For Playing The Game.");
   printf("\n\t\t\t#!#!#!#!#!#!#!#!#!#!#!#!#!#!#!#");
   getch();
   exit(1);
   break;
    }
    printf("\nWant To Play(Y/N) ? ");
    fflush(stdin);
    scanf("%c",&ans);
    }
    while(ans=='y' || ans=='Y');
      }
void Board()
{
  int i,j;
  clrscr();
  printf("\n\t\t\t\tTIC TAC TOE BOARD");
  printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~");
  printf("\n\n\n");
  printf("\n\t\t\t    1\t      2\t        3");
  for(i=1;i<=3;i++)
  {
  printf("\n \t\t\t _____________________________");
  printf("\n \t\t\tº\t  º\t   º\t     º");
  printf("\n\t\t%d\t",i);
  for(j=1;j<=3;j++)
  {
  if(pos_for_P[i][j]==1)
    {
    printf("    P");
    printf("     ");
     }
    else if(pos_for_M[i][j]==1)
     {
     printf("    M");
     printf("     ");
      }
    else
     {
     printf("          ");
     continue;
      }
     }
    printf("\n\t\t\tº\t  º\t   º\t     º");
     }
      printf("\n\t\t\t------------------------------");
      Player_win();
    }
void PlayerP()
{
 int row,col;
 if(win==1)
  return;
  printf("\nEnter the row no. : ");
  fflush(stdin);
  scanf("%d",&row);
  printf("Enter the column no. : ");
  fflush(stdin);
  scanf("%d",&col);
 if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
 {
  printf("\nWRONG POSITION!! Press any key.....");
   wrong_P=1;
   getch();
   Board();
 }
 else
 {
  pos_for_P[row][col]=1;
  pos_marked[row][col]=1;
  Board();
 }
 }
void PlayerM()
{
  int row,col;
  if(win==1)
  return;
  printf("\nEnter the row no. : ");
  scanf("%d",&row);
  printf("Enter the column no. : ");
  scanf("%d",&col);
    if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
    {
    printf("\nWRONG POSITION!! Press any key....");
    wrong_M=1;
    getch();
    Board();
    }
    else
     {
     pos_for_M[row][col]=1;
     pos_marked[row][col]=1;
     Board();
     }
  }
void Player_win()
{
  int i;
  for(i=1;i<=3;i++)
   {
   if(pos_for_P[i][1]==1 && pos_for_P[i][2]==1 && pos_for_P[i][3]==1)
    {
    win=1;
    printf("\n\nRESULT: %s wins!!",name_P);
    printf("\nPress any key............");
    return;
     }
    }
    for(i=1;i<=3;i++)
     {
     if(pos_for_P[1][i]==1 && pos_for_P[2][i]==1 && pos_for_P[3][i]==1)
      {
      win=1;
      printf("\n\nRESULT: %s wins!!",name_P);
      printf("\nPress any key............");
      return;
      }
     }
     if(pos_for_P[1][1]==1 && pos_for_P[2][2]==1 && pos_for_P[3][3]==1)
      {
      win=1;
      printf("\n\nRESULTL: %s wins!!",name_P);
      printf("\nPress any key......");
      return;
       }
      else if(pos_for_P[1][3]==1 && pos_for_P[2][2]==1 && pos_for_P[3][1]==1)
 {
 win=1;
 printf("\n\nRESULT: %s wins!!",name_P);
 printf("\nPress any key.....");
 return;
 }
     for(i=1;i<=3;i++)
     {
     if(pos_for_M[i][1]==1 && pos_for_M[i][2]==1 && pos_for_M[i][3]==1)
      {
       win=1;
       printf("\n\nRESULT: %s wins!!",name_M);
       printf("\nPress any key.....");
       return;
      }
    }
    for(i=1;i<=3;i++)
     {
    if(pos_for_M[1][i]==1 && pos_for_M[2][i]==1 && pos_for_M[3][i]==1)
     {
     win=1;
     printf("\n\nRESULT: %s wins!!",name_M);
     printf("\nPress any key.....");
     return;
     }
    }
   if(pos_for_M[1][1]==1 && pos_for_M[2][2]==1 && pos_for_M[3][3]==1)
    {
    win=1;
    printf("\n\nRESULT: %s wins!!",name_M);
    printf("\nPress any key.....");
    return;
     }
   else if(pos_for_M[1][3]==1 && pos_for_M[2][2]==1 && pos_for_M[3][1]==1)
 {
 win=1;
 printf("\n\nRESULT: %s wins!!",name_M);
 printf("\nPress any key.....");
 return;
     }
   }
void check()
{
  int i,j;
  for(i=1;i<=3;i++)
   {
    for(j=1;j<=3;j++)
    {
    if(pos_marked[i][j]==1)
    chk++;
    else
    continue;
   }
  }
}explain

 

 

Said this on 11-2-2011 At 11:11 pm

#include<stdio.h>
#include<conio.h>
void Board();
void PlayerP();
void PlayerM();
void Player_win();
void check();
int win=0,wrong_P=0,wrong_M=0,chk=0;
char name_P[30];
char name_M[30];
int pos_for_P[3][3];
int pos_for_M[3][3];
int pos_marked[3][3];
void main()
{
   int i,ch,j;
   char ans;
/* clrscr();
 printf("\n\t\t\t\tTIC TAC TOE");
 printf("\n\t\t\t\t");
 for(i=1;i<=11;i++)
 {
  delay(10000);
  printf("*");
 }*/
      do
      {
  clrscr();
  printf("\n\t\t\t\tTIC TAC TOE");
  printf("\n\t\t\t\t");
  for(i=1;i<=6;i++)
   {
    delay(1000);
    printf("*-");
    }
    printf("\n1.Start The Game");
    printf("\n2.Quit The Game");
    printf("\nEnter your choice(1-2) : ");
    scanf("%d",&ch);
   switch(ch)
    {
     case 1:
  chk=0;
  win=0;
  for(i=1;i<=3;i++)
   {
  for(j=1;j<=3;j++)
   {
   pos_for_P[i][j]=0;
   pos_for_M[i][j]=0;
   pos_marked[i][j]=0;
   }
  }
  printf("\n\n");
  clrscr();
  printf("\nEnter the name of the player playing for \'P\': ");
  fflush(stdin);
  gets(name_P);
  printf("\nEnter the name of the player playing for\'M\': ");
  fflush(stdin);
  gets(name_M);
  Board();
  for(;;)
   {
  if(win==1)
  break;
  check();
  if(chk==9)
  {
  printf("\n\t\t\tMATCH DRAWS!!");
  printf("\nPress any key....");
  break;
  }
  else
  chk=0;
  printf("\nTURN FOR %s:",name_P);
  PlayerP();
   do
    {
    if(wrong_P!=1)
    break;
    wrong_P=0;
    printf("\nTURN FOR %s:",name_P);
    PlayerP();
    }
    while(wrong_P==1);
    check();
    if(chk==9)
    {
    printf("\n\t\t\tMATCH DRAWS");
    printf("\nPress any key....");
    break;
    }
    else
    chk=0;
    printf("\nTURN FOR %s:",name_M);
    PlayerM();
    do
    {
    if(wrong_M!=1)
    break;
    wrong_M=0;
    printf("\nTURN FOR %s:",name_M);
    PlayerM();
    }
    while(wrong_M==1);
      }
    Board();
    if(win!=1)
    {
    printf("\n\t\t\tMATCH DRAWS!!");
    printf("\nPress any key.......");
     }
     getch();
     break;
    case 2:
   printf("\n\n\n\t\t\tThank You For Playing The Game.");
   printf("\n\t\t\t#!#!#!#!#!#!#!#!#!#!#!#!#!#!#!#");
   getch();
   exit(1);
   break;
    }
    printf("\nWant To Play(Y/N) ? ");
    fflush(stdin);
    scanf("%c",&ans);
    }
    while(ans=='y' || ans=='Y');
      }
void Board()
{
  int i,j;
  clrscr();
  printf("\n\t\t\t\tTIC TAC TOE BOARD");
  printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~");
  printf("\n\n\n");
  printf("\n\t\t\t    1\t      2\t        3");
  for(i=1;i<=3;i++)
  {
  printf("\n \t\t\t _____________________________");
  printf("\n \t\t\tº\t  º\t   º\t     º");
  printf("\n\t\t%d\t",i);
  for(j=1;j<=3;j++)
  {
  if(pos_for_P[i][j]==1)
    {
    printf("    P");
    printf("     ");
     }
    else if(pos_for_M[i][j]==1)
     {
     printf("    M");
     printf("     ");
      }
    else
     {
     printf("          ");
     continue;
      }
     }
    printf("\n\t\t\tº\t  º\t   º\t     º");
     }
      printf("\n\t\t\t------------------------------");
      Player_win();
    }
void PlayerP()
{
 int row,col;
 if(win==1)
  return;
  printf("\nEnter the row no. : ");
  fflush(stdin);
  scanf("%d",&row);
  printf("Enter the column no. : ");
  fflush(stdin);
  scanf("%d",&col);
 if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
 {
  printf("\nWRONG POSITION!! Press any key.....");
   wrong_P=1;
   getch();
   Board();
 }
 else
 {
  pos_for_P[row][col]=1;
  pos_marked[row][col]=1;
  Board();
 }
 }
void PlayerM()
{
  int row,col;
  if(win==1)
  return;
  printf("\nEnter the row no. : ");
  scanf("%d",&row);
  printf("Enter the column no. : ");
  scanf("%d",&col);
    if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
    {
    printf("\nWRONG POSITION!! Press any key....");
    wrong_M=1;
    getch();
    Board();
    }
    else
     {
     pos_for_M[row][col]=1;
     pos_marked[row][col]=1;
     Board();
     }
  }
void Player_win()
{
  int i;
  for(i=1;i<=3;i++)
   {
   if(pos_for_P[i][1]==1 && pos_for_P[i][2]==1 && pos_for_P[i][3]==1)
    {
    win=1;
    printf("\n\nRESULT: %s wins!!",name_P);
    printf("\nPress any key............");
    return;
     }
    }
    for(i=1;i<=3;i++)
     {
     if(pos_for_P[1][i]==1 && pos_for_P[2][i]==1 && pos_for_P[3][i]==1)
      {
      win=1;
      printf("\n\nRESULT: %s wins!!",name_P);
      printf("\nPress any key............");
      return;
      }
     }
     if(pos_for_P[1][1]==1 && pos_for_P[2][2]==1 && pos_for_P[3][3]==1)
      {
      win=1;
      printf("\n\nRESULTL: %s wins!!",name_P);
      printf("\nPress any key......");
      return;
       }
      else if(pos_for_P[1][3]==1 && pos_for_P[2][2]==1 && pos_for_P[3][1]==1)
 {
 win=1;
 printf("\n\nRESULT: %s wins!!",name_P);
 printf("\nPress any key.....");
 return;
 }
     for(i=1;i<=3;i++)
     {
     if(pos_for_M[i][1]==1 && pos_for_M[i][2]==1 && pos_for_M[i][3]==1)
      {
       win=1;
       printf("\n\nRESULT: %s wins!!",name_M);
       printf("\nPress any key.....");
       return;
      }
    }
    for(i=1;i<=3;i++)
     {
    if(pos_for_M[1][i]==1 && pos_for_M[2][i]==1 && pos_for_M[3][i]==1)
     {
     win=1;
     printf("\n\nRESULT: %s wins!!",name_M);
     printf("\nPress any key.....");
     return;
     }
    }
   if(pos_for_M[1][1]==1 && pos_for_M[2][2]==1 && pos_for_M[3][3]==1)
    {
    win=1;
    printf("\n\nRESULT: %s wins!!",name_M);
    printf("\nPress any key.....");
    return;
     }
   else if(pos_for_M[1][3]==1 && pos_for_M[2][2]==1 && pos_for_M[3][1]==1)
 {
 win=1;
 printf("\n\nRESULT: %s wins!!",name_M);
 printf("\nPress any key.....");
 return;
     }
   }
void check()
{
  int i,j;
  for(i=1;i<=3;i++)
   {
    for(j=1;j<=3;j++)
    {
    if(pos_marked[i][j]==1)
    chk++;
    else
    continue;
   }
  }
}

kweku
Said this on 12-5-2011 At 02:29 pm

         Write a program to read through a file and determine all the numbers that have exactly three distinct factors in
    the input file. Your program should print into another file named numbers.out.               
Sample output
Input file     number.out
numbers.in     106     111     115    118      119     106      1713   71007   71009   31011   11013    12014
               11015   51019   34401  14302
numbers1.in    100013  1000111 100013 1000115 100777   10118    100121 100013  1000111 100013  1000115 100777
               10118   100121 10123   100123 100127    10129    10123  100123  100127 10129

Post a Comment
* Your Name:
* Your Email:
(not publicly displayed)
Reply Notification:
Approval Notification:
Website:
* Security Image:
Security Image Generate new
Copy the numbers and letters from the security image:
* Message:

Email to Friend

Fill in the form below to send this article to a friend:

Email to Friend
* Your Name:
* Your Email:
* Friend's Name:
* Friend's Email:
* Security Image:
Security Image Generate new
Copy the numbers and letters from the security image
* Message: