Can any one write this program without using "scanf"

sndgpr26

sndgpr26

@sndgpr26-Yb3sXd Oct 21, 2024
Write a program to take any number from the user and that can give the output of twice the number, without using "scanf"

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 30, 2012

    Is this your doubt? or you already know it?
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 30, 2012

    There are various alternatives. Command line arguments, fgets etc.
  • sndgpr26

    sndgpr26

    @sndgpr26-Yb3sXd Aug 30, 2012

    Vishal0203
    Is this your doubt? or you already know it?
    no i dont know it
    actually some one had asked me to do this
    but i was failed
  • sndgpr26

    sndgpr26

    @sndgpr26-Yb3sXd Aug 30, 2012

    [Prototype]
    There are various alternatives. Command line arguments, fgets etc.
    sir
    can u please write the whole program
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 30, 2012

    There are various alternatives. Command line arguments, fgets
    wrong info:
    you cannot do it by fgets()
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 30, 2012

    no i dont know it
    actually some one had asked me to do this
    but i was failed
    Note:: I'm not using scanf() but I'm using fscanf().. is that okay??

    #include "stdio.h"
    #include "conio.h"
    int main()
     
    {
       
    int n,a;
    fscanf(stdin,"%d",&n);
    a=2*n;
    fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 30, 2012

    i think its not correct ans. As scanf n fscanf() belong to same family
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 30, 2012

    sndgpr26
    sir
    can u please write the whole program
    You got the info, give it a try yourselves. We're here to help you learn & what you're asking is not the correct way.

    Vishal0203
    wrong info:
    you cannot do it by fgets()
    Well, you gonna do the same with command line arguments i.e. convert a string into a integer. Its surely a possible option.
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 30, 2012

    you gonna do the same with command line arguments
    Can you give a small example?
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    [Prototype]
    You got the info, give it a try yourselves. We're here to help you learn & what you're asking is not the correct way.



    Well, you gonna do the same with command line arguments i.e. convert a string into a integer. Its surely a possible option.
    what conversion you're talking about?? Will you lease elaborate and educate me about it??

    #include "stdio.h"
    #include "conio.h"
    int main()
    { 
    char n[1]; int a,b;
    fflush(stdin);
    fgets(n,sizeof(n),stdin);
    b = int(n);
    printf("%d",b);
    //a=2*n;
    //fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
    Is this you're talking about?? But doesn't give a desired output!! 😖
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 31, 2012

    Same can be done with commandline.

    #include<stdio.h>
    #include<stdlib.h>
     
    int main()
    {
    char n[20];
    int z;
     
    fgets(n, sizeof(n),stdin);
     
    z = atoi((char*)n);
     
    printf("String converted to integer is -> %d\n",z);
    printf("Adding 10 to the converted string -> %d\n",z+10);
     
    return 0;
    }
    
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    z = atoi((char*)n);
    Can you tell me what this part actually does?? ? that was cool!!
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 31, 2012

    Vishal0203
    Can you tell me what this part actually does?? ? that was cool!!
    That's actually the heart of the system atoi (ASCII to INTEGER) is the function which converts the string into integer. However, a major drawback attached to it is that it doesn't report error in case you pass something like "asdads" as input instead of numbers in form of string. You can use strtol() instead of atoi(), but I've just used it to keep the things simple.

    I've done a typecast to char* because atoi requires parameter as char* & not char.
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    doesn't report error in case you pass something like "asdads" as input instead of numbers in form of string.
    I think we can make a check for number or string using conditional statements before passing it to atoi(), isn't it??
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Aug 31, 2012

    Vishal0203
    I think we can make a check for number or string using conditional statements before passing it to atoi(), isn't it??
    How so? Everything that will come as an input will be a string i.e. despite you're sending input as 123456, its will be treated as a string. To make the string distinguishable as an integer, we're using that function.
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    okay!! I surfed the net for strtol() 😀
    really cool!
    thanks to you!! 😀
  • sulochana anand

    sulochana anand

    @sulochana-anand-OrGmKr Aug 31, 2012

    Vishal0203
    Note:: I'm not using scanf() but I'm using fscanf().. is that okay??

    #include "stdio.h"
    #include "conio.h"
    int main()
     
    {
     
    int n,a;
    fscanf(stdin,"%d",&n);
    a=2*n;
    fprintf(stdout,"%d",a);
    getch();
    return 0;
    }
    
    y r u asking?if u used this code then definitely u must run it.wat was the result?
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Aug 31, 2012

    sulochana anand
    y r u asking?if u used this code then definitely u must run it.wat was the result?
    result was fine..
    I'm getting the desired result.
    but I'm using fscanf() which belongs to family of scanf(). That's the reason I asked
  • rahul69

    rahul69

    @rahul69-97fAOs Aug 31, 2012

    Well here is my attempt :
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int no,i;
    char s[40];
    gets(s);
    no=atoi(&s);
    printf("%d",no);
    getch();
    return 0;
    }
    simply gets() can do...😎
  • sulochana anand

    sulochana anand

    @sulochana-anand-OrGmKr Aug 31, 2012

    Vishal0203
    result was fine..
    I'm getting the desired result.
    but I'm using fscanf() which belongs to family of scanf(). That's the reason I asked
    thats right.but u are using fscanf "stdin" but scanf need not to use stdin.The fscanf() function read from the named input stream. The scanf() function reads from the standard input stream .
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Sep 1, 2012

    gets()
    gets() is not a good suggestion as it overloads the buffer. Hence,
    avoid using gets as much as possible.

    for more info,
    #-Link-Snipped-#
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Sep 1, 2012

    sulochana anand
    thats right.but u are using fscanf "stdin" but scanf need not to use stdin.The fscanf() function read from the named input stream. The scanf() function reads from the standard input stream .
    you're right but, the ques is about the function and as far as i know, scanf() fscanf() both belong to same family.
    anyways i got to learn a new thing 😀 atoi() i didn't know about it!
  • Priya Nadkarni

    Priya Nadkarni

    @priya-e8RF2B Sep 1, 2012

    How about cin(console input)? Its simple.

    Program-
    #include<iostream.h>
    int main()
    {
    int num;
    cin>>num;
    cout<<num*2;
    return 0;
    }
  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Sep 1, 2012

    PriyaJ
    How about cin(console input)? Its simple.

    Program-
    #include<iostream.h>
    int main()
    {
    int num;
    cin>>num;
    cout<<num*2;
    return 0;
    }
    Those are C++ functions. This discussion is about C.
  • Priya Nadkarni

    Priya Nadkarni

    @priya-e8RF2B Sep 1, 2012

    Oh k.