ForLoop.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
C# For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
SLIDE-2
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
SLIDE-2(DOWNWARDS)
Example(https://dotnetfiddle.net/9ZGqdB):
using System;
public class Program
{
public static void Main()
{
for(int i=1;i<=10;i++){
Console.WriteLine(i);
}
}
}
SLIDE-3
Exercise:
Q1 to Q10 of While Loop section.
Q11. Write a C# program to print all odd number between 1 to 100.
Q12. Write a C# program Fibonacci Numbers to print numbers upto N, where N is a number input by the user.
Test Data :
Enter a number : 8
Expected Output :
First 8 Fibonacci Numbers are : 0 1 1 2 3 5 8 13 21
Hint: The formula to form the Fibonacci sequence is:
F0 = 1
F1 = 1
Fn = Fn-1 + Fn-2
SLIDE-4
Q13. Write a C# program to check if a number input by the user is prime or not
Test Data :
Enter a number : 7
Expected Output : 7 is prime
Q14. Write a C# program to print the cube of a number entered by the user(Without using Math methods)
Test Data :
Enter a number : 2
Expected Output : cube of 2 is 8
Q15. Two numbers are entered by the user. Write a program to find the value of one number raised to the power of another.
Test Data :
Enter first number : 4
Enter first number : 3
Expected Output : 4 raise to 3 is 64
SLIDE-5
Q16. Write a C# program to swap first and last digits of a number entered by the user.
Test Data :
Enter a number : 1432
Expected Output :
Number after swapping first and last digit is 2431
Q17. Write a C# program to calculate sum of digits of a number.
Test Data :
Enter a number : 1432
Expected Output :
Sum of digits of the number is 10
Q18. Write a C# program to check whether a number is palindrome or not.
Test Data :
Enter a number : 13231
Expected Output :
13231 is palindrome
Hint: A palindrome is a word, phrase, number or sequence of words that reads the same backward as forward.
SLIDE-6
Q19. Write a C# program to print all prime numbers between 1 to N. Where N is a number input by the user.
Test Data :
Enter a number : 20
Expected Output :
The prime number from 1 to 20 are 2 3 5 7 11 13 17 19
Q20. Write a C# program to find all prime factors of a number.
Test Data :
Enter a number : 10
Expected Output :
The prime factors of 10 are 2, 5
Q21. Write a C# program to check if the number entered by the user is an Armstrong number.
Hint: If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
Test Data :
Enter a number : 370
Expected Output :
370 is an Armstrong Number.
Q22. Write a C# program to print out all Armstrong numbers between 1 and 500.
SLIDE-7
Q23. Write a C# program to input number from user and check whether number is Strong number or not.
Hint: Strong number is a special number whose sum of factorial of digits is equal to the original number.
For example: 145 is strong number. Since, 1! + 4! + 5! = 145
Test Data :
Enter a number : 145
Expected Output :
145 is an Strong Number.
Q24: You are given a cubic dice with 6 faces. All the individual faces have a number printed on them. The numbers are in the range of 1 to 6, like any ordinary dice. The user needs to enter number for one face and then guess the number on the opposite face of the cube. Check if the user makes the correct guess or not
Hint: Sum of opposite sides of a dice is 7
Test Data :
Eg1:
Enter a number of a face(1=6): 6
Guess the opposite face : 1
Expected Output :
Your guess is correct.
Eg2:
Enter a number of a face(1=6): 5
Guess the opposite face : 3
Expected Output :
Your guess is not correct.
SLIDE-8
Q25: Given a number N, find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself. Eg:"89 because 8^1+9^2=89
Test Data :
Enter a number: 89
Expected Output :
89 is Disarium
Q26: Given an integer N which has odd number of digits, find whether the given number is a balanced or not.
An odd digit number is called a balanced number if the sum of all digits to the left of the middle digit and the sum of all digits to the right of the middle digit is equal.
Test Data :
Enter a number: 1234006
Expected Output :
1234006 is balanced
Q27: Given a number N, write a program to find the largest and smallest digit in the number
Test Data :
Enter a number: 1234006
Expected Output :
Largest digit is: 6
Smallest digit is: 0
Q28: Given two integers L, R, and digit X.Find the number of occurrences of X in all the numbers in the range (L, R) Excluding L and R.
Test Data:
Enter the value of L=18
Enter the value of R=81
Enter the value of X=9
Expected Output :
There are 7 occurrences of the digit 9
in the numbers in the range (18,81).