DAY 1- DATA TYPE ON HACKER RANK

TASK--> Three variable i, d and s are already declared and initialized for you, you must.

1. Declare 3 variable : One of type int, one of type double, one of type string.

2. Read 3 line of input from stdin and intilized your 3 variable.

3. Use + operator to perform following operation.

    a.)  Print sum of i plus your int variable on a new line.

    b.) Print sum of d Plus your double variable on a new line.

    c.) Concatenate S with the String you need as input & print the result on a new line.



/*PROGRAM START FROM HERE -->*/

    #define MAX_BUFFER 255
    // Declare second integer, double, and String variables.
    int si;
    double dd;
    char *buff = malloc(MAX_BUFFER);
    if (buff == NULL) {
        printf("Memory error\n");
        return 1;
    }
    // Read and save an integer, double, and String to your variables.
    if ( fgets(buff, MAX_BUFFER, stdin) != NULL ) {
        sscanf(buff, "%d", &si);
        printf("%d\n", i + si);
    }
    if ( fgets(buff, MAX_BUFFER, stdin) != NULL ) {
        sscanf(buff, "%lf", &dd);
        printf("%.1f\n", d + dd);
    }
    if ( fgets(buff, MAX_BUFFER, stdin) != NULL ) {
         printf("%s%s\n", s, buff);
    }



/* #include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

 int main(){

   int i=4;

   double d=4.0;

   char S[] = "HackerRank";    */

#define MAX_BUFFER 255

   // declare 2nd integer, double, & String variable

   int si;

   double dd;

   char *buff = malloc (MAX_BUFFER);

   if(buff = NULL){

     printf("Memory Error \n");

       return 1;

}

//read and save an integer, double & structure to your variable.


if(fgets(buff, MAX_BUFFER, stdin)!= NULL){

   sscanf(buff, "%d", &si);

   printf("%d \n", i+si);

}



if(fgets(buff, MAX_BUFFER, stdin)!= NULL){

   sscanf(buff, "%lf", &dd);

   printf("%d \n", d+dd);

}

if(fgets(buff, MAX_BUFFER, stdin)!= NULL){

   sscanf(buff, "%%S", &dd);

   printf("%s %s \n", s , buff);

} 

return 0;

    // Print the sum of both integer variables on a new line.
    // Print the sum of the double variables on a new line.
    
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.

/*


  int i1;

 double d1;

char str[200];

scanf("%d", &i1);

scanf("lf", &d1);

scanf("[^\n]%*c", str);

printf("%d \n", i+i1);

printf("%0.1lf \n", d +d1);

pritnf("%s", s);    //j error here

*/

Comments

Popular posts from this blog

Allocate Book Problem Solution || Code Studio

Peak Index in a Mountain Array || Binary Search practice example || Leet Code poblem #852

First and Last position of an element in sorted array | Binary Search Practice Example | Code Studio Interview Question