PERULANGAN (LOOPING)

LATIHAN PERTEMUAN 9
  1. Membuat tampilan seperti program dibawah dengan
    menggunakan metode perulangan:
    1. While
    2. Do..While

Capture5

Program While
#include<iostream>
#include<conio.h>

using namespace std;
int main()
{
    int a=1, b;

    while(a<=9)
    {
    b=a; while (b<=9)
    {
        cout<<b<<""; b++;
    }
    cout<<"\n"; a++;
    }
return 0;
}
 HASIL INPUT
 
HASIL OUTPUT

 
Program Do While
#include<iostream>
#include<conio.h>

using namespace std;
int main()
{
    int a=1, b;

    do
    {
        b=a;
        do
        {
          cout<<b<<""; b++;
        }
        while (b<=9);
        cout<<"\n"; a++;
    }
    while(a<=9);
return 0;
}
HASIL INPUT
HASIL OUTPUT

3. Buatlah program untuk menghitung 10 deret bilangan ganjil dan hasilnya :
    1 + 3 + 5 + 7 + 9 + 11 + 13 + 17 + 19 = 100 
#include<iostream>
#include<conio.h>

using namespace std;
int main()
{
int a=1,b=19,n;
for(a=a;a<=b;a+=2)
{
cout<<a;
if(a<b)
{
cout<<"+";
}
}

cout<<"=";
n=(b+1)/2;
n=n*n;
cout<<n;
return 0;
}
HASIL INPUT
HASIL OUTPUT


4. Buatlah program untuk menghitung penjumlahan deret bilangan membentuk segitiga siku dengan hasilnya :
    1                               = 1
    1 + 2                         = 3
    1 + 2 + 3                   = 6
    1 + 2 + 3 + 4             = 10
    1 + 2 + 3 + 4 + 5       = 15 
#include<iostream>
#include<stdio.h>
#include<conio.h>

using namespace std;
int main()
{
int a =5,b,i,jmlh;
    for (b=1;b<=a;b+=1){
        jmlh=0;
            for (i=1;i<=b;i+=1)
            {
            if (i !=1)
            cout <<" + ";
            cout <<i;
            jmlh+=i;
            }
        cout <<" = "<<jmlh<<endl;
    }
    cout<<endl <<endl;
return 0;
}
HASIL INPUT

HASIL OUTPUT





SEKIAN TERRIMAKSIH :)




 

 

Comments

Popular posts from this blog

Program untuk menghitung nilai akhir siswa dari kursus yang diikutinya (Latihan 2)