Fundamental Algorithms, Exchanging the values of Two Variables,Counting,Summation of a Set of Numbers,Factorial Computation ,Sine Function Computation ,Generation of the Fibonacci Sequence,Reversing the Digits of an Integer,Base Conversion , Character to Number Conversion , Factoring Methods: Finding the square Root of a Number ,The Smallest Divisor of an Integer,The Greatest Common Divisor of the two integers,Generating Prime Numbers,Computing the Prime Factors of an integer ,Generation of Pseudo,random Numbers,Raising a Number to a Large Power,Computing the nth Fibonacci Number,thiruvalluvar university syllabus,unit 5,computer science basics,new syllabus,programming in c,example program in c,
2. UNIT
Fundamental Algorithms: Exchanging the values of Two
Variables- Counting- Summation of a Set of
Numbers-Factorial Computation -Sine Function
Computation –Generation of the Fibonacci
Sequence-Reversing the Digits of an Integer- Base
Conversion – Character to Number Conversion -
Factoring Methods: Finding the square Root of a
Number –The Smallest Divisor of an Integer-The
Greatest Common Divisor of the two integers-
Generating Prime Numbers- Computing the Prime
Factors of an integer –Generation of Pseudo-
random Numbers-Raising a Number to a Large
Power-Computing the nth Fibonacci Number
10. Exchanging the values of Two
Variables
#include <stdio.h>
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("nEnter Value of y ");
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
11. Exchanging the values of Two
Variables
• Output:
• Enter Value of x 12
• Enter Value of y 14
• After Swapping: x = 14, y = 12
12. ODD OR EVEN
#include <stdio.h>
int main()
{
int a;
printf("Enter an integer: ");
scanf("%d", &a);
// True if num is perfectly divisible by 2
if(a % 2 == 0)
printf("%d is even.", a);
else
printf("%d is odd.", a);
return 0;
}
14. Summation of n Numbers
• Sum of n numbers 1 to n numbers
• Example:
• 1 to 5(n=5)
1+2+3+4+5=15
• 1 to 10(n=10)
1+2+3+4+5+6+7+8+9+10=55
• Formula : n(n+1)/2
15. Summation of n Numbers
#include <stdio.h>
int main()
{
int n=0,sum;
printf(“Enter the N number(Ending Number):”);
scanf(“%d”,&n);
sum=n*(n+1)/2;
printf("SUM OF N NUMBERS:%d",sum);
return 0;
}
16. Summation of n Numbers
Output:
Enter the n number:10
SUM OF N(1 TO N) NUMBER:55
17. Summation of a set of Numbers
#include <stdio.h>
void main()
{
int i, snum,endnum, sum = 0;
printf("Enter an integer number(STARTING NUMBER n");
scanf ("%d", &snum);
printf("Enter an integer number(ENDING NUMBER n");
scanf ("%d", &endnum);
for (i = snum; i <= endnum; i++)
{
sum = sum + i;
}
printf ("Sum of a set of numbers = %dn", sum);
}
20. Summation of a set of Numbers
Output
STARTING NUMBER 5
ENDING NUMBER 10
Sum of a set of numbers 45
21. COUNTING
/* 3.COUNTING OF NO.OF VOWELS,CONSTANTS,WORDS,WHILE
SPACE IN A LINE TEXT */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80],ch;
int i,c=0,s=0,v=0,w=1,len;
clrscr();
printf("nt COUNTING CHARACTERS IN A LINE OF TEXT ");
printf("nt ****************************************** ");
printf("nntEnter the string: ");
gets(str);
24. COUNTING
INPUT :
COUNTING CHARACTERS IN A LINE OF TEXT
******************************************
Enter the string : welcome to computer science department
OUTPUT :
The Entered string is : welcome to computer science department
No.of vowels :13
No.of consonants :21
No.of spaces :4
No.of words:5
25. EXERCISE
WRITE PROGRAM
OUTPUT
ENTER THE OPTION
1.ADD(+)
2.SUBRACTION(-)
3.MULTIPLY(*)
4.DIVISION(/)
5.EXIT
+
ENTER THE 2 VALUES FOR ADD
2
3
ADDITION OF TWO NUMBERS
5
26. Simple arithmetic on two
numbers – ( using switch/case )
#include <stdio.h>
int main(void)
{
int option,a,b;
printf("SIMPLE ARITHMETIC OPERATIONnn");
printf("SELECT YOUR OPTION FROM THE LISTn");
printf("1.ADDITIONn");
printf("2.SUBRATCTIONn");
printf("3.MULTIPLICATIONn");
printf("4.DIVISIONn");
printf("5.MODULOn");
scanf("%d",&option);
printf("Enter Two Variable For Arithmetic Operations");
scanf("%d",&a);scanf("%d",&b);
27. switch(option)
{
case 1:
{
printf("ADDITION OPERATION PERFORMEDn");
printf("RESULT:%d",a+b);
break;
}
case 2:
{
printf("SUBRACTION OPERATION PERFORMEDn");
printf("RESULT:%d",a-b);
break;
}
case 3:
{
printf("MULTIPLICATION OPERTAION PERFORMEDn");
printf("RESULT:%d",a*b);
break;
}
29. Simple arithmetic on two
numbers – ( using switch/case )
OUTPUT
SIMPLE ARITHMETIC OPERATION
SELECT YOUR OPTION FROM THE LIST
1.ADDITION
2.SUBRATCTION
3.MULTIPLICATION
4.DIVISION
5.MODULO
EXIT(e)
1
Enter Two Variable For Arithmetic Operations2
3
ADDITION OPERTION PERFORMED
RESULT:5
32. #include <stdio.h>
int main()
{
unsigned int factorial(unsigned int n);
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
36. NPR
• nPr Formula
• Let's suppose there are n number of players,
in which we have to arrange r number of
players. Therefore, to arrange r things out
of n, use permutation formula:
• nPr = n!/(n-r)!
• Note - Permutation value shows the ways
to arrange r things out of n
37. NCR
• nCr Formula
• Let's suppose there are n number of players,
in which we have to select r number of
players. Therefore, to select r things out of n,
use combination formula:
• nCr = n!/r!(n-r)!
• Note - Combination value shows the ways
to select r things out of n
38. NPR NCR#include<stdio.h>
#include<conio.h>
long int fact(int x);
long int n,r,x;
void main()
{
clrscr();
printf("nntNPR AND NCR PROGRAM");
printf("nnt************************nn") ;
printf("nt Enter the value of n: ");
scanf("%ld",&n);
printf("nt Enter the value of r: ");
scanf("%ld",&r);
printf("nn The result of nPr is: %ld n",fact(n)/fact(n-r));
printf("nn The result of nCr is: %ld n",fact(n)/(fact(r)*fact(n-r)));
getch();
}
long int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}
39. NPR NCR
INPUT
NPR AND NCR PROGRAM
************************
Enter the value of n: 8
Enter the value of r: 4
OUTPUT
The result of nPr is: 1680
The result of nCr is: 420
46. Sine Function Computation
(Taylor series)
• sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - ... +
+ (-1)^(n-1)*x^(2n-1)/(2n-1)! + ....
• A Taylor Series is an expansion of some function
into an infinite sum of terms, where each term
has a larger exponent like x, x2, x3, etc.
47. Sine Function Computation
(Taylor series)
• X=3 N=5
• Sin(x)=x-X3/3!+x5/5!-x7/7!....xn/n!
1. x=3*3.14157/180=0.0524(Converting ‘x’
to radian value)
• Sin(3)= 0.0524-0.05243/3!+ 0.05245/5!
48. Sine Function Computation
#include<stdio.h>
#include<conio.h>
#include <math.h>
float fact(int);
void main()
{
float x,x1,temp,sum=0.0;
int i,sign=-1,n;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x1=x*3.14157/180;
for(i=1;i<=n;i=i+2)
{
temp=pow(x1,i)/fact(i);
sign=sign*(-1);
sum +=temp*sign;
}
49. Sine Function Computation
printf(" The value of Sin(%f) =%.4f",x,sum);
getch();
}
float fact(int a)
{
int i,x=1;
for(i=1;i<=a;i++)
x=x*i;
return(x);
}
50. Sine Function Computation - output
Enter the value for x : 3
Enter the value for n : 5
The value of Sin(3.000000) =0.0523
51. Generation of the Fibonacci Sequence
• The Fibonacci sequence is a series of
numbers where a number is the
addition of the last two numbers,
starting with 0, and 1.
• The Fibonacci Sequence: 0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55…
• Written as a rule, the expression is:
Xn = Xn-1 + Xn-2
53. Generation of the Fibonacci Sequence
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and
1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
55. Generation of the Fibonacci Sequence
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233
377
56. Reversing the Digits of an Integer
• ENTER THE NUMBER TO REVERSE
– 1234
• REVERSE OF THE NUMBER
– 4321
57. Reversing the Digits of an Integer
#include <stdio.h>
int main()
{
int n, r = 0;
printf("Enter a number to reversen");
scanf("%d", &n);
while (n != 0)
{
r = r * 10;
r = r + n%10;
n = n/10;
}
printf("Reverse of the number = %dn", r);
return 0;
}
58. (n=1234 r=0)
r = r * 10; 0*10=0
r = r + n%10; 0+1234%10= 4
n = n/10; 1234/10=123
(n=123 r=4)
r = r * 10; 4*10=40
r = r + n%10; 40+123%10=43
n = n/10; 123/10=12
(n=12 r=43)
r = r * 10; 43*10=430
r = r + n%10; 430+12%10=432
n = n/10; 12/10=1
(n=1 r=432)
r = r * 10; 432*10=4320
r = r + n%10; 4320+1%10=4321
n = n/10; 1/10=0
(n=0 r=4321)
While(n!=0) TRUE
59. Character to Number Conversion
#include <stdio.h>
int main()
{
char n;
printf("Enter the Option(Character)");
scanf("%c",&n);
printf("%d ", n);
return 0;
}
60. Character to Number Conversion
Enter the Option(Character):a
97
Enter the Option(Character):A
65
61. Base Conversion
int main()
{
int a,b,c;
printf("Please enter a number in base 10: ");
scanf("%d",&a);
printf("nPlease enter the base that you want the number to be
converted to: ");
scanf("%d",&b);
do {
c=a%b;
printf("%d",c);
a/=b; }
while(c!=0);
}
62. Finding the square Root of a Number
• When a value is multiplied by itself to give the
original number then that number is a square
root. It is represented by a radical symbol √.
63. Finding the square Root of a Number
• Given a number N, the task is to write
a C program to find the square root of
the given number N.
• Examples:
• Input: N = 12
Output: 3.464102
• Input: N = 16
Output: 4
64. Finding the square Root of a Number
#include <math.h>
#include <stdio.h>
double findsqrt(double N);
// Driver Code
int main()
{
// Given number
int N = 12;
// Function call
printf("%f ", findsqrt(N));
return 0;
}
// Function to find the square-root of N
double findsqrt(double N)
{
return sqrt(N);
}
66. WITHOUT SQRT()
#include <stdio.h>
int main(void){
float square_Root(float n);
int n = 50;
printf("Square root of %d is %f", n, square_Root(n));
n = 17;
printf("nSquare root of %d is %f", n, square_Root(n));
return 0;
}
float square_Root(float n) {
float a = n;
float b = 1;
double e = 0.000001;
while (a - b > e) {
a = (a + b) / 2;
b = n / a;
}
return a; }
68. Greatest Common Divisor
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{ // Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
69. The Smallest Divisor
• Given a number N, find the smallest
divisor of N
Examples:
• Input: 25
Output: 5
• Input: 31
Output: 31
70. The Smallest Divisor
• Check if the number is divisible by 2
or not.
• Iterate from i = 3 to sqrt(N) and
making a jump of 2.
• If any of the numbers divide N then it
is the smallest divisor.
• If none of them divide, then N is the
answer.
72. The Smallest Divisor
#include <stdio.h>
#include <math.h>
int main() {
int n,i;
printf("Enter a Number: ");
scanf("%d", &n);
for (i = 2; i <= sqrt(n); ++i)
{
if (n % i == 0)
{
printf("Smallest Divisor of %d = %d", n, i);
break;
}
printf("Smallest Divisor of %d = %d", n,n );
}
• }
74. Prime Numbers
• Any number which is divisible by 1 and itself is
known as prime number.
• Example:
1,2,3,5,7,11,13
75. Prime Numbers
#include<stdio.h>
int main()
{
int a,i,f;
printf("Enter a number: ");
scanf("%d",&a);
f=0;
i=2;
while(i <= a/2)
{
if(a%i == 0)
{
f=1;
break;
}
i++;
}
if(f==0)
printf("Prime Number")
else
printf("Not Prime Number");
return 0;
}
76. Generation of Pseudo-random
Numbers
• Pseudo Random Number
Generator(PRNG) refers to an
algorithm that uses mathematical
formulas to produce sequences of
random numbers.
• PRNGs generate a sequence of
numbers approximating the properties
of random numbers.
78. Generation of Pseudo-random
Numbers
• Output 1:
• 3 7 0 9 8
• Output 2:
• 7 6 8 1 4
• Explanation: srand() sets the seed which is
used by rand() to generate random numbers.
• time(NULL) return no. of second from JAN 1,
1971 i.e every time we run program we have
difference of few seconds which gives the
program new seed.
79. Computing the nth Fibonacci Number
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
result = fibo(num);
printf("The %d number in fibonacci series is %dn", num, result);
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));}}
80. Computing the Prime Factors
#include <stdio.h>
int main()
{
int i, j, Number, isPrime;
printf("n Please Enter any number to Find Factors : ");
scanf("%d", &Number);
for (i = 2; i <= Number; i++)
{
if(Number % i == 0)
{
isPrime = 1;
for (j = 2; j <= i/2; j++)
{
if(i % j == 0)
{
isPrime = 0;
break;
} }
if(isPrime == 1)
{
printf("n %d is a Prime Factor ", i);
} } }
return 0; }
81. OUTPUT
• Please Enter any number to Find
Factors :6
• 2 is a Prime Factor
• 3 is a Prime Factor