7 CSE 115L More Loops and Their Applications
7 CSE 115L More Loops and Their Applications
Example 3: Write a program that takes N and M as input and prints your name in
a rectangle with N rows and M column of your name
#include <stdio.h>
int main()
{
int M, N, i, j;
printf("enter M\n");
scanf("%d", &M);
printf("enter N\n");
scanf("%d", &N);
for(i=1; i<=M;i++){
for(j=1; j<=N; j++){
printf("Saif\t");
}
printf("\n");
}
return 0;
}
Example 4: Write a program that uses for loop to display a Fibonacci series,
till the nth term, where n is a number given by the user
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
}
return 0;
Task 3: Write a C program to enter a number from user and find first and last digit of number using loop.
How to find first and last digit of a number in C.
Homework
1. Write a program that can calculate the LCM of 2 numbers
2. Write a Program that can calculate the HCF of two numbers
3. Write a Program to generate the Floyds triangle. Number of rows of
Floyd's triangle to print is entered by the user. First four rows of
Floyd's triangle are as follows :1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.