Professional Documents
Culture Documents
Programming Fundamentals
Programming Fundamentals
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;
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;
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(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;
}
}
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){
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);
else{
printf("\nEnter a password for your account (total 6 chars, should contain
1 numeric, 1 upper and 1 lowercase char):");
scanf("%s", pwd);
else{
for(int i = 0; i <= strlen(pwd); i++){
if(pwd[i] >= '0' && pwd[i] <= '9'){
numeric++;
}
upper++;
}
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);
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);
}
(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;
printf("\n");
}
printf("\n");
}
return 0;
}