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

C How To Program 16

Uploaded by

alina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

C How To Program 16

Uploaded by

alina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

C how to program

MENTOR ALEENA RUBAB


Review Exercise

Lets explore the problems 3.45…..


#include <stdio.h>

// Function to compute the factorial of a non-negative integer


unsigned long long factorial(int n) {
if (n == 0) {
return 1;
}
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
B
int main() {
int n;
printf("Enter a non-negative integer: ");
scanf("%d", &n);

if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("%d! = %llu\n", n, factorial(n));
}

return 0;
}
c.
#include <stdio.h>

// Function to compute the factorial of a non-negative integer


unsigned long long factorial(int n) {
if (n == 0) {
return 1;
}
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
int n;
double e = 1.0; // e starts from 1 as 0! = 1

printf("Enter the number of terms for estimating e: ");


scanf("%d", &n);

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


e += 1.0 / factorial(i);
}

printf("Estimated value of e using %d terms: %lf\n", n, e);

return 0;
}
#include <stdio.h>

// Function to compute the factorial of a non-negative integer


unsigned long long factorial(int n) {
if (n == 0) {
return 1;
}
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;}
// Function to compute e^x
double compute_ex(double x, int n) {
double ex = 1.0; // e^x starts from 1 as 0! = 1
double term = 1.0;

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


term *= x / i;
ex += term;
}

return ex;
}
int main() {
double x;
int n;

printf("Enter the value of x: ");


scanf("%lf", &x);

printf("Enter the number of terms for computing e^x: ");


scanf("%d", &n);

printf("Estimated value of e^%.2lf using %d terms: %lf\n", x, n,


compute_ex(x, n));

return 0;
}
#include <stdio.h>

int main() {
double current_population;
double growth_rate;
double population;
double increase;
double double_population;
double quadruple_population;

printf("Enter the current world population: ");


scanf("%lf", &current_population);

printf("Enter the annual growth rate (as a percentage): ");


scanf("%lf", &growth_rate);

growth_rate = growth_rate / 100.0; // Convert percentage to


decimal
double_population = current_population * 2;
quadruple_population = current_population * 4;

printf("%-10s %-25s %-25s\n", "Year", "Projected Population", "Annual


Increase");
printf("-----------------------------------------------------------\n");

for (int year = 1; year <= 100; ++year) {


increase = current_population * growth_rate;
population = current_population + increase;

printf("%-10d %-25.2lf %-25.2lf\n", year, population, increase);

if (current_population < double_population && population >=


double_population) {
printf("Population will double in year: %d\n", year);
}
if (current_population < quadruple_population && population >=
quadruple_population) {
printf("Population will quadruple in year: %d\n", year);
}

current_population = population; // Update the current population for


the next year
}

return 0;
}
Enter the current world population: 7800000000
Enter the annual growth rate (as a percentage): 1.1
Year Projected Population Annual Increase
-----------------------------------------------------------
1 7885800000.00 85800000.00
2 7972543800.00 86743800.00
3 8060231981.80 87688181.80
...
#include <stdio.h>

void encrypt(int *digits) {


// Replace each digit
for (int i = 0; i < 4; ++i) {
digits[i] = (digits[i] + 7) % 10;
}

// Swap the first digit with the third, and the


second digit with the fourth
int temp = digits[0];
digits[0] = digits[2];
digits[2] = temp;

temp = digits[1];
digits[1] = digits[3];
digits[3] = temp;
}
void decrypt(int *digits) {
// Swap the first digit with the third, and the
second digit with the fourth
int temp = digits[0];
digits[0] = digits[2];
digits[2] = temp;

temp = digits[1];
digits[1] = digits[3];
digits[3] = temp;

// Replace each digit


for (int i = 0; i < 4; ++i) {
digits[i] = (digits[i] + 3) % 10; // equivalent to
subtracting 7 and adding 10 if result is negative
}
}
void readDigits(int *digits) {
int number;
printf("Enter a four-digit integer: ");
scanf("%d", &number);

// Split the number into digits


digits[0] = (number / 1000) % 10;
digits[1] = (number / 100) % 10;
digits[2] = (number / 10) % 10;
digits[3] = number % 10;
}
void printDigits(const int *digits) {
for (int i = 0; i < 4; ++i) {
printf("%d", digits[i]);
}
printf("\n");
}

int main() {
int digits[4];

// Encrypt
readDigits(digits);
encrypt(digits);
printf("Encrypted number: ");
printDigits(digits);

// Decrypt
decrypt(digits);
printf("Decrypted number: ");
printDigits(digits);

return 0;
}
The end

You might also like