7 CSE 115L More Loops and Their Applications (1)

download 7 CSE 115L More Loops and Their Applications (1)

of 3

Transcript of 7 CSE 115L More Loops and Their Applications (1)

  • 8/18/2019 7 CSE 115L More Loops and Their Applications (1)

    1/3

    CSE 115LFaculty: Dr. Ahsanur RahmanInstructor: Saif Ahmed

    Multiple nested loops

    Example 1:#include int main(void){ int i, j, k; for(i = 0; i < 3; i ){ for(j = 0; j < !"; j ){ for( k = 0; k < !; k ){ rintf($%c$, &'& j);

    rintf($ n$); return 0;

    Task 1: Use nested Loops to print 3 lines of lowercase and upper casealphabets side by side. E.g: AaBb c!d"""""""..

    E#a$ple %: Using nested loops and if else condition to find the pri$e nu$bersfro$ 1 to 1&&

    #include stdio.h!int main " $ int i% &' for "i ( ) ' i 1** ' i++ $ for " &( ) ' & ( "i, & ' &++

    if "-"i & /rea0 ' ,, if factor found% not rime

    if " &! "i, & rintf "2 d is rime3n2 % i ' 4

    return * '

    4

    Task %: In the following problem, an integer a is given as input. You have to find out the sum of thefollowing expression-a ! * (a*+) ! (a*!) ! * (a*3) ! (a* ) ! * ............. +

  • 8/18/2019 7 CSE 115L More Loops and Their Applications (1)

    2/3

    E#a$ple 3: 'rite a progra$ that takes ( and ) as input and prints your na$e ina rectangle with ( rows and ) colu$n of your na$e

    *include +stdio.h, int $ain-/int )0 (0 i0 2

    printf- enter )4n 2scanf- 5d 0 6) 2

    printf- enter (4n 2scanf- 5d 0 6( 2for-i712 i+7)2i88 / for- 712 +7(2 88 / printf- 9aif4t 2 printf- 4n 2

    return &2

    E#a$ple ;: 'rite a progra$ that uses for loop to display a

  • 8/18/2019 7 CSE 115L More Loops and Their Applications (1)

    3/3

    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 = 1 3!

    "irst digit = 1

    #ast digit = !

    >o$ework

    1. 'rite a progra$ that can calculate the L ) of % nu$bers%. 'rite a ?rogra$ that can calculate the > < of two nu$bers3. 'rite a ?rogra$ to generate the