3. Iteration Statements

Que.1 Find the number of digits

              
  const Find_Digits = (N) => 
  {
  	 // Convert the number to a string
      const numStr = N.toString();
  
      // Initialize the count to 0
      let count = 0;
  
      // Iterate through each character in the string
      for (const ch of numStr) {
          // If the character is a digit, increment the count
          if (/[0-9]/.test(ch)) {
              count++;
          }
      }
  
      // Return the count
      return count;
  };           
        

Que.2 Find the Fives.

            
  const Find_Five = (N) => 
  {
  	 // Convert the number to a string
      const numStr = N.toString();
  
      // Initialize the count to 0
      let count = 0;
  
      // Iterate through each character in the string
      for (const ch of numStr) {
          // If the character is a digit, increment the count
          if (ch == '5') {
              count++;
          }
      }
  
      // Return the count
      return count;
  };
            
        

Que.3 Find Sum

  const findSum = (n) => {
     let sum = 0;
      for(let i=1; i<=n; i++){
        if(i%2===0){
          sum = sum+i;
        }
      }
      return sum;
  };

        

Que.4 Find the sum of the digits of a given number.

  const Number_Sum = (N) => 
  {
  	// Convert the number to a string
      const numStr = N.toString();
  
      
      let sum = 0;
      
      for (const ch of numStr) {
         sum += Number(ch);
      }
  
      return sum;
  };


        

Que.5 Print the Odds.

  const Print_Odd = (N) => 
  {
  	console.log("2"); // print 2 first
  
    for (let i = 3; i <= N; i += 2) { // start from 3 and increment by 2 to get only odd numbers
      console.log(i);
    }
  };
        

Que.6 Print the Pattern.

            
  const Print_pattern = (N) => 
  {
    for(let i=0; i < N; i++){
      let row = "";
      for(let j=0; j<=i; j++){
        row += "*";
      }
      console.log(row);
    }
  };
        

Que.7 Check whether a Number is a prime or not.

  const Prime_Check = (N) => 
  {
  	 if (N < 2) {
      return "NO";
    }
    for (let i = 2; i <= Math.sqrt(N); i++) {
      if (N % i === 0) {
        return "NO";
      }
    }
    return "YES";
  	
  };
        

Que.8 Print Numbers

            
  const printNumbers = (n) => {
      for(let i=1; i<=n; i++){
        console.log(i);
      }
  };

        

Que.9 Print the Table

         
  const Print_Table = (N) => 
  {
  	for (let i = 1; i <= 10; i++) {
      console.log(N + " * " + i + " = " + N*i);
    }
  };
        
Back
Next