[go: up one dir, main page]

0% found this document useful (0 votes)
3 views7 pages

Session 8_C Program

The document outlines algorithms and corresponding C code for four tasks: summing natural, odd, and even numbers up to a given positive integer; generating the first N Fibonacci numbers; calculating the sum of individual digits of a positive integer; and reversing a four-digit integer while checking if it is a palindrome. Each task includes a step-by-step algorithm, flowchart, and code implementation. Error handling is incorporated to ensure valid input from the user.

Uploaded by

vigmahi07
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)
3 views7 pages

Session 8_C Program

The document outlines algorithms and corresponding C code for four tasks: summing natural, odd, and even numbers up to a given positive integer; generating the first N Fibonacci numbers; calculating the sum of individual digits of a positive integer; and reversing a four-digit integer while checking if it is a palindrome. Each task includes a step-by-step algorithm, flowchart, and code implementation. Error handling is incorporated to ensure valid input from the user.

Uploaded by

vigmahi07
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/ 7

Session 8

(i) To sum Natural, Odd and Even numbers up to ‘N’.


Algorithm:
STEP-1: START
STEP-2: Ini alize sum=0, odd_sum=0, even_sum=0
STEP-3: Read the number n from user
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++)
{sum=sum+i;
if i is even, even_sum=even_sum+i.
else odd_sum=odd_sum+i
}
STEP-6: Print the values of sum, odd_sum, even_sum
STEP-7: END

Flowchart:

START

sum=0, odd_sum=0,
even_sum=0

Read the number n

print error message


true
Is n≤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;
}

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


{sum += i; // Sum of natural numbers
if (i % 2 == 0)
even_sum += i; // Sum of even numbers
else
odd_sum += i; // Sum of odd numbers
}

printf("Sum of natural numbers up to %d: %d\n", n, sum);


printf("Sum of odd numbers up to %d: %d\n", n, odd_sum);
printf("Sum of even numbers up to %d: %d\n", n, even_sum);

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

Read the number n print error message

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;

i++ print the value of 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("Fibonacci sequence up to %d terms:\n", n);

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


{
if (i == 1)
fn = f1; // First term
else if (i == 2)
fn = f2; // Second term
else
{
fn = f1 + f2; // Subsequent terms
f1 = f2; // Update f1 to the previous term
f2 = fn; // Update f2 to the current term
}

printf("%d ", fn); // Print the Fibonacci term


}

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

Read the number n print error message

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

// Calculate the sum of digits


while (n!=0)
{sum=sum+(n%10); // Add the last digit to sum
n/=10; // Remove the last digit from n
}
printf("Sum of all digits of the number: %d\n", sum);
return 0;
}

(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

Read the number n print error message

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?

print the given number


true
is not a palindrome with
reversed number print the given number
is a palindrome.

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

original = n; // Store the original number for comparison

// Reverse the number


while (n != 0)
{digit = n % 10; // Get the last digit
reversed = reversed * 10 + digit; // Build the reversed number
n /= 10; // Remove the last digit from n
}

// Check if the original number is a palindrome


if (original == reversed)
printf("The number %d is a palindrome.\n", original);
else
printf("The number %d is not a palindrome. Its reverse is %d.\n", original,
reversed);

return 0;
}

You might also like