Unit-1 Programs
Unit-1 Programs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=50, b=60, c=0;
int *p, *q;
p=&a;
q=&b;
c=(*p)+(*q);
int main()
{
int a=50;
int *p=&a;
3. Pointers to array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5]={2,4,6,8,10};
int *p,i;
p=a;
for(p=a;p<a[5];p++)
printf("%d\t",*(p+i));
}
4. Pointers to pointers.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int *p;
int **q;
p=&a;
q=&p;
a=50;
printf("\n");
printf("Address of a is %u\n", &a);
printf("Address of a is %u\n", p);
printf("Address of a is %u\n", *q);
}
#define size 5
int stack[size];
int top=-1;
void pop()
{
if(top==-1)
{
printf("Stack underflow");
}
else
{
int pele=stack[top];
top--;
printf("%d element popped from the stack", pele);
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack empty");
}
for(i=top;i>=0;i--)
printf("%d",stack[i]);
}
6.Palindrome
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
int top = -1, front = 0;
int stack[MAX];
void push(char);
void pop();
int main()
{
int i, choice;
char s[MAX], b;
while (1)
{
printf("1-enter string\n2-exit\n");
printf("enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the String\n");
scanf("%s", s);
for (i = 0;s[i] != '\0';i++)
{
b = s[i];
push(b);
}
for (i = 0;i < (strlen(s) / 2);i++)
{
if (stack[top] == stack[front])
{
pop();
front++;
}
else
{
printf("%s is not a palindrome\n", s);
break;
}
}
if ((strlen(s) / 2) == front)
printf("%s is palindrome\n", s);
front = 0;
top = -1;
break;
case 2:
exit(0);
default:
printf("enter correct choice\n");
}
}
}
6. Balanced parathesis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
7. String reverse.
#include <stdio.h>
#include <string.h>
void pop(){
// Pop (Removing element from stack)
printf("%c",stack[top--]);
}
main()
{
char str[]="sri lanka";
int len = strlen(str);
int i;
for(i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
}
8. Given class starting time and duration, calculate the class ending time
using structures.
#include <stdio.h>
#include <stdlib.h>
struct TIME
{
int h;
int m;
int s;
};
int main()
{
struct TIME st, dt, et;
printf("Enter class starting time in hours, minutes and seconds\n");
scanf("%d%d%d",&st.h, &st.m, &st.s);
et.h = st.h+dt.h;
et.m = st.m+dt.m;
et.s = st.s+dt.s;
if(et.s>60)
{
et.s=et.s-60;
et.m++;
}
if(et.m>60)
{
et.m=et.m-60;
et.h++;
}
11.Nested structures.
#include <stdio.h>
#include <stdlib.h>
struct student
{
int rno;
char name[10];
struct marks
{
int m1, m2, total;
}m;
}s;
int main()
{
printf("Enter details\n");
printf("\nEnter roll number\t");
scanf("\t%d",&s.rno);
printf("\nEnter NAME\t");
scanf("\t%s",&s.name);
printf("\nEnter marks in 2 subjects\t");
scanf("%d\t%d",&s.m.m1, &s.m.m2);
s.m.total=s.m.m1+s.m.m2;
printf("Total=%d\n",s.m.total);
}
12.Nested structures.
#include <stdio.h>
#include <stdlib.h>
struct marks
{
int m1, m2, total;
};
struct student
{
int rno;
char name[10];
struct marks m;
};
int main()
{
struct student s;
printf("Enter details\n");
printf("\nEnter roll number\t");
scanf("\t%d",&s.rno);
printf("\nEnter NAME\t");
scanf("\t%s",&s.name);
printf("\nEnter marks in 2 subjects\t");
scanf("%d\t%d",&s.m.m1, &s.m.m2);
s.m.total=s.m.m1+s.m.m2;
printf("Total=%d\n",s.m.total);
}
struct marks
{
int m1, m2, total;
};
struct student
{
int rno;
char name[10];
struct marks m;
};
int main()
{
struct student s;
printf("Enter details\n");
printf("\nEnter roll number\t");
scanf("\t%d",&s.rno);
printf("\nEnter NAME\t");
scanf("\t%s",&s.name);
printf("\nEnter marks in 2 subjects\t");
scanf("%d\t%d",&s.m.m1, &s.m.m2);
s.m.total=s.m.m1+s.m.m2;
printf("Total=%d\n",s.m.total);
}
p=&a;
q=&b;
r=&temp;
*r=*p;
*p=*q;
*q=*r;