0% found this document useful (0 votes)
19 views

Unit-1 Programs

Maths

Uploaded by

samikshapshetti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Unit-1 Programs

Maths

Uploaded by

samikshapshetti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1. Adding 2 numbers using pointer to functions.

#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);

printf("the sum is %d\t", c);


}

2. Displaying variable value and address using pointer.


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

int main()
{
int a=50;
int *p=&a;

printf("Printing the value and address using variable\n");


printf("Value is %d\n", a);
printf("Address is %u\n", &a);

printf("Printing the value and address using pointer\n");


printf("Value is %d\n", *p);
printf("Address is %u\n", p);
}

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("Value of a is %d\n", a);


printf("Value of a is %d\n", *p);
printf("Value of a is %d\n", **q);

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);
}

5. Basic stack operations.


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

#define size 5

int stack[size];
int top=-1;

void push(int value)


{
if(top==size-1)
{
printf("Stack overflow:Element can not be pushed");
}
else
{
top=top+1;
stack[top]=value;
}
}

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");
}
}
}

/* to push a character into stack */


void push(char a)
{
top++;
stack[top] = a;
}

/* to delete an element in stack */


void pop()
{
top--;
}

6. Balanced parathesis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Global variables for stack and top


char stack[MAX_SIZE];
int top = -1;

// Function to push a character onto the stack


void push(char data) {
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\n");
return;
}
top++;
stack[top] = data;
}

// Function to pop a character from the stack


char pop() {
if (top == -1) {
printf("Empty stack!\n");
return ' ';
}
char data = stack[top];
top--;
return data;
}
// Function to check if two characters form a matching pair of parentheses
int is_matching_pair(char char1, char char2) {
if (char1 == '(' && char2 == ')') {
return 1;
} else if (char1 == '[' && char2 == ']') {
return 1;
} else if (char1 == '{' && char2 == '}') {
return 1;
} else {
return 0;
}
}

// Function to check if the expression is balanced


int isBalanced(char* text) {
int i;
for (i = 0; i < strlen(text); i++) {
if (text[i] == '(' || text[i] == '[' || text[i] == '{') {
push(text[i]);
} else if (text[i] == ')' || text[i] == ']' || text[i] == '}') {
if (top == -1) {
return 0; // If no opening bracket is present
} else if (!is_matching_pair(pop(), text[i])) {
return 0; // If closing bracket doesn't match the last opening bracket
}
}
}
if (top == -1) {
return 1; // If the stack is empty, the expression is balanced
} else {
return 0; // If the stack is not empty, the expression is not balanced
}
}
// Main function
int main() {
char text[MAX_SIZE];
printf("Input an expression in parentheses: ");
scanf("%s", text);

// Check if the expression is balanced or not


if (isBalanced(text)) {
printf("The expression is balanced.\n");
} else {
printf("The expression is not balanced.\n");
}
return 0;
}

7. String reverse.
#include <stdio.h>
#include <string.h>

#define max 100


int top,stack[max];

void push(char x){

// Push(Inserting Element in stack) operation


if(top == max-1){
printf("stack overflow");
} else {
stack[++top]=x;
}

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);

printf("Enter class duration in hours, minutes and seconds\n");


scanf("%d%d%d",&dt.h, &dt.m, &dt.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++;
}

printf("The class ends at %d hour %d minute and %d seconds",et.h,et.m,et.s);

9. Reading marks of a student in 2 subjects and calculating the total.


#include <stdio.h>
#include <stdlib.h>
struct student
{
int rno;
char name[10];
int m1,m2;
};
int main()
{
struct student s;
printf("Enter students details");
printf("\nEnter roll number");
scanf("%d",&s.rno);
printf("\nEnter NAME");
scanf("%s",&s.name);
printf("\nEnter marks in 2 subjects");
scanf("%d%d",&s.m1, &s.m2);
int total = s.m1+s.m2;
printf("Total=%d",total);
}

10.Reading marks of n students in 2 subjects and calculating the total.


#include <stdio.h>
#include <stdlib.h>
struct student
{
int rno;
char name[10];
int m1,m2,total;
};
int main()
{
struct student s[10];
printf("Enter students details");
for(int i=0;i<5;i++)
{
printf("\nEnter roll number\t");
scanf("\t%d",&s[i].rno);
printf("\nEnter NAME\t");
scanf("\t%s",&s[i].name);
printf("\nEnter marks in 2 subjects\t");
scanf("%d\t%d",&s[i].m1, &s[i].m2);
s[i].total=s[i].m1+s[i].m2;
printf("Total=%d\n",s[i].total);
}

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);
}

13.Reading marks and displaying the result of n students.


#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);
}

14.Swap 2 numbers using pointers.


#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=50, b=60, temp;
int *p,*q,*r;

printf("\n Values before swapping a=%d\tb=%d", a,b);

p=&a;
q=&b;
r=&temp;

*r=*p;
*p=*q;
*q=*r;

printf("\nValues after swapping a=%d\tb=%d", a,b);


}

You might also like