Anda di halaman 1dari 4

1.

#include <stdio.h>
main()
{
int x, n;
for(x = 1; x <= 5; x++ )
{
n = 1;
while( n <= x )
{
printf("%d", x);
n++;
}
printf("\n");
}
}

2. for
#include <stdio.h>
main()
{
int x, y, z;
z = 1;
printf("masukkan angka ");scanf("%d", &y);
{
for (x=1;x<=y;x++)
z = z * x;
}
printf("\nnilai faktorial dari %d adalah %d\n\n" ,y , z);
}

while
#include<stdio.h>
main()
{
int x, y, z;
z = 1;
printf("Masukkan angka ");scanf("%d", &y);
x = 1;

while (x <= y)
{
z = z * x;
x++;
}
printf("\nnilai faktorial dari %d adalah %d\n\n" ,y , z);
}

Do-while
#include <stdio.h>
main()
{
int x, y, z;
z = 1;
printf("Masukkan angka ");scanf("%d", &y);
x = 1;
do
{
z = z * x;
x++;
}
while (x <= y);
printf("\nnilai faktorial dari %d adalah %d\n\n" ,y , z);
}

1.
#include<stdio.h>
main()
{
int data, z;
float rata, nilai, total;
total = 0;
printf("Jumlah mahasiswa adalah : ");scanf("%d", &data);
printf("\n");
for(z=1; z<=data; z++)
{
printf("Nilai mahasiswa ke-%d : ", z); scanf("%f",
&nilai);
total = total + nilai;
}
rata = total/data;
printf("\n\nTotal nilai : %.2f \nNilai rata-rata : %.2f",
total, rata);
printf("\n\n");
}

2.
#include<stdio.h>
main ()
{
int x, bilangan;
printf("nilai ke-n = "); scanf("%d", &bilangan);
printf("\n");
for (x= 1; x <= bilangan; x++)
{
if(x % 2 == 0)
{
printf ("%d ", -1*x);}
else
{
printf ("%d ", x);}
}
printf("\n\n");
}

3.
#include<stdio.h>
main()
{
int nilai, n, x, y;
n = 0;
y = 2;
nilai = 4;
do
{
printf("%d ",nilai);
nilai = nilai + y;
n++;
y++;
} while(n<7);
printf("\n\n");
}

4.
#include<stdio.h>
main()
{
int data, n, x, y, z;
n = 0;
x = 0;
y = 1;
printf("Banyaknya angka : "); scanf("%d", &data);
while(x<data)
{
y++;
for(z=2; z<y; z++)
{
if(y%z == 0)
n = 1;
}
if(n !=1)
{
printf("%d ",y);
x++;
}
n=0;
}
printf("\n ");
}