Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

TUGAS 1

STRUKTUR DATA

DOSEN PEMBIMBING :
Drs. Denny Kurniadi, M.Kom

Oleh :
APRILLIA STEVANI
22076029
Pendidikan Teknik Informatika

DEPARTEMEN TEKNIK ELEKTRONIKA


FAKULTAS TEKNIK
PRODI PENDIDIKAN TEKNIK INFORMATIKA

UNIVERSITAS NEGERI PADANG


2023
Chapter 1

Program 1
Program untuk mengkalkulasikan area dari sebuah lingkaran.
a. Source Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
float radius;
double area;
system("cls");
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the radius of the circle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
printf(" \n Area = %.2lf", area);
return 0;
}

b. Screenshoot Program & Output


Program 2
Program untuk mengubah sebuah bilangan bulat integer menjadi float.
a. Source Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
float f_num;
int i_num;
system("cls");
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter any integer : ");
scanf("%d", &i_num);
f_num = (float)i_num;
printf("\n The floating point variant of %d is = %f", i_num, f_num);
return 0;
}

b. Screenshoot Output Program


Program 3
Program untuk mengetahui apakah suatu bilangan genap atau ganjil.
a. Source Code

#include <stdio.h>
int main()
{
int a;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
printf("\n %d is even", a);
else
printf("\n %d is odd", a);
return 0;
}

b. Screenshoot Output Program


Program 4
Program untuk menentukan apakah karakter yang dimasukkan adalah vokal atau bukan.

a. Source Code

#include <stdio.h>
int main()
{
char ch;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter any character : ");
scanf("%c", &ch);
switch(ch)
{
case 'A':
case 'a':
printf("\n %c is VOWEL", ch);
break;
case 'E':
case 'e':
printf("\n %c is VOWEL", ch);
break;
case 'I':
case 'i':
printf("\n %c is VOWEL", ch);
break;
case 'O':
case 'o':
printf("\n %c is VOWEL", ch);
break;
case 'U':
case 'u':
printf("\n %c is VOWEL", ch);
break;
default: printf("\n %c is not a vowel", ch);
}
return 0;
}

b. Screenshoot Output Program

Program 5
Program untuk menghitung jumlah angka dari m ke n.
a. Source Code

#include <stdio.h>
int main()
{
int n, m, i, sum =0;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the value of m : ");
scanf("%d", &m);
i=m;
printf("\n Enter the value of n : ");
scanf("%d", &n);
while(i<=n)
{
sum = sum + i;
i = i + 1;
}
printf("\n The sum of numbers from %d to %d = %d", m, n, sum);
return 0;
}

b. Screenshoot Output Program

Program 6
Program untuk menghitung rata-rata n angka pertama.
a. Source Code

#include <stdio.h>
int main()
{
int n, i = 0, sum =0;
float avg = 0.0;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the value of n : ");
scanf("%d", &n);
do
{
sum = sum + i;
i = i + 1;
} while(i<=n);
avg = (float)sum/n;
printf("\n The sum of first %d numbers = %d",n, sum);
printf("\n The average of first %d numbers = %.2f", n, avg);
return 0;
}

b. Screenshoot Output Program


Program 7
Program untuk menentukan apakah suatu bilangan tertentu merupakan bilangan prima atau
bilangan komposit.
a. Source Code

#include <stdio.h>
#include <stdlib.h>
int main()
{
int flag = 0, i, num;
system("cls");
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter any number : ");
scanf("%d", &num);
for(i=2; i<num/2;i++)
{
if(num%i == 0)
{
flag =1;
break;
}
}
if(flag == 1)
printf("\n %d is a composite number", num);
else
printf("\n %d is a prime number", num);
return 0;
}
b. Screenshoot Output Program

Program 8
Program untuk mengetahui apakah suatu bilangan genap atau ganjil menggunakan fungsi.
a. Source Code

#include <stdio.h>
int evenodd(int); //FUNCTION DECLARATION
int main()
{
int num, flag;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the number : ");
scanf("%d", &num);
flag = evenodd(num); //FUNCTION CALL
if (flag == 1)
printf("\n %d is EVEN", num);
else
printf("\n %d is ODD", num);
return 0;
}
int evenodd(int a) // FUNCTION HEADER
{
// FUNCTION BODY
if(a%2 == 0)
return 1;
else
return 0;
}

b. Scrennshoot Output Program

Program 9
Program untuk menambahkan dua bilangan bulat menggunakan pointer dan fungsi.
a. Source Code

#include <stdio.h>
void sum (int*, int*, int*);
int main()
{
int num1, num2, total;
printf("===================================\n");
printf("= Programmer : APRILLIA STEVANI \t =\n");
printf("= NIM : 22076029\t =\n");
printf("===================================\n");
printf("\n Enter the first number : ");
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
sum(&num1, &num2, &total);
printf("\n Total = %d", total);
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}

b. Screenshoot Output Program


Chapter 2
1. Write an algorithm for swapping two values.
Step 1: Input first number as A
Step 2: Input second number as B
Step 3: SET TEMP = A
Step 4: SET A = B
Step 5: SET B = TEMP
Step 6: PRINT A, B
Step 7: END

2. Write an algorithm to find the larger of two numbers.


Step 1: Input first number as A
Step 2: Input second number as B
Step 3: IF A>B
PRINT A
ELSE
IF A<B
PRINT B
ELSE
PRINT "The numbers are equal"
[END OF IF]
[END OF IF]
Step 4: END

3. Write an algorithm to find whether a number is even or odd.


Step 1: Input number as A
Step 2: IF A%2 =0
PRINT "EVEN"
ELSE
PRINT "ODD"
[END OF IF]
Step 3: END

4. Write an algorithm to print the grade obtained by a student using the following rules.
Step 1: Enter the Marks obtained as M
Step 2: IF M>75
PRINT O
Step 3: IF M>=60 AND M<75
PRINT A
Step 4: IF M>=50 AND M<60
PRINT B
Step 5: IF M>=40 AND M<50
PRINT C
ELSE
PRINT D
[END OF IF]
Step 6: END

5. Write an algorithm to find the sum of first N natural numbers.


Step 1: Input N
Step 2: SET I = 1, SUM = 0
Step 3: Repeat Step 4 while I <= N
Step 4: SET SUM = SUM + I
SET I = I + 1
[END OF LOOP]
Step 5: PRINT SUM
Step 6: END

You might also like