0% found this document useful (0 votes)
42 views4 pages

Main (X, N (X 1 X 5 X++) (N 1 (N X) (Printf (, X) N++ ) Printf ) )

The document contains 4 C code examples demonstrating the use of for, while, do-while, and nested loops. The first example uses a for loop to print increasing numbers in rows. The second uses a for loop to calculate the factorial of a user-input number. The third uses a while loop for the same factorial calculation. The fourth uses a do-while loop for the factorial calculation. The last example uses a nested while loop to print prime numbers up to a user-input value.

Uploaded by

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

Main (X, N (X 1 X 5 X++) (N 1 (N X) (Printf (, X) N++ ) Printf ) )

The document contains 4 C code examples demonstrating the use of for, while, do-while, and nested loops. The first example uses a for loop to print increasing numbers in rows. The second uses a for loop to calculate the factorial of a user-input number. The third uses a while loop for the same factorial calculation. The fourth uses a do-while loop for the factorial calculation. The last example uses a nested while loop to print prime numbers up to a user-input value.

Uploaded by

Taufiqur Rohman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 ");
}

You might also like