Check the latest jobs opening news in private and public sector.

Thursday, December 11, 2014

Top 30 "C" programs asked in interview of Top MNC ANSWERS ..!!!



<<<<<<< Programs Questions.  
 

16.Write a program to delete a specified line from a text file.
In this program, user is asked for a filename he needs to change. User is also asked for the line number that is
to be deleted. The filename is stored in'filename' . The file is opened and all the data is transferre d to another file
except that one line the user specifies to delete.

Program: Program to delete a specific line.
#include
int main(){
FILE *fp1, *fp2;
// consider 40 character string to store filename
char filename[4 0];
char c;
int del_line, temp = 1;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in'filename'
scanf("%s" , filename);
//open file in read mode
fp1 = fopen(file name,"r");
c = getc(fp1);
//until the last character of file is obtained
while (c != EOF)
{
printf("%c ", c);
//print current character and read next character
c = getc(fp1);
}
//rewind
rewind(fp1 );
printf("\n Enter line number of the line to be deleted:") ;
//accept number from user.
scanf("%d" ,&del_line) ;
//open new file in write mode
fp2 = fopen("cop y.c","w");
c = getc(fp1);
while (c != EOF){
c = getc(fp1);
if (c =='\n')
temp++;
//except the line to be deleted
if (temp != del_line)
{
//copy all lines in file copy.c
putc(c, fp2);
}
}
//close both the files.
fclose(fp1 );
fclose(fp2 );
//remove original file
remove(fil ename);
//rename the file copy.c to original name
rename("co py.c", filename);
printf("\n The contents of file after being modified are as follows:\n ");
fp1 = fopen(file name,"r");
c = getc(fp1);
while (c != EOF){
printf("%c ", c);
c = getc(fp1);
}
fclose(fp1 );
return 0;
}
Output:
Enter file name:abc.t xt
hi.
Hello
how are you?
I am fine
hope the same
Enter line number of the line to be deleted:4
The contents of file after being modified are as follows:
hi.
hello
how are you?
hope the same
Explanatio n:
In this program, user is asked for a filename that needs to be modified. Entered file name is stored in a char
array'filename' . This file is opened in read mode using file pointer'fp1'. Character'c'is used to read characters
from the file and print them to the output. User is asked for the line number in the file to be deleted. The file
pointer is rewinded back and all the lines of the file except for the line to be deleted are copied into another file
"copy.c". Now"copy.c"is renamed to the original filename. The original file is opened in read mode and the
modified contents of the file are displayed on the screen.


You can also see: Accenture Placement Papers

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


17.Write a program to replace a specified line in a text file.
Program: Program to replace a specified line in a text file.
http://www.ojasjob.com/
#include
int main(void) {
FILE *fp1, *fp2;
// 'filename'i s a 40 character string to store filename
char filename[4 0];
char c;
int del_line, temp = 1;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in'filename'
scanf("%s" , filename);
fp1 = fopen(file name,"r");
//open file in read mode
c = getc(fp1);
//print the contents of file .
while (c != EOF){
printf("%c ", c);
c = getc(fp1);
}
//ask user for line number to be deleted.
printf("\n Enter line number to be deleted and replaced") ;
scanf("%d" ,&del_line) ;
//take fp1 to start point.
rewind(fp1 );
//open copy.c in write mode
fp2 = fopen("cop y.c","w");
c = getc(fp1);
while (c != EOF){
if (c =='\n'){
temp++;
}
// till the line to be deleted comes,copy the content from one file to other
if (temp != del_line){
putc(c, fp2);
}
else //when the line to be deleted comes
{
while ((c = getc(fp1)) !='\n'){
}
//read and skip the line ask for new text
printf("En ter new text");
//flush the input stream
fflush(std in);
putc('\n', fp2);
//put'\n'in new file
while ((c = getchar()) !='\n')
putc(c, fp2);
//take the data from user and place it in new file
fputs("\n" , fp2);
temp++;
}
// continue this till EOF is encountere d
c = getc(fp1);
}
//close both files
fclose(fp1 );
fclose(fp2 );
//remove original file
remove(fil ename);
//rename new file with old name opens the file in read mode
rename("co py.c", filename);
fp1 = fopen(file name,"r");
//reads the character from file
c = getc(fp1);
// until last character of file is encountered
while (c != EOF){
printf("%c ", c);
// all characters are printed
c = getc(fp1);
}
//close the file pointer
fclose(fp1 );
return 0;
}
Output:
Enter file name:abc.t xt
hi.
hello
how are you?
hope the same
Enter line number of the line to be deleted and replaced:4
Enter new text: sayonara see you soon
hi.
hello
how are you?
sayonara see you soon
Explanatio n:
In this program, the user is asked to type the name of the file. The File by name entered by user is opened in
read mode. The line number of the line to be replaced is asked as input. Next the data to be replaced is asked. A
new file is opened in write mode named"copy.c". Now the contents of original file are transferre d into new file
and the line to be modified is deleted. New data is stored in its place and remaining lines of the original file are
also transferre d. The copied file with modified contents is replaced with the original file's name. Both the file
pointers are closed and the original file is again opened in read mode and the contents of the original file is
printed as output.


//******************************************************************************//
18.Write a program to find the number of lines in a text file.
Number of lines in a file can be determined by counting the number of new line characters present.

Program: Program to count number of lines in a file.
#include
int main()
/* Ask for a filename and count number of lines in the file*/
{
//a pointer to a FILE structure
FILE *fp;
int no_lines = 0;
// consider 40 character string to store filename
char filename[4 0], sample_chr ;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in a string named'filename'
scanf("%s" , filename);
//open file in read mode
fp = fopen(file name,"r");
//get character from file and store in sample_chr
sample_chr = getc(fp);
while (sample_ch r != EOF){
// Count whenever sample_chr is'\n'(new line) is encountere d
if (sample_ch r =='\n')
{
// increment variable'no_lines' by 1
no_lines=n o_lines+1;
}
//take next character from file.
sample_chr = getc(fp);
}
fclose(fp) ; //close file.
printf("Th ere are %d lines in %s \n", no_lines, filename);
return 0;
}
Output:
Enter file name:abc.t xt
There are 4 lines in abc.txt
Explanatio n:
In this program, name of the file to be read is taken as input. A file by the given name is opened in read-mode
using a File pointer'fp'. Characters from the file are read into a char variable'sample_ch r'with the help of getc
function. If a new line character( '\n') is encountere d, the integer variable'no_lines' is incremente d. If the
character read into'sample_ch ar'is not a new line character, next character is read from the file. This process is
continued until the last character of the file(EOF) is encountere d. The file pointer is then closed and the total
number of lines is shown as output.


//******************************************************************************//
19.Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt
the user for a valid input.

Program: Program for accepting a number in a given range.
#include
int getnumber( );
int main(){
int input = 0;
//call a function to input number from key board
input = getnumber( );
//when input is not in the range of 1 to 9,print error message
while (!((input = 1))){
printf("[E RROR] The number you entered is out of range");
//input another number
input = getnumber( );
}
//this function is repeated until a valid input is given by user.
printf("\n The number you entered is %d", input);
return 0;
}/
/this function returns the number given by user
int getnumber( ){
int number;
//asks user for a input in given range
printf("\n Enter a number between 1 to 9 \n");
scanf("%d" ,&number);
return (number);
}
Output:
Enter a number between 1 to 9
45
[ERROR] The number you entered is out of range
Enter a number between 1 to 9
4
The number you entered is 4
Explanatio n:
getfunctio n() function accepts input from user.'while'loop checks whether the number falls within range or not
and accordingl y either prints the number(If the number falls in desired range) or shows error message(nu mber is
out of range).


You can also see: L&T Infotech Placement Papers

//******************************************************************************//
20.Write a program to display the multiplica tion table of a given number.
Program: Multiplica tion table of a given number
#include
int main(){
int num, i = 1;
printf("\n Enter any Number:");
scanf("%d" ,&num);
printf("Mu ltiplicati on table of %d: \n", num);
while (i
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
Output:
Enter any Number:5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Explanatio n:
We need to multiply the given number (i.e. the number for which we want the multiplica tion table)
with value of'i'which increments from 1 to 10.


//******************************************************************************//
21. .WAP to check a string is Caliondrom e or not. // Maventic question.
#include
#include
void main()
{
int i,j=0; char a[100];
clrscr();
printf("\n Enter the string to check for caliondrom e:\n");
gets(a);
if(strlen( a)%6)
{
printf("\n %s: is Not a caliondrom e..",a);
getch();
exit(0);
}
for (i=0;a[i]! ='\0'
{
if((a[i]== a[i+5])&&( a[i+1]==a[ i+4])&&(a[ i+2]==a[i+ 3]))
i=i+6;
else
{
j=1;
break;
}
}
if(j)
printf("\n %s: is Not a caliondrom e..",a);
else
printf("\n %s: is a caliondrom e..",a);
getch();
}


//******************************************************************************//
22.WAP to print DONE,witho ut using any loop. // asked to my frnd in any company.
#include
void main()
{
static int i=0;
printf("\n %d. DONE",i);
if(i++
main();
getch();
exit(0); / * I used exit(0) to terminate the program after 100 DONE,,i dunno why it was not terminating without using it,may be just at my system,try without it at ur sustem,it sud work */
}


//******************************************************************************//
23.WAP to print DONE,witho ut using any loop and any conditonal clause or operators.
/* This code is just in purpose to solve the above question,, but its not a good code in programmin g,as its terminatin g at divide error,,if anyone have a better code,let me know */
main()
{
static int i=100;
printf("%d . DONE\n",10 1-i);
main(1/ --i);
}
/* use"ctrl+f9",then"alt+f5"to see the result */


//******************************************************************************//
24. WAP to find out the longest word in a string.
#include
#include
#include
void main()
{
int i,max=0,co unt=0,j;
char str[100]; / * ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */
printf("\n Enter the string\n:" );
gets(str);
for(i=0;i
{
if(!(str[i ]==32))
{
count++;
}
else
{
if(max
{
j=i-count;
max=count;
}
count=0;
}
}
for(i=j;i
printf("%c ",str[i]);
getch();
}


//******************************************************************************//
25.Prog of WORLD MAP.
#include main(l ,a,n,d)cha r**a;{for(d=atoi (a[1])/ 10*80- atoi(a[2]) / 5-596;n="@N KA\CLCCGZA AQBEAADAFa ISADJABBA^ \SNLGAQABD AXIMBAACTB ATAHDBAN\Z cEMMCCCCAA hEIJFAEAAA BAfHJE\TBd FLDAANEfDN BPHdBcBBBE A_AL\ H E L L O, W O R L D!"[l++-3];)f or(;n-->64 putchar(!d +++33^ l&1);print f("\n\n\n\ n\t\tFound By:\n\t\t\ t Amit Aru");getc h();}


//******************************************************************************//
26.WAP to print the triangle of letters in increasing order of lines.
#include
#include
void main()
{
int i,j,k;
char ch;
printf("\n Enter the number of lines wants to make the triangle \n:");
scanf("%d" ,&i);
for(j=1;j
{
ch=65;
for(k=1;k
{
printf("%c ",ch++);
}
printf("\n ");
}
getch();
}


You can also see: Virtusa Placement Papers

//******************************************************************************//
27.WAP to print'xay'in place of every'a'in a string.
#include
#include
void main()
{
int i=0;
char str[100],x ='x',y='y' ;
printf("En ter the string\n:");
gets(str);
while(str[ i]!='\0')
{
if(str[i]= ='a')
{
printf("%c ",x);
printf("%c ",str[i++] );
printf("%c ",y);
}
else
{
printf("%c ",str[i++] );
}
}
getch();
}


//******************************************************************************//
28.Count the Total Number of 7 comming between 1 to 100.
/* I made this code in a way that u can give Upper limit i.e. 100,Lower limit i.e. 1 and the specific number u wants to count in between i.e. 7 */
#include
#include
void main()
{
int i,j,U=100, L=1,count= 0,r=1,n;
clrscr();
printf("\n Enter the number u wants to count\n:");
scanf("%d" ,&n);
printf("\n Enter the lower limit\n:");
scanf("%d" ,&L);
printf("\n Enter the upper limit\n:");
scanf("%d" ,&U);
for (i=L;i
{
j=i;
while(j)
{
r=j%10;
if (r==n)
{
count++;
}
j=j/10;
}
}
if(n==0&&L ==0)
count++;
printf("\n Total Number of %d between %d and %d = %d",n,L,U, count);
getch();
}

//******************************************************************************//
29. Code for duplicate' s removal,by Amit Aru.
#include
#include
void main()
{
int i,j,k=0,co unt[300]={ 0};
char ch,str[100 0],str1[10 00];
clrscr();
printf("\n Enter the string to remove duplicasy\ n:");
gets(str);
for (i=0;str[i ]!='\0';i+ +)
{
ch=str[i];
count['']=0; / * U can use other delimiter inplace of space''here,just put that char inside'',for ex: count['A']=0 ; if u dnt want any delimiter, just remove this line.*/
if(count[c h])
continue;
else
{
str1[k++]= ch;
count[ch]= 1;
}
}
puts(str1) ;
getch();
}


 //******************************************************************************//
30. WAP to find out if a given number is a power series of 2 or not,withou t any loop and without using % modulo operator.
#include
#include
int pow2(float );
void main()
{
int i,flag;
clrscr();
printf("En ter the number\n") ;
scanf("%d" ,&i);
flag=pow2( i);
if(flag)
printf("\n %d is power series of 2",i);
else
printf("\n %d is not a power series of 2",i);
getch();
}
int pow2(float j)

{
static float x;
x=j/2;
if(x==2)
return 1;
if(x
return 0;
x=pow2(x);


ANSWERS Click Here >>> Ans 1-15 

More Placement Papers Click Here 

Get Latest Freshers Jobs Alerts For Free 


Register Here


For Jobs Updates via Facebook 

facebook


Share:

0 comments:

Post a Comment

Copyright © Latest Job Opening News in Private & Public Sector- Bestjobalert.com | Powered by Blogger Design by ronangelo | Blogger Theme by NewBloggerThemes.com