See a problem in else if statement

Dangling else problem

Image
Hello, This problem comes when we use else if statement and do some mistake more describe is given below......... Dangling else problem: One of the major problem with nested if is dangling else problem. The problem arises when both outer if statement inner if statement might pair with common else statement. Thus the compiler is not able to judge to pair the else with which statement. The solution to this problem is given by the compiler that it pairs else with the recent if thus sometimes giving wrong result.                                           Hence the programmer should put early bracket wherever required. This post is may very short because my only aim was to describe this problem. Thank you, Keep supporting............. 

Array Puzzle hackerrank solution in C

 

A Product Array Puzzle

Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i].

Example :

Input: arr[]  = {10, 3, 5, 6, 2}
Output: prod[]  = {180, 600, 360, 300, 900}
3 * 5 * 6 * 2 product of other array 
elements except 10 is 180
10 * 5 * 6 * 2 product of other array 
elements except 3 is 600
10 * 3 * 6 * 2 product of other array 
elements except 5 is 360
10 * 3 * 5 * 2 product of other array 
elements except 6 is 300
10 * 3 * 6 * 5 product of other array 
elements except 2 is 900


Input: arr[]  = {1, 2, 3, 4, 5}
Output: prod[]  = {120, 60, 40, 30, 24 }
2 * 3 * 4 * 5  product of other array 
elements except 1 is 120
1 * 3 * 4 * 5  product of other array 
elements except 2 is 60
1 * 2 * 4 * 5  product of other array 
elements except 3 is 40
1 * 2 * 3 * 5  product of other array 
elements except 4 is 30
1 * 2 * 3 * 4  product of other array 
elements except 5 is 24
My own C++ code not used any algorithm and DS Just only practice for beginners.  
#include<iostream>
using namespace std;
int main()
{
	int n,arr[10],i,prod[10],mult=1,j;
	cout<<"Enter range of array:";
	cin>>n;
	for(i=0;i<n;i++)
	{
		cout<<"Enter elements:";
		cin>>arr[i];
	}
	for(i=0;i<n;i++)	
	{
		for(j=0;j<n;j++)
		{
		if(j==i)
		continue;
		else
		mult=mult*arr[j];
		}
		prod[i]=mult;
		mult=1;
	}
	for(i=0;i<n;i++)
	{
		cout<<prod[i]<<"\n";
	}
}

Comments

Popular posts from this blog

Permutations of Strings hackerrank solution in C || Permutations of Strings

Input and output device

Pointing Devices