C Programming Examples

Interview Questions on C Programming Examples

1) Write a program to add two numbers?
#include <stdio.h>

int main(void) {
int first, second;
printf("Enter two integers > ");
scanf("%d %d", &first, &second);
printf("The two numbers are: %d %d\n", first, second);
printf("Their sum is %d\n", first+second);
}

2) Specify the skeletons of two C loops with the test at the top?
next = 0; /* setup */

while ( next < max) { /* test */
printf("Hello "); /* body */
next++; /* update */
}

3) Write a program for a simple count from 1 to 100?
#include <stdio.h>

int
main (void)
{
int n;

n = 1; /* loop initialization */

/* the loop */
while (n <= 100) /* loop condition */
{
printf ("%d ", n); /* loop body */
n = n + 1; /* loop update */
}

return (0);
}

4) Give a sample to determine if a number is a prime?
#include <stdio.h>

int main(void) {
int n;
int i;
int flag;

printf("Enter value of N > ");
scanf("%d", &n);
flag = 1;
for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
values of i greater than the square root of n? */
if ((n % i) == 0) /* If true n is divisible by i */
flag = 0;
else
i++;
}

if (flag)
printf("%d is prime\n", n);
else
printf("%d has %d as a factor\n", n, i);
return 0;
}

5) Computing Fibonacci numbers?
A program for computing Fibonacci numbers:
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;

//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);

printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.

for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}

return 0;
}

Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

6) Finding all the proper factors of a number using C?
#include <stdio.h>

int main(void) {
int n,
lcv,
flag; /* flag initially is 1 and becomes 0 if we determine that n
is not a prime */

printf("Enter value of N > ");
scanf("%d", &n);
for (lcv=2, flag=1; lcv <= (n / 2); lcv++) {
if ((n % lcv) == 0) {
if (flag)
printf("The non-trivial factors of %d are: \n", n);
flag = 0;
printf("\t%d\n", lcv);
}
}
if (flag)
printf("%d is prime\n", n);
}

7) Write a program for finding the value of a collection of coins?
#include <stdio.h>

void main ()
{
// Local data ...
int pennies; // input: count of pennies
int nickels; // input: count of nickels
int dimes; // input: count of dimes
int quarters; // input: count of quarters
int temp, left; // temporaries for various
// computations

// Read in the count of quarters, dimes, nickels and pennies.
printf("Enter the number of quarters, dimes, nickels, and pennies: ");
scanf("%d %d %d %d", &quarters, &dimes, &nickels, &pennies);

// Compute the total value in cents.
left = 25 * quarters + 10 * dimes + 5 * nickels + pennies;

// Find and display the value in dollars
printf("Your collection is worth\n ");
temp = left / 100;
printf("\t%d dollar", temp);
if (temp==1)
printf(", ");
else
printf("s, ");
left = left % 100;

// Find and display the value left in quarters
temp = left / 25;
printf("%d quarter", temp);
if (temp==1)
printf(", ");
else
printf("s, ");
left = left % 25;

// Find and display the value left in dimes
temp = left / 10;
printf("%d dime", temp);
// Here, just for fun, instead of using a conditional statement,
// I use a conditional expression and string concatenation
printf ((temp==1) ? ", " : "s, ");
left = left % 10;

// Find and display the value left in nickels
temp = left / 5;
printf("%d nickel", temp);
if (temp==1)
printf(", and ");
else
printf("s, and ");
left = left % 5;

// Find and display the value left in pennies
printf("%d penn", left);
if (left==1)
printf("y\n");
else
printf("ies\n");
}

8) Write a program to add a sequence of positive integers?
#include <stdio.h>
#define SENTINEL 0

int main(void)
{
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */

do {
printf("\nEnter an integer > ");
scanf("%d", &current);
if (current > SENTINEL)
sum = sum + current;
} while (current > SENTINEL);
printf("\nThe sum is %d\n", sum);
}

9) Write a program to add n integers?
#include <stdio.h>

int main(void)
{
int n; /* The number of numbers to be read */
int sum; /* The sum of numbers already read */
int current; /* The number just read */
int lcv; /* Loop control variable, it counts the number
of numbers already read */

printf("Enter a positive number n > ");
scanf("%d",&n); /* We should check that n is really positive*/
sum = 0;
for (lcv=0; lcv < n; lcv++) {
printf("\nEnter an integer > ");
scanf("%d",&current);
/* printf("\nThe number was %d\n", current); */
sum = sum + current;
}
printf("The sum is %d\n", sum);
return 0;
}

10) Specify a C loop with the test at the bottom?
next = 0; /* setup */

do {
printf("Hello"); /* body */
next++; /* update */
} while ( next < max); /* test */


and

for ( next = 0; next < max; next++) /* setup,test */
/* and update */
printf("Hello"); /* body */

11) Print out powers of 2: 1, 2, 4, 8, .. up to 2^N?
#include <stdio.h>
#define N 16

int main(void) {
int n; /* The current exponent */
int val = 1; /* The current power of 2 */

printf("\t n \t 2^n\n");
printf("\t================\n");
for (n=0; n<=N; n++) {
printf("\t%3d \t %6d\n", n, val);
val = 2*val;
}
return 0;
}

12) Give an example for factorial program in C using for loop?
#include <stdio.h>
int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);

for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}

0 comments:

Post a Comment