Lab 3 Correction
Lab 3 Correction
Exercise 2:
1st way: While loop
#include <stdio.h>
main()
{
int j=7;
int i=1;
while(j>0)
{
while(i<=j)
{
printf("*");
i++;
}
i=1;
printf("\n");
j=j-1; }}
2nd way: For loop
/** FOR LOOP*/
#include <stdio.h>
main()
{
for(int j=7; j>=1; j--)
{
for(int i=1; i<=j;i++)
{
printf("*");
}
printf("\n");
}
}
Exercise 3:
#include<stdio.h>
main()
{
int j,k;
for(int i=1;i<=7;i++)
{
for(j=1; j<=i;j++)
{
printf("%d",j);
}
for(k=j;k<=7;k++)
{
printf("*");
}
printf("\n");
}
}
Exercise 4:
/* Write a program that Prints n Random Numbers where n is strictly positive.*/
1st way: for loop
#include <stdio.h>
#include <stdlib.h>
main()
{
int i, n;
printf("Some randomly distributed integers will be printed. \n How many do you want to see? \n
");scanf("%d", &n);
for (i = 0; i < n;i++) {
if (i % 6 == 0)
printf("\n");
printf("%9d", rand());
}
printf("\n");
}
2nd way: while loop
#include<stdio.h>
#include<stdlib.h>
main()
{
int i, n;
i=0;
printf("Some randomly distributed integers will be printed. \n How many do you want to see? \n ");
scanf("%d", &n);
while(i < n) {
if (i % 6 == 0)
printf("\n");
printf("%9d", rand());
i++; }
}
Exercise 5:
#include<stdio.h>
main()
{
int i,c,d,u;
for (i=100;i<=999;i++)
{
c=i/100;
d=i%100/10;
u=i%10;
if (u*100+d*10+c==i) printf("%d\n",i);
} }
Exercise 6:
#include<stdio.h>
main()
{
int i,nb_male=0, nb_female=0, younger18=0, older25=0, som_age_female=0, percent_male,
percent_female,percent_older_male25, average_age_female,age;
char gender;
for(i=1; i<=10;i++)
{
do
{
printf("put the gender of the %d nd student\n", i);
scanf("%c",&gender);
} while ((gender!='M')&&(gender!='F'));
do
{
printf("put the age of the %d nd student\n", i);
scanf("%d",&age);
}while (age < 1);
if (gender=='M'){
nb_male++;
if(age > 25)
{
older25++;
}
}
else {
nb_female++;
som_age_female = som_age_female + age;
}
if (type == domestic)
{
totaldomestic =totaldomestic + duration;
if (duration<=3)
{
bill= bill + (duration*0.2);
}
else
{
duration_upto3domestic = duration - 3;
bill= bill +(3*0.2)+ (duration_upto3domestic*0.3);
}
}
else // type of call is intercity
{
totalintercity =totalintercity + duration;
if (duration<=2)
{
bill= bill + (duration*0.5);
}
else
{
duration_upto3intercity = duration -2;
bill= bill + (2*0.5)+ (duration_upto3intercity*0.7);
}
}
}
printf("The results are:\n");
printf("----------------------------------------------------------\n");
printf("The Total duration of domestic calls is : %d \n", totaldomestic);
printf("The Total duration of intercity calls is : %d \n", totalintercity);
printf("The Total bill of customer is : %0.2f \n", bill);