0% found this document useful (0 votes)
8 views5 pages

Lab 3 Correction

The document contains multiple C programming exercises demonstrating different looping techniques, random number generation, and data processing. Exercises include printing patterns with loops, generating random integers, checking for palindromic numbers, and calculating statistics based on student data. Additionally, it covers calculating call durations and billing based on type of calls made.

Uploaded by

raslen gharssa
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)
8 views5 pages

Lab 3 Correction

The document contains multiple C programming exercises demonstrating different looping techniques, random number generation, and data processing. Exercises include printing patterns with loops, generating random integers, checking for palindromic numbers, and calculating statistics based on student data. Additionally, it covers calculating call durations and billing based on type of calls made.

Uploaded by

raslen gharssa
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/ 5

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 (age > 18)


{
younger18++;
}
}
percent_male= nb_male*10;
percent_female = nb_female*10;
//younger18 is calculed in for loop
percent_older_male25= older25*10;
average_age_female = som_age_female / nb_female;

printf("The results are:\n");


printf("----------------------------------------------------------\n");
printf("The percentage of male is: %d \n", percent_male);
printf("The percentage of female is: %d \n", percent_female);
printf("The number of students younger than 18 is: %d \n", younger18);
printf("The percentage of male older than 25: %d \n", percent_older_male25);
printf("The average age of female students is: %d \n", average_age_female); }
Exercise 7:
#include <stdio.h>
#include <stdlib.h>
#define domestic 0
#define intercity 1
main()
{
int calls=0;
int i=0;
float bill=0;
int type=0;
int totaldomestic =0;
int totalintercity=0;
int duration= 0;
int duration_upto3domestic=0;
int duration_upto3intercity=0;

printf("Enter number of calls :");


scanf("%d", &calls);

for(i=0; i< calls;i++)


{

printf("Enter the type of call number %d: ",i+1);


scanf("%d",&type);
printf("\n");
while ((type!=domestic)&&(type !=intercity))
{
printf("Enter the type of call number %d: ",i);
scanf("%d \n",&type);
printf("\n");
}
do
{
printf("Enter the duration of call number %d: ",i+1);
scanf("%d",&duration);
printf("\n");
}while (duration < 0);

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);

You might also like