Session 8_C Program
Session 8_C Program
Flowchart:
START
sum=0, odd_sum=0,
even_sum=0
false
i=1
false
Is i≤n ?
true
sum=sum+i;
true false
is n%2≠0 ?
odd_sum=odd_sum+i even_sum=even_sum+i
i++
print the values of sum,
odd_sum and even_sum
END
Code:
#include <stdio.h>
int main()
{int n, sum = 0, odd_sum = 0, even_sum = 0;
input: printf("Enter a positive integer: ");
scanf("%d", &n);
if (n<=0)
{printf("Please enter a positive integer greater than 0.\n");
goto input;
}
return 0;
}
(ii) To generate and print the first ‘N’ Fibonacci numbers such that Fn = F(n-1) + F(n-2) where n>2.
A Fibonacci sequence is defined as “the first and second terms in the sequence are 0 and 1.
Subsequent terms are found by adding the preceding two terms in the sequence”.
Algorithm:
STEP-1: START
STEP-2: Ini alize f1=0, f2=1
STEP-3: Read the number n
STEP-4: If the number n is non-posi ve number, print error message and go to STEP-3 again. Otherwise,
move on to STEP-5.
STEP-5: Run the loop, FOR (int i=1; i≤n; i++)
{IF (i=1) fn=f1;
ELSE IF (i=2) fn=f2;
ELSE
{fn=f1+f2;
f1=f2;
f2=fn;}
}
print the value fn.
STEP-6: END
Flowchart:
START
f1=0, f2-1
true
is n≤0?
false
i=1
false
is i≤n ?
true
true false
is i=1?
true false
fn=f1 is i=2?
fn=f1+f2;
fn=f2
f1=f2;
f2=fn;
END
Code:
#include <stdio.h>
int main()
{int n, f1 = 0, f2 = 1, fn;
input: printf("Enter the number of Fibonacci terms to generate: ");
scanf("%d", &n);
if (n <= 0)
{printf("Please enter a positive integer greater than 0.\n");
goto input;
}
printf("\n");
return 0;
}
(iii) To find the sum of individual digits of a posi ve integer number reducing into single digit.
Algorithm:
STEP-1: START
STEP-2: Ini alize sum=0
STEP-3: Read the number n
STEP-4: If the number n is non-posi ve number, print error message and go to STEP-3 again. Otherwise,
move on to STEP-5.
STEP-5: Run the loop, WHILE (n≠0)
{sum=sum+(n%10);
n=n/10;
}
STEP-6: print the value of sum
STEP-7: END
Flowchart:
START
sum=0
true
is n≤0?
false
false
is n≠0? print sum
true
sum=sum+(n%10);
n=n/10;
END
Code:
#include <stdio.h>
int main()
{int n, sum = 0, digit;
input: printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 0)
{printf("Please enter an integer greater than 0.\n");
goto input;
}
(iv) To reverse a given four-digit integer number and check whether it is a palindrome or not.
Output the given number with suitable message.
Algorithm:
STEP-1: START
STEP-2: Ini alize reversed=0;
STEP-3: Read the number n from user
STEP-4: If the number n is either less than 1000 or greater than 9999, print error message and go to STEP-3
again. Otherwise, move on to STEP-5.
STEP-5: Store the value of n in the variable original
STEP-6: Run the loop WHILE(n≠0)
{digit=n%10;
reversed=reversed×10 + digit;
n=n/10;
}
STEP-7: IF the original=reversed,
print that the given number is a palindrome.
ELSE
print that the given number is not a palindrome along with its reversed number.
STEP-8: END
Flowchart:
START
reversed=0
is n<1000 true
or n>9999?
false
original=n;
false
is n≠0?
true
digit=n%10;
reversed=reversed×10+digit;
n=n/10;
false is
original=reversed?
END
Code:
#include <stdio.h>
int main()
{int n, reversed = 0, original, digit;
input: printf("Enter a four-digit integer number: ");
scanf("%d", &n);
if (n < 1000 || n > 9999)
{printf("Please enter a four-digit integer number.\n");
goto input;
}
return 0;
}