Saturday, 20 May 2017

DUMMY PROGRAMMER QUESTIONS

Questions made by DUMMY PROGRAMMER


Question-Convert a given Decimal number to binary
ANS-
#include<iostream>
#include<string.h>
using namespace std;
void take(long long int  n)
{
    int c=0,i=0,rem=0;
    int m[50];
    do{
        rem=n%2;
        n=n/2;
       m[i]=rem;
        i=i+1;
    }while(n!=0);

    for(int j=i-1;j>=0;j--)
    {
        cout<<m[j];
    }
}
int main()
{
    long long int n;
    string s;
    cin>>n;
    take(n);
    return(0);

}


_________________________________________________________________



Ques-A positive interger N is passes as the input. The program must print the number of 1s in the binary representation of N.
Input format-
input the number N.
Output format-
First line contains the count of 1s in the binary representation of N.
Boundary condition-
1<=N<=9000000000.

Example-
input-22
output-3
Explaination-the binary representation of 22 is 10110. There are three 1s in it.

____________________________________________________________


Solution-

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    unsigned long long int n;
    int i=0;
    cin>>n;
    while(n!=0)
    {
        if(n%2==1)
            i++;
        n=n/2;
    }
    cout<<i;
}



____________________________________________________________

Ques-Take input a string(with spaces) and for each word make sure that the word starts with uppercase and gives alternate lowercase

Example1-

input-Every BoY HaS hiS OWn LoVESTOry
expected output-EvErY BoY HaS HiS OwN LoVeStOrY

Example2-

input-this sEEMS CoOL
expected output-ThIs SeEmS CoOl

______________________________________________________________

Solution-


#include<iostream>

#include<string>
using namespace std;

void convert( string& s)

{
    for(int i=0;i<s.length();i++)
    {
        s[i]=tolower(s[i]);
    }
}

void convertevenodd( string &s)

{
    for(int i=0;i<s.length();i=i+2)
    {
        if(s[i]==' ')
        {
            i=i-1;
        }
        else
        {
        s[i]=toupper(s[i]);
        }
    }
}
int main()
{
    string s;
    getline(cin,s);
    convert(s);
    convertevenodd(s);
    cout<<s;
    return(0);
}


No comments:

Post a Comment

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...