0% found this document useful (0 votes)
85 views18 pages

C - Program For Interview

The document provides C program code snippets for various programming problems including: 1) Printing a pattern using nested for loops. 2) Solving the Tower of Hanoi problem recursively. 3) Printing a numeric pattern using nested for loops. 4) Converting a decimal number to binary. 5) Finding the transpose of a matrix. 6) Sorting strings in dictionary order. 7) Implementing binary search. 8) Reversing a string recursively. 9) Comparing two strings without library functions. 10) Finding the largest element using dynamic memory allocation. 11) Sorting a stack recursively.

Uploaded by

Myphone Myphone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views18 pages

C - Program For Interview

The document provides C program code snippets for various programming problems including: 1) Printing a pattern using nested for loops. 2) Solving the Tower of Hanoi problem recursively. 3) Printing a numeric pattern using nested for loops. 4) Converting a decimal number to binary. 5) Finding the transpose of a matrix. 6) Sorting strings in dictionary order. 7) Implementing binary search. 8) Reversing a string recursively. 9) Comparing two strings without library functions. 10) Finding the largest element using dynamic memory allocation. 11) Sorting a stack recursively.

Uploaded by

Myphone Myphone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C – Program for Interview

1. Write a C program to print the following pattern

Solution:

#include<stdio.h>

int main()

int n, c, k, space, count = 1;

printf("Enter number of rows\n");

scanf("%d", &n);

space = n;

for (c = 1; c <= n; c++)

for (k = 1; k < space; k++)

printf(" ");

for (k = 1; k <= c; k++)

printf("*");

if (c > 1 && count < c)


{

printf("A");

count++;

printf("\n");

space--;

count = 1;

return 0;

2. Write a C Program to Solve Tower-of-Hanoi Problem using Recursion

include <stdio.h>

void towers(int, char, char, char);

int main()
{
int num;

printf("Enter the number of disks : ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

OUTPUT
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

3. Write a C Program to print the following pattern

#include <stdio.h>
int main() {
int i,j;
for (i=1;i<=4;i++) {
for (j=1;j<=i;j++) {
printf("%d ", 9+i+j);
}
printf("\n");
}
return 0;
}

4. Write a C program to perform Decimal to binary conversion


#include<stdio.h>

int main(){

long int decimalNumber,remainder,quotient;

int binaryNumber[100],i=1,j;

printf("Enter any decimal number: ");

scanf("%ld",&decimalNumber);

quotient = decimalNumber;

while(quotient!=0){

binaryNumber[i++]= quotient % 2;

quotient = quotient / 2;

printf("Equivalent binary value of decimal number %d: ",decimalNumber);

for(j = i -1 ;j> 0;j--)

printf("%d",binaryNumber[j]);
return 0;

Sample output:

Enter any decimal number: 50


Equivalent binary value of decimal number 50: 110010

5. Write a Program to Find Transpose of a Matrix


#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}
Output

Enter rows and columns of matrix: 2


3

Enter element of matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix:
2 3 4
5 6 4

Transpose of Matrix:
2 5
3 6
4 4
6. Write a Program to Sort Strings in Dictionary Order
#include<stdio.h>
#include <string.h>
int main()
{
int i, j;
char str[10][50], temp[50];
printf("Enter 10 words:\n");
for(i=0; i<10; ++i)
scanf("%s[^\n]",str[i]);
for(i=0; i<9; ++i)
for(j=i+1; j<10 ; ++j)
{
if(strcmp(str[i], str[j])>0)
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
printf("\nIn lexicographical order: \n");
for(i=0; i<10; ++i)
{
puts(str[i]);
}
return 0;
}

Output:

Enter 10 words:
C
C++
Java
PHP
Python
Perl
Ruby
R
JavaScript
PHP

In lexicographical order:
C
C++
Java
JavaScript
PHP
PHP
Perl
Python
R
Ruby

7. Write a C program to implement binary search


#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}

8. Write a C program to reverse the given string using recursion


#include <stdio.h>

void swap(char *x, char *y)


{
char temp = *x;
*x = *y;
*y = temp;
}

void reverse(char *str, int k)


{
static int i = 0;
// if we have reached the end of the string
if (*(str + k) == '\0')
return;

reverse(str, k + 1);

if (i <= k)
swap(&str[i++], &str[k]);
}

int main()
{
char str[] = "Techie Delight";

reverse(str, 0);
printf("Reverse of the given string is : %s", str);

return 0;
}

9. Program to compare two strings without using library function in C

#include <stdio.h>
#include <ctype.h>

int stringCmp (char *s1,char *s2);


int stringCmpi(char *s1,char *s2);

int main()
{
char str1[100],str2[100];

printf("Enter string 1 : ");


scanf("%[^\n]s",str1);//read string with spaces

getchar(); //to read enter after first string


printf("Enter string 2 : ");
scanf("%[^\n]s",str2);//read string with spaces

if(!stringCmp(str1,str2))
printf("\n stringCmp :String are same.");
else
printf("\n stringCmp :String are not same.");

if(!stringCmpi(str1,str2))
printf("\n stringCmpi :String are same.");
else
printf("\n stringCmpi :String are not same.");

printf("\n");
return 0;
}

int stringCmp (char *s1,char *s2)


{
int i=0;
for(i=0; s1[i]!='\0'; i++)
{
if(s1[i]!=s2[i])
return 1;
}
return 0;
}

int stringCmpi (char *s1,char *s2)


{
int i=0,diff=0;
for(i=0; s1[i]!='\0'; i++)
{
if( toupper(s1[i])!=toupper(s2[i]) )
return 1;
}
return 0;
}

10. Write a program in C to find the largest element using Dynamic Memory
Allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
float *element;
printf("\n\n Pointer : Find the largest element using Dynamic Memory
Allocation :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input total number of elements(1 to 100): ");
scanf("%d",&n);
element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements
if(element==NULL)
{
printf(" No memory is allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)
{
printf(" Number %d: ",i+1);
scanf("%f",element+i);
}
for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
printf(" The Largest element is : %.2f \n\n",*element);
return 0;
}

11. Write a C program to Sort a stack using recursion


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

struct stack
{
int data;
struct stack *next;
};

void initStack(struct stack **s)


{
*s = NULL;
}

int isEmpty(struct stack *s)


{
if (s == NULL)
return 1;
return 0;
}

void push(struct stack **s, int x)


{
struct stack *p = (struct stack *)malloc(sizeof(*p));

if (p == NULL)
{
fprintf(stderr, "Memory allocation failed.\n");
return;
}

p->data = x;
p->next = *s;
*s = p;
}

int pop(struct stack **s)


{
int x;
struct stack *temp;

x = (*s)->data;
temp = *s;
(*s) = (*s)->next;
free(temp);

return x;
}

int top(struct stack *s)


{
return (s->data);
}

void sortedInsert(struct stack **s, int x)


{

if (isEmpty(*s) || x > top(*s))


{
push(s, x);
return;
}

int temp = pop(s);


sortedInsert(s, x);

push(s, temp);
}

void sortStack(struct stack **s)


{
if (!isEmpty(*s))
{
int x = pop(s);

sortStack(s);

sortedInsert(s, x);
}
}

void printStack(struct stack *s)


{
while (s)
{
printf("%d ", s->data);
s = s->next;
}
printf("\n");
}

int main(void)
{
struct stack *top;

initStack(&top);
push(&top, 30);
push(&top, -5);
push(&top, 18);
push(&top, 14);
push(&top, -3);

printf("Stack elements before sorting:\n");


printStack(top);

sortStack(&top);
printf("\n\n");
printf("Stack elements after sorting:\n");
printStack(top);

return 0;
}

12. Write a C program to merge two files


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

struct stack
{
int data;
struct stack *next;
};

void initStack(struct stack **s)


{
*s = NULL;
}

int isEmpty(struct stack *s)


{
if (s == NULL)
return 1;
return 0;
}

void push(struct stack **s, int x)


{
struct stack *p = (struct stack *)malloc(sizeof(*p));

if (p == NULL)
{
fprintf(stderr, "Memory allocation failed.\n");
return;
}

p->data = x;
p->next = *s;
*s = p;
}

int pop(struct stack **s)


{
int x;
struct stack *temp;
x = (*s)->data;
temp = *s;
(*s) = (*s)->next;
free(temp);

return x;
}

int top(struct stack *s)


{
return (s->data);
}

void sortedInsert(struct stack **s, int x)


{

if (isEmpty(*s) || x > top(*s))


{
push(s, x);
return;
}

int temp = pop(s);


sortedInsert(s, x);

push(s, temp);
}

void sortStack(struct stack **s)


{
if (!isEmpty(*s))
{
int x = pop(s);

sortStack(s);

sortedInsert(s, x);
}
}

void printStack(struct stack *s)


{
while (s)
{
printf("%d ", s->data);
s = s->next;
}
printf("\n");
}

int main(void)
{
struct stack *top;

initStack(&top);
push(&top, 30);
push(&top, -5);
push(&top, 18);
push(&top, 14);
push(&top, -3);

printf("Stack elements before sorting:\n");


printStack(top);

sortStack(&top);
printf("\n\n");

printf("Stack elements after sorting:\n");


printStack(top);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy