This paper aims at proposing a procedure to derive distributed algorithms for distributed consensus-based optimization by using distributed algorithms for network resource allocation and vice versa over switching networks with/without synchronous protocol. It is shown that first-order gradient distributed consensus-based optimization algorithms can be used for finding an optimal solution of distributed resource allocation with synchronous protocol under weaker assumptions than those given in the literature for non-switching (static) networks. It is shown that first-order gradient distributed resource allocation algorithms can be utilized for finding an optimal solution of distributed consensus-based optimization. The results presented here can be applied to time-varying and random directed networks with or without synchronous protocol with arbitrary initialization. As a result, several algorithms can now be used to derive distributed algorithms for both consensus-based optimization and resource allocation, that can overcome limitations of the existing results. While the focus of this paper is on the first-order gradient algorithms, it is to be noted that the results also work with second-order gradient algorithms.