Tutorial_10

 ICT/2022/139

R.S.R.Ranathunga


Print the following shapes using loop construct of C programming.

* * 

* * * 

* * * * 

* * * * * 

* * * * * *

#include <stdio.h>

int main(){

int i,x;

for(i=1;i<=6;i++){

for(x=1;x<=i;x++){

printf("*");

}

printf("\n");

}

return 0;

}


* * * * * * * 

* * * * * * * 

* * * * * * * 

* * * * * * * 

* * * * * * *

#include <stdio.h>

int main(){

int i,x;

for(i=1;i<=5;i++){

for(x=1;x<=7;x++){

printf("*");

}

printf("\n");

}

return 0;

}


* * * * * * 

* * * * * 

* * * * 

* * * 

* * 

*

#include <stdio.h>

int main(){

int i,x;

for(i=6;i>=1;i--){

for(x=1;x<=i;x++){

printf("*");

}

printf("\n");

}

return 0;

}

           * 

         * * 

       * * * 

     * * * * 

   * * * * * 

 * * * * * * 

#include <stdio.h>


int main() {

    int i, j, k;


    for (i = 6; i >= 1; i--) {

        

        for (j = 1; j <= i; j++) {

            printf(" ");

        }


        

        for (k = 0; k <=6 - i; k++) {

            printf("*");

        }


        printf("\n");

    }


    return 0;

}


* * * * * * 

  * * * * * 

    * * * * 

      * * * 

        * * 

          * 

#include <stdio.h>

int main() {

    int i, j;


    for (i = 6; i >= 1; i--) {

        

        for (j = 1; j <= 6 - i; j++) {

            printf("  ");

        }


        

        for (j = 1; j <= i; j++) {

            printf("* ");

        }


        printf("\n");

    }


    return 0;

}


1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5 


#include <stdio.h>

int main(){

int i,x;

for(i=1;i<=5;i++){

for(x=1;x<=i;x++){

printf("%i", x);

}

printf("\n");

}

return 0;

}


3. Find the minimum and maximum of sequence of 10 numbers. 

#include<stdio.h>

int main(){


int max, min, num1, num2;

int i;

printf("Enter first number from the sequence: ");

scanf("%i", &num1);

      

min=num1;

max=num1;

for(i=1; i<=9; i++){

printf("Enter next number from the sequence: ");

scanf("%i", &num2);

if(min > num2){

min=num2;

continue;}

else if(max < num2){

max=num2;

continue;}

}

printf("Minimum number is %i\n", min);

printf("Maximum number is %i", max);

return 0;

}


4. Find the total and average of a sequence of 10 numbers.

#include<stdio.h>

int main(){


int total=0, num1;

float avg=0;

int i;

for(i=1; i<=10; i++){

printf("Enter the number from the sequence: ");

scanf("%i", &num1);

total+=num1;

}

avg=(float)total/10;

printf("Total of the sequence= %i\n", total);

printf("Average of the sequence= %.2f", avg);

return 0;

}

5. Write a program to generate and display a table of n and n

2

, for integer values of n 

ranging from 1 to 10. Be certain to print appropriate column headings. 


#include <stdio.h>

int main(){

int n,c=0, x=10;

printf(" ________________________________\n");

printf(" |\tn\t|\tn^2\t|\n");

printf(" |______________|_______________|\n");

for(n=1;n<=x;n++){

c=n*n;

printf(" |\t%i\t|\t%i\t|\n", n,c);

}

printf(" |______________|_______________|\n");

return 0;

}


6. A triangular number can also be generated by the formula 

triangularNumber = n (n + 1) / 2 

for any integer value of n. For example, the 10th triangular number, 55, can be generated 

by substituting 10 as the value for n in the preceding formula. Write a program that 

generates a table of triangular numbers using the preceding formula. 


#include <stdio.h>

int main(){

int n,Tn=0, i;

printf("Enter number count: ");

scanf("%i", &i);

printf("\n\tDisplay Tringular number list\n");

printf(" ________________________________________________\n");

printf(" |\tNumber\t|\tTriangular Number\t|\n");

printf(" |______________|_______________________________|\n");

for(n=1;n<=i;n++){

Tn=n*(n+1)/2;

printf(" |\t%i\t|\t\t%i\t\t|\n", n,Tn);

}

printf(" |______________|_______________________________|\n");

return 0;

}


7. The factorial of an integer n, written n!, is the product of the consecutive integers 1 

through n. For example, 5 factorial is calculated as 

5! = 5 x 4 x 3 x 2 x 1 = 120 

#include <stdio.h>

int main(){

int i, n, fact=1;

printf("Enter number:");

scanf("%i", &i);

for(n=i; n>=1; n--){

fact=fact*n;

}

printf("Factorial of number %i is %i", i,fact);   

return 0;

}


8. Write a program to generate and print a table of the first 10 factorials. 

#include <stdio.h>

int main(){

int i,a=10, n, fact=1;

printf("\n\tDisplay first 10 factorial number list\n");

printf("  _______________________________________________\n");

printf("  |\tNumber\t|\tFactorial Number\t|\n");

printf("  |_____________|_______________________________|\n");

 

for(i=1; i<=a; i++){

for(n=i; n>=1; n--){

fact=fact*n;

}

printf("  |\t%i\t|\t\t%i\t\t|\n", i, fact);

printf("  |_____________|_______________________________|\n");

fact=1;

}

return 0;

}


9. Display the n terms of harmonic series and their sum. 

1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms 

Test Data : 

Input the number of terms : 5 

Expected Output : 

1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 

Sum of Series upto 5 terms : 2.283334


#include<stdio.h>

int main(){


int n,i=5;

float a=1, sum=0;

printf("Expected Output:\n");

for(n=1; n<=i; n++){

printf(" 1/%i +", n);

sum=sum+(a/n);

}

printf("\nSum of Series upto 5 terms : %.6f", sum);

return 0;

}


10. Write a program to generate students repot as shown below. 


Index Number Name Mathes Physics Chemistry Total Average Grade

________________________________________________________________

 

In this program, for each student you have to input marks of three subjects. Students’ 

grade is determined according to following criteria: 

a. Average >= 90% : Grade A 

b. Average >= 80% : Grade B 

c. Average >= 70% : Grade C 

d. Average >= 60% : Grade D 

e. Average >= 40% : Grade E 

f. Average < 40% : Grade F 

#include <stdio.h>

#include <string.h>

int main(){

int index, marks1, marks2, marks3;

int total = 0;

float avg = 0;

char name_1[]="Kumar";

char name_2[]="Mahela";

char name_3[]="Angelo";

char name_4[]="Dilshan";

char name_5[]="Invalid";

char temp_name[50];

int index_1=11, index_2=27, index_3=69, index_4=23;

printf("Enter Index number: ");

scanf("%i", &index);

if(index==11){

strcpy(temp_name, name_1);

}

else if(index==27){

strcpy(temp_name, name_2);

}

else if(index==69){

strcpy(temp_name, name_3);

}

else if(index==23){

strcpy(temp_name, name_4);

}

else{

strcpy(temp_name, name_5);

}

printf("Enter Maths subject marks: ");

scanf("%i", &marks1);

printf("Enter Physics subject marks: ");

scanf("%i", &marks2);

printf("Enter Chemistry subject marks: ");

scanf("%i", &marks3);

printf("\n");

printf("  Index Number \tName\t Mathes Physics Chemistry  Total  Average Grade \n");

printf("  ______________________________________________________________________________\n");

total = marks1 + marks2 + marks3 ;

avg = total/3;

avg = (avg/100)*100;

if(avg>=90)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    A\n", index, temp_name, marks1, marks2, marks3, total, avg);

else if(avg>=80)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    B\n", index, temp_name, marks1, marks2, marks3, total, avg);

else if(avg>=70)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    C\n", index, temp_name, marks1, marks2, marks3, total, avg);

else if(avg>=60)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    D\n", index, temp_name, marks1, marks2, marks3, total, avg);

else if(avg>=40)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    E\n", index, temp_name, marks1, marks2, marks3, total, avg);

else if(avg<40)

printf("\n      %i        %s    %i      %i       %i       %i    %.2f    F\n", index, temp_name, marks1, marks2, marks3, total, avg);


return 0;

}

11. Assume a village has 20 houses. Input electricity unit charges and calculate total 

electricity bill according to the following criteria: 

 For first 50 units Rs. 0.50/unit 

 For next 100 units Rs. 0.75/unit 

 For next 100 units Rs. 1.20/unit 

 For unit above 250 Rs. 1.50/unit 

An additional surcharge of 20% is added to the bill 

The output should be as follows: 

Serial Number: House Address Units Surcharge Amount to be paid 


#include <stdio.h>


int main() {


    int i, count;

    float bill = 0, surcharge = 0, total_bill = 0;

    char name_1[20];

    int unit_charges[20];

    int serial_count[20];


    for (i = 0; i < 20; i++) {

        printf("Enter serial number: ");

        scanf("%i", &serial_count[i]);

        printf("Enter unit charges %i: ", i + 1);

        scanf("%i", &unit_charges[i]);

printf("Enter house address:: ");

        scanf("%s", &name_1[i]);

    }

printf("\n");

    printf("\tSerial Number:    House Address      Units   Surcharge   Amount to be paid\n ");

    printf("\t________________________________________________________________________\n");


    for (count = 0; count < 20; count++) {


        if (unit_charges[count] <= 50) {

            bill = unit_charges[count] * 0.50;


        } else if (unit_charges[count] <= 150) {

            bill = (unit_charges[count] - 50) * 0.75 + 25;


        } else if (unit_charges[count] <= 250) {

            bill = (unit_charges[count] - 150) * 1.20 + 100;


        } else if (unit_charges[count] > 250) {

            bill = (unit_charges[count] - 250) * 1.50 + 220;

        }


        surcharge = bill * 0.20;

        total_bill = bill + surcharge;


        printf("\n\t     %i            %s           %i      %.2f          %.2f \n", serial_count[count], name_1[count], unit_charges[count], surcharge, total_bill);

    }


    return 0;

}


Comments