C - Program For Interview
C - Program For Interview
C - Program For Interview
Solution:
#include<stdio.h>
int main()
scanf("%d", &n);
space = n;
printf(" ");
printf("*");
printf("A");
count++;
printf("\n");
space--;
count = 1;
return 0;
include <stdio.h>
int main()
{
int num;
OUTPUT
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
#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;
}
int main(){
int binaryNumber[100],i=1,j;
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
printf("%d",binaryNumber[j]);
return 0;
Sample output:
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
int main()
{
int c, first, last, middle, n, search, array[100];
return 0;
}
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;
}
#include <stdio.h>
#include <ctype.h>
int main()
{
char str1[100],str2[100];
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;
}
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;
}
struct stack
{
int data;
struct stack *next;
};
if (p == NULL)
{
fprintf(stderr, "Memory allocation failed.\n");
return;
}
p->data = x;
p->next = *s;
*s = p;
}
x = (*s)->data;
temp = *s;
(*s) = (*s)->next;
free(temp);
return x;
}
push(s, temp);
}
sortStack(s);
sortedInsert(s, x);
}
}
int main(void)
{
struct stack *top;
initStack(&top);
push(&top, 30);
push(&top, -5);
push(&top, 18);
push(&top, 14);
push(&top, -3);
sortStack(&top);
printf("\n\n");
printf("Stack elements after sorting:\n");
printStack(top);
return 0;
}
struct stack
{
int data;
struct stack *next;
};
if (p == NULL)
{
fprintf(stderr, "Memory allocation failed.\n");
return;
}
p->data = x;
p->next = *s;
*s = p;
}
return x;
}
push(s, temp);
}
sortStack(s);
sortedInsert(s, x);
}
}
int main(void)
{
struct stack *top;
initStack(&top);
push(&top, 30);
push(&top, -5);
push(&top, 18);
push(&top, 14);
push(&top, -3);
sortStack(&top);
printf("\n\n");
return 0;
}