C Programs
C Programs
30 "C" programs asked in interview,,Codes By.. Amit Aru..!!! By Srihari Parimi, Ashish Patra and 85 others in Bangalore Job seekers (Files) Edit Doc Programs 1. Write a program to find factorial of the given number... 2. Write a program to check whether the given number is even or odd. 3. Write a program to swap two numbers using a temporary variable. 4. Write a program to swap two numbers without using a temporary variable. 5. Write a program to swap two numbers using bitwise operators. 6. Write a program to find the greatest of three numbers. 7. Write a program to find the greatest among ten numbers. 8. Write a program to check whether the given number is a prime. 9. Write a program to check whether the given number is a palindromic number. 10.Write a program to check whether the given string is a palindrome. 11.Write a program to generate the Fibonacci series. 12.Write a program to print "Hello World" without using semicolon anywhere in the code. 13.Write a program to print a semicolon without using a semicolon anywhere in the code. 14.Write a program to compare two strings without using strcmp() function. 15.Write a program to concatenate two strings without using strcat() function. 16.Write a program to delete a specified line from a text file. 17.Write a program to replace a specified line in a text file. 18.Write a program to find the number of lines in a text file.. 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. 20.Write a program to display the multiplication table of a given number..
BJS
Page 1
BJS
Page 2
2. Write a program to check whether the given number is even or odd. Program: #include <stdio.h> int main() { int a; printf("Enter a: \n"); scanf("%d", &a); /* logic */ if (a % 2 == 0) { printf("The given number is EVEN\n"); } else { printf("The given number is ODD\n"); } return 0; } Output: Enter a: 2 The given number is EVEN Explanation with examples: Example 1: If entered number is an even number Let value of 'a' entered is 4 if(a%2==0) then a is an even number, else odd. i.e. if(4%2==0) then 4 is an even number, else odd. To check whether 4 is even or odd, we need to calculate (4%2). /* % (modulus) implies remainder value. */ /* Therefore if the remainder obtained when 4 is divided by 2 is 0, then 4 is even. */ 4%2==0 is true Thus 4 is an even number. Example 2: If entered number is an odd number.
BJS Page 4
3. Write a program to swap two numbers using a temporary variable. Swapping interchanges the values of two given variables. Logic: step1: temp=x; step2: x=y; step3: y=temp; Example: if x=5 and y=8, consider a temporary variable temp. step1: temp=x=5; step2: x=y=8; step3: y=temp=5; Thus the values of the variables x and y are interchanged. Program: #include <stdio.h> int main() { int a, b, temp; printf("Enter the value of a and b: \n"); scanf("%d %d", &a, &b); printf("Before swapping a=%d, b=%d \n", a, b); /*Swapping logic */ temp = a; a = b; b = temp; printf("After swapping a=%d, b=%d", a, b); return 0; } Output: Enter the values of a and b: 2 3 Before swapping a=2, b=3 After swapping a=3, b=2
BJS Page 5
5. Write a program to swap two numbers using bitwise operators. Program: #include <stdio.h> int main() { int i = 65;
BJS Page 6
6. Write a program to find the greatest of three numbers. Program: #include <stdio.h> int main(){ int a, b, c; printf("Enter a,b,c: \n"); scanf("%d %d %d", &a, &b, &c);
BJS Page 7
7. Write a program to find the greatest among ten numbers. Program: #include <stdio.h> int main() { int a[10];
BJS Page 8
BJS
Page 9
10.Write a program to check whether the given string is a palindrome. Palindrome is a string, which when read in both forward and backward way is same. Example: radar, madam, pop, lol, rubber, etc., Program: #include <stdio.h> #include <string.h> int main() { char string1[20]; int i, length; int flag = 0; printf("Enter a string: \n"); scanf("%s", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) {
BJS Page 13
11.Write a program to generate the Fibonacci series. Fibonacci series: Any number in the series is obtained by adding the previous two numbers of the series. Let f(n) be n'th term. f(0)=0; f(1)=1; f(n)=f(n-1)+f(n-2); (for n>=2)
BJS Page 14
12.Write a program to print "Hello World" without using semicolon anywhere in the code. Generally when we use printf("") statement, we have to use a semicolon at the end. If printf is used inside an if Condition, semicolon can be avoided. Program: Program to print something without using semicolon (;) #include <stdio.h> int main() { //printf returns the length of string being printed if (printf("Hello World\n")) //prints Hello World and returns 11 { //do nothing } return 0; } Output: Hello World Explanation: The if statement checks for condition whether the return value of printf("Hello World") is greater than 0. printf function returns the length of the string printed. Hence the statement if (printf("Hello World")) prints the string "Hello World".
13.Write a program to print a semicolon without using a semicolon anywhere in the code. Generally when use printf("") statement we have to use semicolon at the end.
BJS Page 16
14.Write a program to compare two strings without using strcmp() function. strcmp() function compares two strings lexicographically. strcmp is declared in stdio.h Case 1: when the strings are equal, it returns zero. Case 2: when the strings are unequal, it returns the difference between ascii values of the characters that differ. a) When string1 is greater than string2, it returns positive value. b) When string1 is lesser than string2, it returns negative value. Syntax: int strcmp (const char *s1, const char *s2); Program: to compare two strings. #include<stdio.h> #include<string.h>
BJS Page 17
15.Write a program to concatenate two strings without using strcat() function. strcat(string1,string2) is a C standard function declared in the header file string.h The strcat() function concatenates string2, string1 and returns string1. Program: Program to concatenate two strings #include<stdio.h> #include<string.h> char *strct(char *c1, char *c2); char *strct(char *c1, char *c2) { //strlen function returns length of argument string int i = strlen(c1); int k = 0; //loops until null is encountered and appends string c2 to c1 while (c2[k] != '\0') { c1[i + k] = c2[k]; k++; } return c1; } int main() { char string1[15] = "first"; char string2[15] = "second"; char *finalstr; printf("Before concatenation:" " \n string1 = %s \n string2 = %s", string1, string2); //addresses of string1, string2 are passed to strct()
BJS Page 19
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 transferred to another file except that one line the user specifies to delete. Program: Program to delete a specific line. #include <stdio.h> int main() { FILE *fp1, *fp2;
BJS Page 20
17.Write a program to replace a specified line in a text file. Program: Program to replace a specified line in a text file. #include <stdio.h> int main(void) { FILE *fp1, *fp2; //'filename'is a 40 character string to store filename char filename[40]; char c; int del_line, temp = 1; //asks user for file name printf("Enter file name: "); //receives file name from user and stores in 'filename' scanf("%s", filename); fp1 = fopen(filename, "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("copy.c", "w"); c = getc(fp1); while (c != EOF) {
BJS Page 23
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<stdio.h> 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 <= 9) && (input >= 1))) { printf("[ERROR] 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("\nThe number you entered is %d", input); return 0; }/
BJS Page 27
24. WAP to find out the longest word in a string. #include<stdio.h> #include<string.h> #include<conio.h> void main() { int i,max=0,count=0,j; char str[100]; /* ={"INDIA IS DEMOCRATIC COUNTRY"}; u can use a string inside,in place of user input */ printf("\nEnter the string\n:"); gets(str); for(i=0;i<strlen(str);i++) { if(!(str[i]==32)) { count++; } else { if(max<count) { j=i-count; max=count; } count=0; } } for(i=j;i<(j+max);i++) printf("%c",str[i]); getch(); }
BJS
Page 31
26.WAP to print the triangle of letters in increasing order of lines. #include<stdio.h> #include<conio.h> void main() { int i,j,k; char ch; printf("\nEnter the number of lines wants to make the triangle \n: "); scanf("%d",&i); for(j=1;j<=i;j++) { ch=65; for(k=1;k<=j;k++) { printf("%c",ch++); } printf("\n"); } getch(); } 27.WAP to print 'xay' in place of every 'a' in a string. #include<stdio.h> #include<conio.h> void main() {
BJS Page 32
29. Code for duplicate's removal,by Amit Aru. #include<stdio.h> #include<conio.h> void main() { int i,j,k=0,count[300]={0}; char ch,str[1000],str1[1000]; clrscr(); printf("\nEnter 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.*/
BJS
Page 34
BJS
Page 36