Question

Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( ch


can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format

dont worry about the answers i have down

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, it is not clear what are you trying to ask. Your code is given in C language and not in C++. Also you have asked to solve it in C++ but at the same time you are specifying that general format for that is printf, scanf which is not true, it is for C language and not for C++. Do you want the above code to be converted to C++ from C or do you want its output after compiling along with explanation.

Anyways, am doing all of these, but sorry if you meant something else. Pls don't rate bad for this.

Code and its output in C -

#include<stdio.h>
#include<string.h>

int main(void)
{
char str[]= "39704";
int N= strlen(str); /* here N = 5, since length of str[] is 5 */
int num;

/* trasversing along full string str[] */

for(int i=0; i<N; i++)
{
for(int j=0; j<i; j++) /* this for loop creates 0 space in first row, 1 space in next row, and so on */
printf(" ");  
num= (str[i]- '0'); /* num gets integer value of i th str[] element (ASCII value concept is prerequisite for this), for eg, '3' - '0' gives 3, bcos, if we directly see str[0], we will get str[0] as 51(which is ASCII code of 3), and '0' is 48, so we do '3' - '0' and we get 3 (actually 51-48 =3 ) similarly, '9' - '0' gives 9 (actually 57-48 = 9 )*/
if(num > 0)
{
for(int j=0; j< N-1-i; j++) /* this loop multiplies 10 to num till N-1 times */
num *=10;
printf("%d ", num); /* prints value of num till now */
}
else /* enters here if num is less than OR equals to zero*/
{
for(int j=0; j< N-i; j++)
printf("-");
printf(" ");
}
}
}

Code output-


I am also writing code in C++ -

#include <iostream>
#include<cstring>
using namespace std;

int main(void)
{
char str[]= "39704";
int N = strlen(str);   /* here N = 5, since length of str[] is 5 */
int num;

  /* trasversing along full string str[] */

for(int i=0; i<N; i++)
{
for(int j=0; j<i; j++) /* this for loop creates 0 space in first row, 1 space in next row, and so on */
cout<<" ";
num= (str[i]- '0'); /* num gets integer value of i th str[] element (ASCII value concept is prerequisite for this), for eg, '3' - '0' gives 3, similarly, '9' - '0' gives 9 */
if(num > 0)
{
for(int j=0; j< N-1-i; j++) /* this loop multiplies 10 to num till N-1 times */
num *=10;
cout<<num<<endl; /* prints value of num till now */
}
else    /* enters here if num is less than OR equals to zero*/
{
for(int j=0; j< N-i; j++)
cout<<"-";
cout<<endl;
}
}
}

Add a comment
Know the answer?
Add Answer to:
can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void...

    #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void evaluate(int x) { if(x > 10) printf("Value entered by you is greater than 10"); else if(x < 10) printf("Value entered by you is less than 10"); else printf("Value entered by you is equal 10"); } _______________________________________ report for this in hr ?

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT