0% found this document useful (0 votes)
66 views3 pages

7 CSE 115L More Loops and Their Applications

The document provides examples of using nested loops and conditional statements in C programming. It includes examples to print alphabets using nested loops, find prime numbers from 1 to 100, print a name in a rectangle given row and column inputs, generate the Fibonacci series up to a given number, and find the first and last digit of a number. It also lists tasks for students to write programs to calculate the sum of an expression using a given input number, print a name in a rectangle, find the first and last digit of a number, calculate LCM and HCF of two numbers, and generate Floyd's triangle for a given number of rows. The document is teaching C programming concepts like loops, conditions, user input, printing patterns

Uploaded by

Saif Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views3 pages

7 CSE 115L More Loops and Their Applications

The document provides examples of using nested loops and conditional statements in C programming. It includes examples to print alphabets using nested loops, find prime numbers from 1 to 100, print a name in a rectangle given row and column inputs, generate the Fibonacci series up to a given number, and find the first and last digit of a number. It also lists tasks for students to write programs to calculate the sum of an expression using a given input number, print a name in a rectangle, find the first and last digit of a number, calculate LCM and HCF of two numbers, and generate Floyd's triangle for a given number of rows. The document is teaching C programming concepts like loops, conditions, user input, printing patterns

Uploaded by

Saif Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 115L

Faculty: Dr. Ahsanur Rahman


Instructor: Saif Ahmed
Multiple nested loops
Example 1:
#include <stdio.h>
int main(void)
{
int i, j, k;
for(i = 0; i < 3; i++){
for(j = 0; j < 26; j++){
for( k = 0; k < 2; k++){
printf("%c", 'A' + j);
}
}
printf("\n");
}
return 0;
}
Task 1: Use nested Loops to print 3 lines of lowercase and upper case
alphabets side by side. E.g: AaBbCcDd..
Example 2: Using nested loops and if else condition to find the prime numbers
from 1 to 100
#include <stdio.h>
int main () {
int i, j;
for(i = 2; i<100; i++) {
for(j = 2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) printf("%d is prime\n", i);
}
return 0;
}

a is given as input. You have to find out the sum of the


following expressiona2 - (a-1)2 + (a-2)2 - (a-3)2 + (a-4)2 - ............. + 1
Task 2: In the following problem, an integer

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.

Example: If number = 1234


First digit = 1
Last digit = 4

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.

You might also like