EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

class Customizedstack

{

private int arr[];//Array to store stack elements

private int top; //acts as a cursor for stack

private int capacity;//maximum size of stack

 

Customizedstack(int size)//5

{

capacity=size;//5

arr=new int[capacity];//0-4

top=-1;

}

 

public void push(int value)//10

{

if(top==capacity-1)//4

{

System.out.println(“stack is overflow”);

}

else

{

arr[++top]=value;

}

 

}

 

public int sum()

{

int total=0;

for(int i=0; i<=top; i++)

{

total=total+arr[i];

}

return total;

}

}

 

class Mystacks

{

public static void main(String args[])

{

Customizedstack c=new Customizedstack(5);

 

c.push(10);

c.push(20);

c.push(30);

 

int sum=c.sum();

 

System.out.println(“sum of all the elements of stack=” +sum);

}

}

Java stack sum of element program

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer