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

Sessional

The document contains various C programming exercises focusing on numerical methods, including counting integers divisible by 7, finding prime numbers, generating Fibonacci numbers, and calculating factorials. It provides code snippets for each task, demonstrating the use of loops and basic programming concepts. Additionally, it includes mathematical concepts like the Fibonacci sequence and the golden ratio.

Uploaded by

earnfree2946
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)
2 views24 pages

Sessional

The document contains various C programming exercises focusing on numerical methods, including counting integers divisible by 7, finding prime numbers, generating Fibonacci numbers, and calculating factorials. It provides code snippets for each task, demonstrating the use of loops and basic programming concepts. Additionally, it includes mathematical concepts like the Fibonacci sequence and the golden ratio.

Uploaded by

earnfree2946
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

Numerical Methods & Computer Programming

Sessional
Loop
Write a program to count the number and sum of all integers
greater than 50 and less than 250 that are divided by 7.
loop
#include <stdio.h>

int main()
{
int i, n=0, sum=0;
for (i=56; i<=250; i=i+7)
{
n++;
sum=sum+i;
}
printf("Number=%d\n", n);
printf("sum=%d", sum);
return 0;
}
Using do……while loop, print the numbers less than 100 which
are divided by 6.
loop
#include <stdio.h>

int main()
{
int i=6, n=100;
do
{
i%6==0;
printf("\nThe value=%d", i);
i=i+6;
}
while(i<n);
return 0;
}
Write a program to find out the prime number.
loop
#include <stdio.h>
int main()
{
int i, n;
printf("Enter the value of num:\n");
scanf("%d", &n);
for (i=2;i<=n;i++)
{
if(n%i==0)
break;
}
if(n==i)
printf("%d is Prime Number", n);
else
printf("%d is not Prime Number", n);
return 0;
}
Write a C program to print all the prime numbers between
1 and n, where n is the value supplied by the user
loop
#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter the value of num:\n");
scanf("%d", &n);
for (j=2; j<=n; j++)
{
for (i=2;i<=j;i++)
{
if(j%i==0)
break;
}
if(j==i)
printf("%d\t", i);
}
return 0;
}
Fibonacci Numbers and Golden Ratio
• In mathematics, the Fibonacci numbers, commonly denoted 𝐹𝑛
form a sequence, called the Fibonacci sequence, such that each
number is the sum of the two preceding ones, starting from 0 and
1.
1+ 5
• Golden ratio, ɸ = = 1.6180339887……
2
• Ratio of two successive Fibonacci numbers is also golden ratio.

F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765


Write a C program to print nth Fibonacci number
loop
#include<stdio.h>
int main()
{
int d1, d2, fibo, n, i;
printf("Enter n\n");
scanf("%d", &n);
d1=0;
d2=1;
fibo=d1+d2;
printf("Fibonacci Series=%d\t%d\t", d1, d2);
loop
for(i=3; i<=n; i++)
{
printf("%d\t", fibo);
d1=d2;
d2=fibo;
fibo=d1+d2;
}
return(0);
}
Write a C program to print nth Fibonacci number by using while loop.
Write a C program to display the n terms of odd natural numbers and their sum.
Write a program to calculate the factorial of an integer number n!
loop
#include <stdio.h>
int main()
{
int fact = 1,n,i;
printf("Enter the value of n:\n");
scanf("%d", &n);
for (i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of n:%d", fact);
return 0;
}
Exercise Program 6.2: The factorial of an integer m is
the product of consecutive integers from 1 to m.
That is, factorial m = m! = m*(m-1)*(m-2)………….*1.
Write a program that computes and prints a table of
factorials for any given m. [4th year Backlog 2017]
Write a program to calculate
1!+3!+5!+7!+……………………..+(2n-1)!
loop
#include<stdio.h>
int main()
{
int n, i, j, fact=1, sum=0 ;
printf("Enter n");
scanf("%d", &n);
for(i=1; i<=2*n-1; i+=2)
{
fact=1;
for(j=1; j<=i; j++)
loop
{
fact=fact*j;
}
sum=sum+fact;
}
printf("The sum=%d", sum);
return 0;
}
Series of Factorial Numbers
1 1 1 1
+ + ………………………….+
1! 2! 3! 𝑛!
1 1 1 1
+ + ………………………….+
1! 3! 5! (2𝑛−1)!

1 1 1 1
+ + ………………………….+
2! 4! 6! 30!

12 22 32 𝑁2
+ + ………………………….+
1! 2! 3! 𝑁!
Programming Exercises: Series of Numbers

1 1 1 1
1+ + + … … … … … … … … … … . +
1! 2! 3! 𝑛!

1 2 1 3 1 4
SUM = 1+( ) +( ) +( ) +………………………
2 3 4
Chapter Page Sample Program
1.5 7 Interest Calculation
1.7 10 Cosine Function
2.4 42 Interactive Investment Program
Case Study 46 Average of N numbers
Case Study 47 Temperature Conversion Problem

3.2 58 Sequence of squares of numbers


3.6 67 1 1 1
1+ + + ………………………..
2 3 𝑛
5.3 118 𝑥2 𝑥3 𝑥𝑛
𝑒𝑥 = 1 + 𝑥 + + + …………. +
2! 3! 𝑛!

You might also like