TUTORIAL 10

ICT/2022/101 - K.N.MALWEWA - KM2194
By -
0

 

2. Print the following shapes using the loop construct of C programming.



#include<stdio.h>

int main()
{
int A;
int B;
for(A=1;A<=6;A++){
for(B=1;B<=A;B++){
printf("*");
}
printf("\n");
}
return 0;
}
 

 





          



#include<stdio.h>

int main()
{
int A;
int B;
for(A=1;A<=6;A++){
for(B=1;B<=6;B++){
printf("*");
}
printf("\n");
}
return 0;
}


    


#include<stdio.h>

int main()
{
int A;
int B;
int C;
for(A=6;A>=1;A--){
for(B=1;B<=A;B++){
printf("*");
}
printf("\n");
}
return 0;
}
 

    

#include<stdio.h>

int main()
{
int A;
int B;
int C;
for(A=1;A<=6;A++){
for(B=6;B>=A;B--){
printf(" ");
}
for(C=1;C<=A;C++)
printf("*");
printf("\n");
}
return 0;
}




 
#include<stdio.h>

int main()
{
int A;
int B;
int C;
for(A=1;A<=6;A++){
for(B=2;B<=A;B++){
printf(" ");
}
for(C=6;C>=A;C--)
{
printf("*");
}
printf("\n");
}
return 0;
}



    


#include<stdio.h>

int main()
{
int A;
int B;
for(A=1;A<=5;A++){
for(B=1;B<=A;B++)
{
printf("%i",B);
}
printf("\n");
}
return 0;
}


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


#include<stdio.h>

int main()
{
int x,max=0,min=0,num;

printf("Enter 10 numbers:\n");
scanf("%i",&num);
min=num;
max=num;
for(x=1;x<=9;x++){
scanf("%i",&num);
if(num>max){
max=num;
}
if(num<min) 
min=num;
}
printf("Maximum value is:%i\n",max);
printf("Maximum value is:%i",min);
return 0;
}

        

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


#include <stdio.h>

int main ()
{
int x, number;
float average;
int total = 0;
printf("Enter 10 numbers\n");
for (x=1; x<=10; x++)
{  
scanf("%i", &number);
total = total+number;
}
average = total /10.0;
printf("Total : %i\n",total);
printf("Average : %.2f",average);
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 x,c;
printf(" n   n^2\n\n");
for(x=1;x<=10;x++)
{
printf("%2i %4i\n",x,x*x);
}
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,x;
int triangularNumber=0;
printf("Enter number:");
scanf("%i",&n);
printf("number\ttriangular number\n");
printf("------\t------------------\n");
for(x=1;x<=n;x++)
{
triangularNumber=x*(x+1)/2;
printf("%3i %5i\n",x,triangularNumber);
}
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 x;
unsigned long long total = 1;

printf("Enter a non-negative number:");
scanf("%i",&x);
if(x<0)
{
printf("Factorial is not defined for negative numbers.\n");
}
else
{
printf("%i! = ",x);
for(x;x>=2;x--)
{
total*=x;
printf("%i*",x);
}
printf("1 = %llu\n",total);
}
return 0;
}


    



 

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


#include<stdio.h>

int main()
{
int total=1,x;
printf("First 10 factorials\n\n");
for(x=1;x<=10;x++)
{
total=total*x;
printf("%2i%8i\n",x,total);
}
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 x,n;
float Sum=0;
printf("Enter the number of terms:");
scanf("%i",&n);

for(x=1;x<=n;x++)
{
Sum += 1.0/x;
printf("1/%i + ",x);

}
printf("\nSum of series up to 5 terms : %.6f",Sum);

return 0;
}


        


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


#include<stdio.h>

int main()
{
int x,n;
int Index[100];
int m[100];
int p[100];
int c[100];
int total[100];
float Average[100];
char Name[100][100];
char Grade[100];

printf("How many students do you entered:");
scanf("%i",&n);
for(x=0;x<n;x++)
{
printf("\nStudent 0%i\n",x+1);
printf("-----------\n");
printf("Enter Index number:");
scanf("%i",&Index[x]);
printf("Enter Name:");
scanf("%s",&Name[x]);
printf("Enter Maths marks:");
scanf("%i",&m[x]);
printf("Enter Physics marks:");
scanf("%i",&p[x]);
printf("Enter Chemistry marks:");
scanf("%i",&c[x]);
total[x]=m[x]+p[x]+c[x];
Average[x] = total[x]/3.0;
if(Average[x]>=90)
Grade[x] = 'A';
else if(Average[x]>=80)
Grade[x] = 'B';
else if(Average[x]>=70)
Grade[x] = 'C';
else if(Average[x]>=60)
Grade[x] = 'D';
else if(Average[x]>=40)
Grade[x] = 'E';
else 
Grade[x] = 'F';    
}
printf("\nIndex Number Name Maths Physics Chemistry Total                                    Average Grade\n");
printf("----------------------------------------------------------------------------------------\n");
for(x=0;x<n;x++){
printf("%4i\t",Index[x]);
printf("\t%s\t",Name[x]);
printf("%3i\t",m[x]);
printf("%3i\t",p[x]);
printf("%3i\t\t",c[x]);
printf("%3i\t  ",total[x]);
printf("%3.2f\t ",Average[x]);
printf("%c\n",Grade[x]);
}
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 



#include<stdio.h>

int main()
{
int x;
int serial[20];
char house[20][100];
int units[20];
float bill;
float surcharge[20];
float amount[20];
for(x=0;x<20;x++)
{
printf("House %2i\n",x+1);
printf("-----------\n");
printf("Enter Serial Number:");
scanf("%i",&serial[x]);
printf("House Address:");
scanf("%s",&house[x]);
printf("Units:");
scanf("%i",&units[x]);
bill=0;
if(units[x]<=50)
bill=units[x]*0.50;
else if(units[x]<=150)
bill=50*0.50+(units[x]-100)*0.75;
else if(units[x]<=250)
bill=50*0.50+100*0.75+(units[x]-150)*1.20;
else
bill=50*0.50+100*0.75+100*1.20+(units[x]-250)*1.50;
surcharge[x]=bill*20/100;
amount[x]= bill+surcharge[x];
}

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


for(x=0;x<20;x++)
{
printf("%5i\t\t",serial[x]);
printf("%-12s\t",house[x]);
printf("%5i\t",units[x]);
printf("%10.2f\t",surcharge[x]);
printf("%13.2f\t\n",amount[x]);
}
return 0;
}








Post a Comment

0Comments

Post a Comment (0)