You are on page 1of 11

Name: Muhammad Sabih Salam

Roll #: CTAI-040

LAB 06

Write a program which will find the factorial of a given number. Exit the program if the
input number is negative. Test case: Input number = 5, Factorial is=5*4*3*2*1

#include <stdio.h>

int main(void){
int n;
double factorial = 1;

printf("\nEnter any number for it's factorial: ");


scanf("%d", &n);

if(n < 0){


printf("\nInvalid input.");
return 1;
}
else if(n == 0){
printf("\nThe factorial of %d is %.0lf", n, factorial);
}
else{
for(int i = n; i >= 1; i--){
factorial *= i;
}
printf("\nThe factorial of %d is %.0lf", n, factorial);
}

Write a program which will generate the Fibonacci series up to 1000. Also find the sum of the
generated Fibonacci numbers divisible by 3, 5 and 7 only.
#include <stdio.h>
int main(void){
int sum_3 = 0, sum_5 = 0, sum_7 = 0;
int first = 0, second = 1, next = 0;

//Using a while loop


printf("\nFibonacci Sequence using a while loop.\n");
printf("%d", second);

while(1){
next = first + second;

if(next%3==0){
sum_3 += next;
}

if(next%5==0){
sum_5 += next;
}

if(next%7==0){
sum_7 += next;
}

if(next >= 1000){


break;
}
else{
printf(" %d", next);
first = second;
second = next;
}
}

//Using a for loop


first = 0, second = 1, next = 0, sum_3 = 0, sum_5 = 0, sum_7 = 0;

printf("\n\nFibonacci Sequence using a for loop.\n");


printf("%d", second);

for(int i = next; i <= 1000;){


i = first + second;

if(i%3==0){
sum_3 += i;
}

if(i%5==0){
sum_5 += i;
}

if(i%7==0){
sum_7 += i;
}
if(i >= 1000){
break;
}

else{
printf(" %d", i);
first = second;
second = i;
}
}

printf("\n\nSum of fib numbers divisible by 3 = %d\nSum of fib numbers divisible


by 5 = %d\nSum of fib numbers divisible by 7 = %d\n", sum_3, sum_5, sum_7);

While loops are typically used when the number of repeats for a given problem is
unknown, whereas for-loops are used when the number of repetitions is known.
Therefore, using a for-loop instead of a while loop would produce more efficient results
when addressing this problem. Since we know how many times our loop will repeat in
total for this problem, the loop should end as soon as the Fibonacci sequence's next
number hits 1000.
Write a program which will input a 5-digit number. If the sum of digits is even, find whether the
input number is a prime or not. If the sum of digits is odd find, whether the number is
palindrome or not?
#include <stdio.h>
int main(void){

int num,tth, th, h, t, u, new_num;

printf("\nEnter any 5 digit number: ");


scanf("%d", &num);

if(num == 0 || num < 0 || !(num >= 10000 && num <= 99999)){
printf("\nInvalid input entered.");
return 1;
}
else{
tth = num/10000;
th = (num - tth*10000)/1000;
h = (num - tth*10000 - th*1000)/100;
t = (num - tth*10000 - th*1000 - h*100)/10;
u = (num - tth*10000 - th*1000 - h*100 - t*10);
if((tth + th + h + t + u) % 2 == 0){
int factors = 2;
for(int i = 2; i < num; i++){
if(num % i == 0){
factors++;
break;
}
}
if(factors == 2){
printf("\n%d is a prime number.", num);
}
else{
printf("\n%d is not a prime number.", num);
}
}
else if((tth + th + h + t + u) % 2 != 0){
new_num = u * 10000 + t * 1000 + h * 100 + th * 10 + tth;
if(new_num == num){
printf("\nThe number %d is a palindrome.", num);
}
else{
printf("\nThe number %d is not a palindrome.", num);
}
}
else{
printf("\nInvalid input (s).");
return 1;
}

}
return 0;
}
Develop a user-registration system have the following options. a) Ask the user for a user-
name (5 alphabets). b) Password should be 6 characters long with at least 1 numeric, 1
capital and 1 small letter. c) Display a “Account Created Successfully”. d) Login the user
with correct username and password. e) Display “Welcome username, you are now
logged in”.
Note: Develop your application using break and continue statement.

#include<stdio.h>

int main(void){
char username[10];
char pwd[10];
char login_username[10];
char login_pwd[10];
int numeric = 0, upper = 0, lower = 0;
printf("\nEnter your username (only 5 characters):");
scanf("%s", username);

if(strlen(username) > 5){


printf("\nInvalid. Username limit exceeded.");
return 1;
}

else if(strlen(username) < 5){


printf("\nInvalid. Username characters are less than 5.");
return 1;
}

else{
printf("\nEnter a password for your account (total 6 chars, should contain
1 numeric, 1 upper and 1 lowercase char):");
scanf("%s", pwd);

if(strlen(pwd) > 6){


printf("\nInvalid. Password character limit exceeded.");
return 1;
}

else if(strlen(pwd) < 6){


printf("\nInvalid. Password characters are less than 6.");
return 1;
}

else{
for(int i = 0; i <= strlen(pwd); i++){
if(pwd[i] >= '0' && pwd[i] <= '9'){
numeric++;
}

if(pwd[i] >= 65 && pwd[i] <= 90){

upper++;
}

if(pwd[i] >= 97 && pwd[i] <= 122){


lower++;
}
}

if(numeric >= 1 && upper >= 1 && lower >= 1){


printf("\nAccount created successfully.");
printf("\nPlease login using your credidentials.");

while(1){
int invalid_username = 0, invalid_pwd = 0;
printf("\n\nEnter your username:");
scanf("%s", login_username);
printf("\nEnter your password:");
scanf("%s", login_pwd);

for(int i = 0; (login_username[i] != '\0' ||


username[i] != '\0'); i++){
if(login_username[i] != username[i]){
invalid_username++;
break;
}
else{
continue;
}
}
for(int i = 0; (login_pwd[i] != '\0' || pwd[i] != '\0');
i++){
if(login_pwd[i] != pwd[i]){
invalid_pwd++;
break;
}
else{
continue;
}
}

if(invalid_username == 0 && invalid_pwd == 0){


printf("\nWelcome %s, you are now logged
in.", username);
break;
}
else{
printf("\nYou are entering something
wrong...");
}
}
}

else{
printf("\nPassword criteria not completely met.");
return 1;
}
}
}
return 0;
}

Outputs:
(Calculating the Value of π) Calculate the value of π from the infinite series. Print a table that
shows the value of π approximated by one term of this series, by two terms, by three terms,
and so on. How many terms of this series do you have to use before you first get 3.14?
3.141? 3.1415? 3.14159?

#include <stdio.h>

int main(void){
int term_count;
double pi = 0.0;

printf("Number of terms\tApproximation\n");

for(term_count = 1; ; term_count++){
if(term_count % 2 == 1){
pi += 4.0 / (2 * term_count - 1);
}
else{
pi -= 4.0 / (2 * term_count - 1);
}

printf("%d\t\t%.10f\n", term_count, pi);

if (pi >= 3.14 && pi <= 3.14159){


break;
}
}

printf("\nNumber of terms needed: %d\n", term_count);

(Diamond-Printing Program) Write a program that prints the following diamond shape. You may
use printf statements that print either a single asterisk (*) or a single blank. Maximize your use
of repetition (with nested for statements) and minimize the number of printf statements
#include <stdio.h>

int main(void){

int rows = 5;

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

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


printf(" ");
}

for(int k = 1; k <= (2 * i - 1); k++){


printf("*");
}

printf("\n");
}

for(int i = rows; i >= 1; i--){

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


printf(" ");
}

for(int k = 1; k <= (2 * i - 1); k++){


printf("*");
}

printf("\n");
}

return 0;
}

You might also like