Pronic number in c++ error - c++

#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m;
while(m!=n){
m=i*j;
cout<<m;
i++;
j++;
}
return 0;
}
I want to display Pronic Number ie . 0,2,6,12,20,30,42... When I insert value of n as Pronic Number Code run fine ... and desire output is given.. Bug is when you insert value of n=15 while loop goes to infinity but i want to display till 15 or less than
15.. Here 15 is not a Pronic Number..

Initialize m before using it, plus change your condition to stop the loop when m>n
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m=0;
while(m<=n){
cout<<m<<" ";
i++;
j++;
m=i*j;
}
return 0;
}

Related

Loops not executing in functions C++

Here's my code. Whenever I run the program, I expect that the cout statement in the for loop in the function Binary() to execute. But whenever I input something, it gives me 0
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i<0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
I think the condition in your for loop is the opposite way round than your expect it. You seem to want it to continue looping until i<0. But it will actually loop while i<0. The condition is already not true when you enter the loop, so it will finish immediately without doing any iterations.
You just have to change the loop i check.
#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
int temp=0,res=0;
for(int i=x;i>0;i--){
temp++;
res=res+ pow(10,temp)*(i%2);
i=i/2;
cout<<i<<" "<<res<<endl;
}
return res;
}
int main(){
int x; cin>>x;
cout<<endl<<Binary(x);
return 0;
}
Check the live version here http://cpp.sh/95z74

Why am I not getting any output(linear search)?

#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}

How to avoid TLE when array is used to store 1 *10^5 integer entries

My code is showing TLE when there when number of entries in array(n) is 1 *10^5. What should i do? I saw the submission status and in all cases it ran fine except in the last case when n is 100 000, it shows time limit error.
Problem:: https://codeforces.com/contest/474/problem/B
My solution: https://pastebin.com/5RLBirpF
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
int arr[t];
int arrfreq[t];
int sum=1;
for(int i=0;i<t;i++ )
{
cin>>arr[t];
sum+=arr[t];
arrfreq[i]=sum;
}
int m;
cin>>m;
int qsn[m];
int k;
for(int i =0;i<m;i++)
{
cin>>qsn[i];
}
for(int j =0;j<t;j++)
{
if(k<arrfreq[j])
{
cout<<j+1<<"\n";
break;
}
if(k==arrfreq[j])
{
cout<<j+2<<"\n";
break;
}
}
}
You are using one loop for calculating the number of pile. What you can do instead is find the answer in the same loop in which you are taking the input for qsn. Just take the input of qsn and find the pile number in that loop itself. That will reduce your code's time complexity and remove the TLE error.
I saw other solutions on the internet and found out that std::lower_bound function is what has to be used to avoid T.L.E.
But one thing, as mentioned on internet, this function returns a pointer to the lower bound, then , why are we subtracting c from it and then +1 also(in the 2nd last cout line)??(refer to the code.)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j,cnt=0,ans,x,sum=0;
cin>>n;
int a[n],c[n];
for(i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
c[i]=sum;
}
cin>>m;
int b[m];
for(i=0; i<m; i++) cin>>b[i];
for(j=0; j<m; j++)
{
cout<<lower_bound(c,c+n,b[j])-c+1<<endl;
}
return 0;
}

Return value always 0 unless I use cout

The function should return 156 with a "Z" input but it outputs 0.
If the letter's U-Z, the function finds the largest factor of the letters numeric value other than the value itself and multiplies it by 12.
Tried putting cout in the function and it works somehow. Don't know where's the problem. Someone help.
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int uz(int n);
int main(){
string letter, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphSize=alphabet.size(), l=0, total=0;
cin>>letter;
int letterSize=letter.size();
for(int i=0;i<letterSize;i++){
for(int j=0;j<alphSize;j++){
if(toupper(letter[i])==alphabet[j])
l=j+1;
}
if(l>20&&l<=26){
l=uz(l);}
total+=l;
}
cout<<total<<endl;
return 0;
}
int uz(int n){
int size=1;
int arr[size];
for(int i=1;i<=n;i++){
if(n%i==0){
size++;
arr[size]+=i;
}
}
n=12*arr[size-1];
//cout<<n;
return n;
}

Unexpected results converting decimal number to binary

# include <iostream>
# include <cmath>
using namespace std;
int main()
{
int p;
int n;
int q;
cin>>n;
int r;
r=0;
for (int i=0,n; n>1; i=i+1,n=n/2)
{
p=n%2;
q= p*(pow(10,i));
r=r + q;
}
cout<<r;
system("pause");
return 0;
}
I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results.
For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.
your way is limited and not effient in converting to binary
you should use string it's more helpful and the range is big enough for any number
this is my code for decimal-to-binary
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
long long n;
string s,bin;
stack<string> res;
cin>>n;
while(n>0)
{
if(n%2==0)
s='0';
else
s='1';
res.push(s);
n=n/2;
}
while(!res.empty())
{
bin=bin+res.top();
res.pop();
}
cout<<bin<<endl;
return 0;
}
I hope it will help you.
int i=0,n;
should be
int i=0;
I don't know what you thought you were doing there, but what you are actually doing is declaring another variable n. Because the second n variable doesn't have a value the rest of the code doesn't work.
That's not the only problem with your code, but I'm sure you can figure out the rest.