Skip to main content

Posts

Showing posts with the label sample c programs

Technical Interview - C Programs Frequently Asked In Interviews.

C Programs Frequently Asked In Interviews. This section covers C programs often asked in campus interviews and freshers walk-in. It helps job seekers who are about to attend TECHNICAL interview round . Here you will find simple C programming questions with answers basically asked in technical interviews. 1) C Program To Print A Message. 2) C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. 3) C Program To Accept Student Roll No, Marks in 3 Subjects and Calculate Total, Average and Print it. 4) C Program To Read Three Numbers And Print The Biggest Of Given Three Numbers. 5) C Program To Read A Number And Find Whether The Given Number Is Even Or Odd. 6) C Program to accept a year and check whether the given year is a leap year or not. 7) C Program to print sum of individual digits of given number. 8) C Program to accept a three digit number and print the sum of individual digits. 9) C Program to accept a number and check the given number is Armstron...

Sample C Program to Print a Semicolon without using a Semicolon anywhere in the code.

# include <stdio.h> int main(void) { // 59 is the ascii value of semicolumn     if (printf("%c ", 59))     {     }     return 0; } Output: ; Sample C program to accept an integer and reverse it. # include <stdio.h> void main() {     long  num, reverse = 0, temp, remainder;     printf("Enter the number\n");     scanf("%ld", &num);     temp = num;     while (num > 0)     {         remainder = num % 10;         reverse = reverse * 10 + remainder;         num /= 10;     }     printf("Given number = %ld\n", temp);     printf("Its reverse is = %ld\n", reverse); } Output: Enter the number 567865 Given number   = 567865 Its ...

Sample C Program to accept a string and print the reverse of the given string.

40) Sample C Program to accept a string and print the reverse of the given string. # include <stdio.h> # include <conio.h> main( ) { int i, j; char name[80]; clrscr( ); printf("Enter a string"); gets(name); for(i = 0; i < 80 && ((name [i] = getchar()) != '\n'); i++);      if(name[i]= ='\n')          name[i]='\0'; for(j=i;j>=0;j--)       putchar(name[j]); printf("Is the reverse of given string"); getch( ); } 41) Sample C Program to accept a string and check whether the given string is palindrome or not. # include <stdio.h> # include <conio.h> main( ) { int i, lim, c, check = 1; char word[80]; clrscr( ); printf("Enter a string"); for(i = 0; i < 80 && ((word [i] = getchar()) != '\n'); i++); lim = i - 1; c = lim / 2; for(i = 0; i <= 0; i++, lim--)      if(word[i] != word[lim])        {     ...

Sample C Program For Armstrong Numbers

7) Sample C Program to print sum of individual digits of given number. # include <stdio.h> # include <conio.h> main( ) { int a, b, c, d; clrscr( ); printf ( "Enter any two digit number :"); scanf ("%d", &a); b = a / 10; c = a % 10; d = b + c; printf ("Sum of individual digits of given numbers is %d", d); getch( ); } 8) Sample C Program Program to accept a three digit number and print the sum of individual digits. # include <stdio.h> # include <conio.h> main( ) { int a, b, c, n, sum; clrscr( ); printf ("Enter a Three Digit Number:"); scanf ("%d",&n); a = n / 100; b = ( ( n % 100 ) / 10 ); c = n % 10; sum = a + b + c; printf ("Sum of Individual Digits of Given Numbers is %d", Sum); getch( ); } 9. Sample C Program to accept a number and check the given number is Armstrong or not. # include <stdio.h> # include <conio.h> main( ) { int n,...

Sample C Program To Print A Message.

1) Sample C Program To Print A Message. # include <stdio.h> # include <conio.h> main() { clrscr(); printf("HELLO WELCOME TO WWW.INTERVIEW-MADE-EASY.COM"); printf("Answers To All Important Interview Questions"); getch(); } 2) Sample C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. # include <stdio.h> # include <conio.h> main() { int a,b, sum; clrscr (); printf ("ENTER VALUE FOR A;"); scanf ("%d",&a); printf("ENTER VALUE FOR B;"); scanf("%d",&b); sum = a + b; printf("Sum Of Given Two Numbers are %d", sum); getch(); } 3) Sample C Program To Accept Student Roll No, Marks in 3 Subjects and Calculate Total, Average and Print it. # include <stdio.h> # include <conio.h> main() { int r, b, c, d, tot, avg; clrscr(); printf ("ENTER STUDENT RNO;"); scanf ("%d",&r); printf("ENTER FIRST SU...

Sample C Program to accept a string in upper case and print it in lower case.

28) Sample C Program to accept a string and print it by using the while loop. # include <stdio.h> # include <conio.h> main( ) { char ch; clrscr(); printf("Enter a string:"); while(( ch = getchar() ) != '\n')    putchar(ch); getch(); } 29) Sample C Program to accept a string in upper case and print it in lower case. # include <stdio.h> # include <conio.h> main( ) { char ch,c; clrscr(); printf("Enter a string in upper case:"); while(( ch = getchar( ) ) != '\n')   {      c = ch + 32;      putchar(c);   } printf(" :String In Lower Case"); getch( ); } 30) Sample C Program to accept a string in any case and print it by another case. # include <stdio.h> # include <conio.h> main( ) { char ch; clrscr( ); printf("Enter a string:");  while(( ch = getchar( )) != '\n') { if(ch >= 'A' && ch <= 'Z') putchar(ch + 32); else ...

Sample C Program to print numerical pyramid.

16) Sample C Program to print numeric pyramid. # include <stdio.h> # include <conio.h> main() { int i, j; clrscr( ); for(i = 1; i <= 5; i++)  {    for(j = 1; j <= i; j++)      printf("%d", j);   printf("\n");  } getch(); } 17) Sample C Program to print numerical pyramid. # include <stdio.h> # include <conio.h> main() { int i, j, l, k = 40; clrscr( ); for(i = 1; i <= 9; i += 2)  {    for(l = 1; l <= k; l++)       printf(" " );      for(j = 1; j <= i ; j++);       printf("%d", j);      printf("\n");    k = k - 2;  } getch( ); } 18) Sample C Program to print numerical diamond. # include <stdio.h> # include <conio.h> main( ) { int i, j, l, n, s, k = 40; clrscr( ); for(i = 1; i <= 9; i += 2)  {     for(l = 1; l <= k; l++)   ...

C Program To Print Pyramid Of Numbers.

19) Sample C Program to print character pyramid. # include <stdio.h> # include <conio.h> main( ) { char i, j; clrscr(); for(i = 65; i <= 70; i++)  {    for(j = 65; j <= i; j++)       printf("%c", j);    printf("\n");  } getch( ); } 20) Sample C Program to print character diamond. # include <stdio.h> # include <conio.h> main( ) { char i, j, n, r; int s, sp = 40; clrscr( ); for(i = 65; i <= 75; i += 2)  {    for(s = 1; s <= sp; s++)       printf(" ");    for(j = 65; j < i; j++)       printf("%c", j);    printf("\n");    sp = sp - 2;  } sp = sp + 4; for(n = 73; n >= 65; n -= 2)  {    for(s = 1; s <= sp; s++)       printf(" ");    for(r = 65; r <= n; r++)       printf("%c",r);    sp = sp + 2;  } getc...

C Program To Find Largest/Smallest Of Four Nos. Using Ternary Operator.

22) Sample C Program to find largest of four nos. by using Ternary Operator. # include <stdio.h> # include <conio.h> main( ) { int a, b, c, d, big; clrscr( ); printf("Enter the value of a:"); scanf("%d",&a); printf("Enter the value of b:"); scanf("%d",&b); printf("Enter the value of c:"); scanf("%d",&c); printf("Enter the value of d:”); scanf("%d",&d); big = (a > b)?(a > c)?(a > d)?a:d:(c > d)?c:d:(b > c)?(b > d)?b:d:(c > d)?c:d; printf("Biggest of the given 4 numbers is %d", big); getch(); } 23) Sample C Program to find smallest of four nos. by using Ternary Operator. # include <stdio.h> # include <conio.h> main( ) { int a, b, c, d, small; clrscr( ); printf("Enter the value of a:"); scanf("%d",&a); printf("Enter the value of b:"); scanf("%d",&b); printf(...

C Program To Print Multiplication Table Using For Loop.

13) Sample C Program to accept a number and print mathematical table of the given no. # include <stdio.h> # include <conio.h> main( ) { int i, t; clrscr( ); printf("Which table you want:"); scanf("%d",&t); for (i = 1; i <= 10; i++)    printf("\n%d * %d = %d", t, i, i * t); getch( ); } 14) Sample C Program to print 1 to 10 mathematical tables. # include <stdio.h> # include <conio.h> main( ) { int i, j; clrscr( ); for (i = 1; i <= 10; i++)   for (j = 1; j <= 10; j++)     printf("\n%d * %d = %d", i, j, i * j); getch( ); } 15) Sample C Program to print Fibonacci series. # include <stdio.h> # include <conio.h> main( ) { int a = 0, b = 1, c = 0, i; clrscr( ); printf("%d",a); printf("\n%d",b); for (i = 1; i <= 10; i++)  {    c = a + b;    printf("\n%d", c);    a = b;    b = c;  } getch( ); }

C Program To Print ODD Numbers From 1 To 100.

10) Sample C Program to print ODD numbers from 1 to 100. # include <stdio.h> # include <conio.h> main( ) { int i; clrscr( ); for (i = 1; i <= 100; i += 2) printf("%d\n",i); getch( ); } 11) Sample C Program to print natural numbers from 1 to 100 in reverse order. # include <stdio.h> # include <conio.h> main( ) { int i; clrscr( ); for (i = 100; i >= 1; i --) printf(“%d\n”,i); getch( ); } 12) Sample C Program to print sum of the natural numbers from 1 to 100. # include <stdio.h> # include <conio.h> main( ) { int n, sum = 0, i; clrscr( ); for (i = 1; i <= 100; i ++) sum = sum + i; printf("Sum of natural numbers from 1 to 100 is %d\n",sum); getch( ); }

C Program to Check Given No. Is Armstrong Number Or Not.

37) Sample C Program to accept a number and check whether the given number is an Armstrong number or not. # include <stdio.h> # include <conio.h> main( ) { int n, arm; clrscr(); printf("Enter any 3 digit number:") scanf("%d",&n); arm = armstrong (n); if(arm == n)    printf("%d is an  Armstrong number. ",n); else    printf("%d is not an Armstrong number .",n); getch( ); } int armstrong (int n) { int a, b, c, d; a = n / 100; b = ( (n / 10) % 10); c = n % 10; d = a*a*a + b*b*b + c*c*c; return d; } 38) Sample C Program to accept a number and print the sum of given and reverse number. # include <stdio.h> # include <conio.h> main( ) { int a, b, n; clrscr( ); printf("Enter a number:") scanf("%d",&n); a = rev(n); printf("Reverse of a given number is: %d", a); b = add(n, a); printf("\n Sum of a given and reverse number is: %d", b)...

C Program To Convert Uppercase To Lowercase.

25) Sample C Program to accept a character in Uppercase & print in Lowercase. # include <stdio.h> # include <conio.h> main( ) { char ch, c1; clrscr( ); printf("Enter a character in Uppercase:"); ch = getchar(); c1 = ch + 32; printf("The given character in Lowercase is:"); putchar(c1); getch(); } 26) Sample C Program to accept a character in any case and print it in another case. # include <stdio.h> # include <conio.h> main( ) { char ch, c1; clrscr( ); printf("Enter a character in any case(Uppercase/Lowercase):"); ch = getchar(); if(ch >= 65 && ch <= 90)    c1 = ch + 32; elseif(ch >= 97 && ch <= 122)    c1 = ch - 32; endif; printf("The given char in other case is:"); putchar(c1); getch(); } 27) Sample C Program to print natural number from 1 to 10 by using while loop. # include <stdio.h> # include <conio.h> main( ) { int a = 0...

C Program To Find Prime Number Between 1 And 100.

34) Sample C Program to print prime numbers between 1 to 100. # include <stdio.h> # include <conio.h> main( ) { int n, i, check; clrscr(); for(i = 1; i <= 100; i++) { check = 1; for(n = 2; n <= i / 2; n++) if(i % n == 0) { check = 0; break; } if(check == 1) printf("\n %d is a prime", i); else printf("\n %d is not a prime", i); } getch( ); } 35) Sample C Program to accept two numbers and print sum of two numbers by using functions. # include <stdio.h> # include <conio.h> main( ) { int a, b, c; clrscr(); printf("Enter the value for a:") scanf("%d",&a); printf("Enter the value for b:") scanf("%d",&b); c = add(a, b); printf("Sum of two numbers is %d",c); getch( ); } int add(int x, int y) { int z; z = x + y; return z; } 36) Sample C Program to accept a number and find factorial of given number. # include <stdio.h> # incl...

String Programs In C Programming Asked In Interviews.

31) Sample C Program to accept a string and print each word in new line. # include <stdio.h> # include <conio.h> main( ) { char ch; clrscr( ); printf("Enter a string :"); while( ( ch = getchar( ) ) != '\n') { putchar(ch); if(ch == ' ') printf("\n"); } getch( ); } 32) Sample C Program to accept a string and count no of capital letters, no. of small letters and no. of special characters. # include <stdio.h> # include <conio.h> main( ) { char ch; int c = 0,s = 0,s1 = 0; clrscr( ); printf("Enter a string :"); while( ( ch = getchar( ) ) != '\n')   {      if(ch >= 'A' && ch >= 'Z')         c = c + 1;      else if(ch >= 'a' && ch >= 'z')         s = s + 1;      else         s1 = s1 + 1;   } printf("No. of capital letters are %d", c); printf("No. of small letters are %d", s); printf("N...

Sample C Program To Implement Minimum Spanning Tree Using Functions, Pointers & Structures.

ALGORITHM: STEP 1: Start the program. STEP 2: Assign MAX a constant value of 20. STEP 3: Declare a structure edge with structure variable *front. STEP 4: Define the functions and variables required in the program globally. STEP 5: Call the function create_graph() inside the main function. STEP 6: Call the function make_tree(). STEP 7: Set a for loop using i. for(i = 1; i <= count; i++) STEP 8: Print the values of tree[i].u and tree[i].v STEP 9: Stop the program. FUNCTION OF CREATE_GRAPH ( ) STEP 1: Declare the local variables. STEP 2: Read the number of nodes n. STEP 3: Calculate the value of max_edge as max_edge = n * (n - 1) / 2. STEP 4: Set a for loop using i. for(i = 0; i <= max_edge; i++) STEP 5: Read the values of origin and destiny. STEP 6: Check whether origin and destiny are equal to 0. STEP 7: If so exit the loop using break statement STEP 8: Read the weight of the current edge wt. STEP 9: Check the following condition. if(or...

Sample C Program To Implement 8 Queens Problem Using Arrays, Pointers & Functions.

ALGORITHM: STEP 1: Define the functions that are to be used. STEP 2: Assign a constant value of 8 to QUEENNO. STEP 3: Call the function placequeen(0, x). STEP 4: Print the message “End”. STEP 5: Stop the program. FUNCTION VOID PLACEQUEEN(int k, int *x) STEP 1: Declare local variables STEP 2: Set a for loop for i. for(i = 0; i < 8; i++) STEP 3: Check for result of function canbeplaced(k, I, x). STEP 4: If it is 1 then assign the value of i to x[k]. x[k] = i; STEP 5: Check if k is equal to 7 if show call the function showboard(x). STEP 6: Read the value of ch from the user. STEP 7: If the value is equal to n or N then exit. STEP 8: Check whether k is less than 7 if so then call the function placequeen(k + 1, x). FUNCTION INT CANBEPLACED(int k, int i, int *x) STEP 1: Declare the local variables. STEP 2: Set a for loop for j. for(j = 0; j < k; j++) STEP 3: Check for the following condition if( (abs(j - k) == abs(x[j] - i) || (x[j] == i) ) ) S...

Sample C Program To Implement Quick Sort Using Pointers, Functions & Arrays.

ALGORITHM: STEP 1: Start the program. STEP 2: Assign the pointer array *a[100] as global, Read the value of n. STEP 3: Set a for loop to read the elements of array. for(i = 0; i < n; i++) STEP 4: Call the function sort(0, n - 1). STEP 5: Print the sorted array a. STEP 6: Stop the program FUNCTION SORT (int first, int last) STEP 1: Declare the local variable. STEP 2: Check if first is less than last. first < last STEP 3: If so then assign the following pivot = a[first] i = first j = last STEP 4: Assign a while loop till the condition. I < j STEP 5: Assign a while loop to increment i till a[i] < pivot and i < last STEP 6: Assign a while loop to decrement j till a[j] > pivot and j > first STEP 7: Check whether i is than j if so then swap the values of a[i] and a[j] temp = a[i] a[j] = a[j] a[j] = temp STEP 8: Then swap the values of a[j] and a[first]. temp = a[j] a[j] = a[first] a[first] =temp STEP 9: Call another f...

Sample C Program To Implement Binary Search Without Using Recursive Function.

ALGORITHM: STEP 1: Start the program. STEP 2: Read the value of n. STEP 3: Set a for loop to read the elements of array. for(i = 0; i < n; i++) STEP 4: Set a for loop. for(i = 0; i < n; i++) STEP 5: Nest another for loop. for(j = i + 1; j < n; j++) STEP 6: Check the condition a[i] > a[j] STEP 7: If so swap the two values using temporary variable t as t = a[i] a[i] = a[j] a[j] = t STEP 8: Else go back to step 6. STEP 9: Set a for loop to print the value of array a. for(i = 0; i < n; i++) STEP 10: Read the search key as k. STEP 11: Assign low = 0 and high = n – 1. STEP 12: Call the function binsearch(a, k, low, high) STEP 13: Check if ans is not equal to 1, if so print the position b + i. else print that element is not found. STEP 14: Stop the program. FUNCTION BINARY SEARCH (int *a[ ], int x, int low, int high) STEP 1: Check if low > high if so return -1. STEP 2: Else assign mean value of low and high to mid. mid = ( high + ...

Sample C Program To Implement Binary Search Using Recursive Function.

ALGORITHM: STEP 1: Start the program. STEP 2: Read the value of n. STEP 3: Set a for loop to read the elements of array. for(i = 0; i < n; i++) STEP 4: Set a for loop. for(i = 0; i < n; i++) STEP 5: Nest another for loop. for(j = i + 1; j < n; j++) STEP 6: Check the condition a[i] > a[j]. STEP 7: If so swap the two values using temporary variable t as t = a[i] a[i] = a[j] a[j] = t STEP 8: Else go back to step 6. STEP 9: Set a for loop to print the value of array a. for(i = 0; i < n; i++) STEP 10: Read the search key as k. STEP 11: Assign low = 0 and high = n – 1. STEP 12: Call the function binsearch(a, k, low, high) STEP 13: Check if ans is not equal to 1, if so print the position b + i. Else print that element is not found. STEP 14: Stop the program. FUNCTION BINARY SEARCH (int *x[ ], int x, int low, int high) STEP 1: Set a while loop till low is greater than high. STEP 2: Assign mean value of low and high to mid. mid = (high...