Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Print Name

Easy
0/40
Average time to solve is 5m
profile
Contributed by
26 upvotes
Asked in company
Capegemini Consulting India Private Limited

Problem statement

You are given a string ‘S’ of lowercase English alphabets, you are required to print ‘S’ 5 times, each on a new line using a while loop.

EXAMPLE:
Input: 'S' = ‘alicia’

Output: alicia
        alicia
        alicia
        alicia
        alicia
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain the integer 'T', the number of test cases.

For each test case, there will be only one line containing a string ‘S’.
Output format :
For each test case, print the ‘name’ five times, each on a new line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
1 <= 'S.Length' <= 10^4

Time Limit: 1 sec
Sample Input 1 :
2
ramu
shamu
Sample Output 1 :
ramu
ramu
ramu
ramu
ramu
shamu
shamu
shamu
shamu
shamu
Sample Input 2 :
2
ankush
kannu
Sample Output 2 :
ankush
ankush
ankush
ankush
ankush
kannu
kannu
kannu
kannu
kannu
Approaches (1)
Naive Force

Approach: 
 

Create a variable ‘COUNT’ to store the coun’ of iteration and end the loop when the ‘COUNT’ reaches 5.
 

Algorithm :  
 

  • printName(string ‘S’)
    • Create and initialize the ‘COUNT’ variable as ‘0’
    • Do a while loop until ‘COUNT’ < 5
      • Print ‘S’ and simultaneously end the line
Time Complexity

O(1).

As we are only doing the operation 5 times, the time complexity will be O(1).

Space Complexity

O(1).


As we are using the extra ‘count’ variable, the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Print Name
All tags
Sort by
Search icon

Interview problems

Java Solution || Print Name

import java.util.*;
import java.io.*;


public class Solution {
    static void printName(String s) {
        // Write your code here.
        int i = 5;
        while (i >= 1) {
            System.out.println(s);
            i--;
        }
        
    }
}
35 views
0 replies
0 upvotes

Interview problems

C++ (17ms), beats 100%

#include <bits/stdc++.h> 

void printName(string s){

    // Write your code here.

    int count = 5;

    while(count){

        cout<<s<<endl;

        count--;

    }

}

 

61 views
0 replies
0 upvotes

Interview problems

C++ Solution || All test case passed || Time Complexity: O(1)

#include <bits/stdc++.h> 
void printName(string s){
    
    int i = 0;

    while (i < 5) {
        cout << s << endl;
        i++;
    }

    return;
}
78 views
0 replies
1 upvote

Interview problems

100% performance

#include <bits/stdc++.h> 

void printName(string s){

    // Write your code here.

    int count=0;

    while(count<5){

        cout<<s<<endl;

        count++;

    }

}

 

90 views
0 replies
0 upvotes

Interview problems

solution in python

def printName(s):

    for i in range(5):

        print(s)

python

103 views
0 replies
0 upvotes

Interview problems

21 Day Coding Challenge, DAY 14 🔥|| Easy JAVA Solution 🔥

import java.util.* ;

import java.io.*; 

 

public class Solution {

    static void printName(String s){

           int i=1;

        while(i<=5){

            System.out.println(s);

            i++;

        }

    }

}

PLEASE UPVOTE ME

java

135 views
0 replies
1 upvote

Interview problems

java

import java.util.* ;

 

import java.io.*; 

 

 

 

public class Solution {

 

    static void printName(String s){

 

        // Write your code here.

 

        int i=1;

 

        while(i<=5){

 

            System.out.println(s);

 

            i++;

 

        }

 

    }

 

}

49 views
0 replies
1 upvote

Interview problems

easy c++ solution

https://youtu.be/ulFQWG-Xcn0

 

void printName(string s){
    // Write your code here.
    int c=0;
    while(c<5)
    {
        cout<<s<<"\n";
        c++;
    }
}
93 views
0 replies
0 upvotes

Interview problems

Python Solution

def printName(s):
    c = 0
    while c<5 :
        c+=1
        print(s)
116 views
0 replies
1 upvote

Python Solution

def printName(s):

    # Write your code here

    c = 0

    while c<5 :

        c+=1

        print(s)

82 views
0 replies
0 upvotes
Full screen
Console