Documente Academic
Documente Profesional
Documente Cultură
struct student
char name[20];
int roll;
float marks;
}s1;
int main()
scanf("%s",&s1.name);
scanf("%d",&s1.roll);
scanf("%f",&s1.marks);
printf("\nName=> %s",s1.name);
printf("\nRoll=> %d",s1.roll);
printf("\nMarks=> %f",s1.marks);
return 0;
Output:
2.Write a program in C to add two distances (in inch-feet) using structure.
Code:
#include<stdio.h>
struct add
float d1,d2,sum;
}s1;
int main()
scanf("%f",&s1.d1);
scanf("%f",&s1.d2);
s1.sum = s1.d1+s1.d2;
return 0;
Output:
3. Write a program in C to calculate the difference between two time periods.
Code:
#include<stdio.h>
struct tp
int h,m,s,d;
}t1,t2,d1,d2,d3;
int main()
scanf("%d",&t1.h);
scanf("%d",&t1.m);
scanf("%d",&t1.s);
scanf("%d",&t2.m);
scanf("%d",&t2.s);
d1.d=t1.h-t2.h;
d2.d=t1.m-t2.m;
d3.d=t1.s-t2.s;
return 0;
Output:
struct student
char name[20];
int roll;
float marks;
}s[10];
int main()
int i;
for(i=0;i<10;i++)
scanf("%s",&s[i].name);
scanf("%d",&s[i].roll);
scanf("%f",&s[i].marks);
for(i=0;i<10;i++)
printf("\nName=> %s",s[i].name);
printf("\nRoll=> %d",s[i].roll);
printf("\nMarks=> %f",s[i].marks);
return 0;
output:
5.Write a program in c to store information using structures for n elements
dynamically.
Code:
#include<stdio.h>
#include<stdlib.h>
struct student
int num;
};
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
printf("\n%d",p[i]);
return 0;
Output:
6.Write a program in c to access array members using structure.
Code:
#include<stdio.h>
struct student
char name[20];
int roll;
float marks;
};
int main()
int i;
for(i=0;i<3;i++)
scanf("%s",&s[i].name);
scanf("%d",&s[i].roll);
scanf("%f",&s[i].marks);
for(i=0;i<3;i++)
printf("\n\nName=> %s",s[i].name);
printf("\n\nRoll=> %d",s[i].roll);
printf("\n\nMarks=> %f",s[i].marks);
return 0;
}
Output:
struct student
char name[20];
int roll;
float marks;
};
int main()
int i;
scanf("%s",&s[i].name);
scanf("%d",&s[i].roll);
scanf("%f",&s[i].marks);
t=s;
for(i=0;i<3;i++)
printf("\n\nName=> %s",t[i].name);
printf("\n\nRoll=> %d",t[i].roll);
printf("\n\nMarks=> %f",t[i].marks);
return 0;
Output:
8.Write a program in c to store employee details in a structure named
employee.Store address of employee in another structure named address
which is nested .Then print the total information of the employee uing the
concept of nested structure.
Code:
#include<stdio.h>
#include<stdlib.h>
struct address
char adrs[30];
};
struct employee
int empid;
char name[20];
char mobno[11];
};
int main()
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&p[i].empid);
printf("\nName\n");
scanf("%s",p[i].name);
printf("\nAddress\n");
scanf("%s",p[i].ads.adrs);
printf("\nMobile number\n");
scanf("%s",p[i].mobno);
for(i=0;i<n;i++)
printf("\n\nEmployee id\n\n");
printf("%d",p[i].empid);
printf("\n\nName\n");
printf("%s",p[i].name);
printf("\n\nAddress\n");
printf("%s",p[i].ads.adrs);
printf("\nMobile number\n");
printf("%s",p[i].mobno);
free(p);
return 0;
Output:
#define MAX 50
int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *);
void infix_to_postfix(char infix[],char postfix[]);
int main()
{
char infix[30],postfix[30];
printf("Enter an infix expression(eg: 5+2*4): ");
gets(infix);
infix_to_postfix(infix,postfix);
printf("\nPostfix expression: %s",postfix);
}
while(!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
postfix[j]='\0';
}
int precedence(char x)
{
if(x=='(')
return(0);
if(x=='+'||x=='-')
return(1);
if(x=='*'||x=='/'||x=='%')
return(2);
return(3);
}
return(0);
}
return(0);
}
Output:
11. Write a C program to convert infix expression into
prefix expression.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int pop();
int precedence(char symbol);
int isEmpty();
void infix_to_prefix();
int checker(char symbol);
void push(long int symbol);
int main()
{
int count, length;
char temp;
top = -1;
printf("\nEnter an Expression in Infix format:\t");
scanf("%[^\n]s", infix_string);
infix_to_prefix();
printf("\nExpression in Postfix Format: \t%s\n", postfix_string);
length = strlen(postfix_string) - 1;
strncpy(prefix_string, postfix_string, 20);
for(count = 0; count < length; count++, length--)
{
temp = prefix_string[count];
prefix_string[count] = prefix_string[length];
prefix_string[length] = temp;
}
printf("\nExpression in Prefix Format: \t%s\n", prefix_string);
return 0;
}
void infix_to_prefix()
{
unsigned int count, temp = 0;
char next;
char symbol;
for(count = 0; count < strlen(infix_string); count++)
{
symbol = infix_string[count];
if(!checker(symbol))
{
switch(symbol)
{
case '(': push(symbol);
break;
case ')':
while((next = pop()) != '(')
{
postfix_string[temp++] = next;
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while(!isEmpty() && precedence(stack[top]) >=
precedence(symbol))
postfix_string[temp++] = pop();
push(symbol);
break;
default:
postfix_string[temp++] = symbol;
}
}
}
while(!isEmpty())
{
postfix_string[temp++] = pop();
}
postfix_string[temp] = '\0';
}
int isEmpty()
{
if(top == -1)
{
return 1;
}
else
{
return 0;
}
}
int pop()
{
if(isEmpty())
{
printf("Stack is Empty\n");
exit(1);
}
return(stack[top--]);
}
Output:
12. Write a C program to convert prefix expression into
infix expression
Code:
#include<stdio.h>
#include <string.h>
#include <ctype.h>
char pop_operator()
{
return(operator_stack[top_operator--]);
}
char *pop_operand()
{
return(operand_stack[top_operand--]);
}
int empty(int t)
{
if(t == 0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char prefix_expression[50], ch, temporary_string[50], operand_a[50],
operand_b[50], operator[2];
int count = 0, k = 0, operand_count = 0;
printf("\nEnter a Prefix Expression:\t");
scanf("%s", prefix_expression);
while((ch = prefix_expression[count++]) != '\0')
{
if(isalnum(ch))
{
temporary_string[0] = ch;
temporary_string[1]='\0';
push_operand(temporary_string);
operand_count++;
if(operand_count >= 2)
{
strcpy(operand_b, pop_operand());
strcpy(operand_a, pop_operand());
strcpy(temporary_string, "(");
strcat(temporary_string, operand_a);
ch = pop_operator();
operator[0] = ch;
operator[1] = '\0';
strcat(temporary_string, operator);
strcat(temporary_string, operand_b);
strcat(temporary_string, ")");
push_operand(temporary_string);
operand_count = operand_count - 1;
}
}
else
{
push_operator(ch);
if(operand_count == 1)
{
operand_count = 0;
}
}
}
if(!empty(top_operand))
{
strcpy(operand_b, pop_operand());
strcpy(operand_a, pop_operand());
strcpy(temporary_string, "(");
strcat(temporary_string, operand_a);
ch = pop_operator();
operator[0] = ch;
operator[1] = '\0';
strcat(temporary_string, operator);
strcat(temporary_string, operand_b);
strcat(temporary_string, ")");
push_operand(temporary_string);
}
printf("\nInfix Expression:\t %s\n", operand_stack[top_operand]);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
# define MAX 20
char str[MAX], stack[MAX];
int top = -1;
char pop()
{
return stack[top--];
}
int main()
{
char postfix_expression[50];
printf("\nEnter Postfix Expression:\t");
scanf("%s", postfix_expression);
postfix_to_infix(postfix_expression);
printf("\n");
return 0;
}
Output:
#include <ctype.h>
#define MAXSTACK 100
int stack[MAXSTACK];
return;
else {
top = top + 1;
stack[top] = item;
int pop()
int item;
if (top < 0) {
else {
item = stack[top];
top = top - 1;
return item;
{
int i;
char ch;
int val;
int A, B;
ch = postfix[i];
if (isdigit(ch)) {
push(ch - '0');
A = pop();
B = pop();
switch (ch)
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
push(val);
int main()
int i;
char postfix[POSTFIXSIZE];
printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");
scanf("%c", &postfix[i]);
if (postfix[i] == ')')
break;
EvalPostfix(postfix);
return 0;
Output:
#define SIZE 10
int ar[SIZE];
ar[++top1] = data;
else
ar[--top2] = data;
else
{
printf ("Stack Full! Cannot Push\n");
}
void pop_stack1 ()
if (top1 >= 0)
else
void pop_stack2 ()
{
if (top2 < SIZE)
else
void print_stack1 ()
int i;
printf ("\n");
void print_stack2 ()
int i;
printf ("\n");
int main()
int ar[SIZE];
int i;
int num_of_ele;
push_stack1 (i);
push_stack2 (i);
print_stack1 ();
print_stack2 ();
push_stack1 (11);
num_of_ele = top1 + 1;
while (num_of_ele)
pop_stack1 ();
--num_of_ele;
}
pop_stack1 ();
return 0;
}
Output:
16. C program to check whether expression is correctly
parenthesized using stack.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top = -1;
char stack[100];
void push(char);
void pop();
void find_top();
intmain()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s", &a);
if (a[i] == '(')
push(a[i]);
pop();
}
find_top();
void push(char a)
stack[top] = a;
top++;
void pop()
if (top == -1)
printf("expression is invalid\n");
exit(0);
else
{
top--;
void find_top()
if (top == -1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}
Output: