SlideShare ist ein Scribd-Unternehmen logo
1 von 81
C PROGRAMMING
R SARASWATHI
SRI AKILANDESWARI WOMENS COLLEGE
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
Exchanging the values of Two
Variables
Exchanging the values of Two
Variables
• Input : x = 10, y = 20;
• Output : x = 20, y = 10
• Input : x = 200, y = 100
• Output : x = 100, y = 200
Exchanging the values of Two
Variables
• Assign x to a temp variable : temp = x
• Assign y to x : x = y
• Assign temp to y : y = temp
FIRST STEP
SECOND STEP
THIRD STEP
FOURTH STEP
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;
}
Exchanging the values of Two
Variables
• Output:
• Enter Value of x 12
• Enter Value of y 14
• After Swapping: x = 14, y = 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;
}
ODD OR EVEN
• OUTPUT
Enter an integer: 15
15 Is Odd
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
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;
}
Summation of n Numbers
Output:
Enter the n number:10
SUM OF N(1 TO N) NUMBER:55
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);
}
1st loop
i=5
i<=10(true)
Sum=5
2nd loop
i=6
i<=10(true)
Sum=5+6(11)
3rd loop
i=7
i<=10(true)
Sum=11+7(18)
• 4th loop
i=8
i<=10(true)
Sum=18+8(26)
5th loop
i=9
i<=10(true)
Sum=26+9(35)
6th loop
i=10
i<=10(true)
Sum=35+10(45)
Summation of a set of Numbers
Output
STARTING NUMBER 5
ENDING NUMBER 10
Sum of a set of numbers 45
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);
COUNTING
len=strlen(str);
for(i=0;i<len;i++)
{
ch=str[i];
switch(tolower(ch))
{
case'a':
case'e':
case'i':
case'o':
case'u':
{
v++;
break;
}
case' ':
{
s++;
break;
}
default:
c++;
break;
COUNTING
if((str[i]==' ')&&(str[i-1]!=' '))
w++;
}
printf("nn The Entered string is:%s",str);
printf("nn No.of vowels:%d",v);
printf("nn No.of consonants:%d",c);
printf("nn No.of spaces:%d",s);
printf("nn No.of words:%d",w);
getch();
}
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
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
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);
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;
}
case 4:
{
printf("DIVISION OPERATION PERFORMEDn");
printf("RESULT:%d",a/b);
break;
}
case 5:
{
printf("MODULO DIVISION OPERATION
PERFORMEDn");
printf("RESULT:%d",a%b);
break;
}
} return 0;
}
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
Factorial Computation
Factorial Computation
• Factorial can be calculated using following
recursive formula.
• n! = n * (n-1)! n! = 1 if n = 0 or n = 1
#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);
}
1.factorial(n=5)
if (n == 0)
return 5*factorial(4)
2.factorial(n=4)
If(n==0)
Return 4*factorial(3)
3.factorial(n=3)
If(n==0)
Return 3*factorial(2)
4.factorial(n=2)
If(n==0)
Return 2*factorial(1)
5.factorial(n=1)
If(n==0)
Return 1*factorial(0)
6.factorial(0)
If(n==0)
Return 1
1*2*3*4*5=120
Factorial Computation
• FACTORIAL OF 5 IS :120
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
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
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));
}
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
ITERATION
• NPR
n!/n-r!
n=8 r=4 8!/4! Fact(n) fact(8)
8!
Fact(x) fact(8)
If(x==0) (false)
Return (8*fact(8-1))
Fact(7)
If(x==0) (false)
Return(7*fact(7-1)
Fact(6)
If(x==0) (false)
Return(6*fact(6-1)
Fact(5)
If(x==0) (false)
Return(5*fact(5-1)
Fact(4)
If(x==0) (false)
Return(4*fact(4-1)
Fact(3)
if(x==0) (false)
Return 3*fact(3-1)
Fact(2)
If(x==0) (false)
Return 2*fact(2-1)
Fact(1)
If(x==0) (false)
Return 1*fact(1-1)
Fact(0)
If(x==0) (true)return 1
1*2*3*4*5*6*7*8=40320
1*2*3*4=24
40320/24==1680
EXERCISE - 2
ENTER THE INTEGER NUMBER
TWO
ENTERED 2
Sine Function Computation
#include <stdio.h>
#include <math.h>
int main()
{
double x;
double result;
x = 2.3;
result = sin(x);
printf("sin(%.2lf) = %.2lfn", x, result);
x = -2.3;
result = sin(x);
printf("sin(%.2lf) = %.2lfn", x, result);
x = 0;
result = sin(x);
printf("sin(%.2lf) = %.2lfn", x, result);
return 0; }
Sine Function Computation
Output:
sin(2.30) = 0.75
sin(-2.30) = -0.75
sin(0.00) = 0.00
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.
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!
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;
}
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);
}
Sine Function Computation - output
Enter the value for x : 3
Enter the value for n : 5
The value of Sin(3.000000) =0.0523
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
Generation of the Fibonacci Sequence
• S.no Seq Technique for fibonaci
• 1- 0 (0)
• 2- 1 (1)
• 3- 1 (0+1)
• 4- 2 (1+1)
• 5- 3 (1+2)
• 6- 5 (2+3)
• 7- 8 (5+3)
• 8- 13 (5+8)
• 9- 21 (8+13)
• 10- 34 (13+21)
• 11- 55 (21+34)
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;
}
Generation of the Fibonacci Sequence
printf("n%d %d",n1,n2);//printing 0 and 1
1. for(i=2;2<15;++i(3))
{
n3=n1+n2; (0+1) 1
printf(" %d",n3); 1
n1=n2; n1=1
n2=n3; n3=1
2. for(i=2;3<15;++i(4))
{
n3=n1+n2; (1+1) 2
printf(" %d",n3); 2
n1=n2; n1=2
n2=n3; n3=1
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
Reversing the Digits of an Integer
• ENTER THE NUMBER TO REVERSE
– 1234
• REVERSE OF THE NUMBER
– 4321
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;
}
(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
Character to Number Conversion
#include <stdio.h>
int main()
{
char n;
printf("Enter the Option(Character)");
scanf("%c",&n);
printf("%d ", n);
return 0;
}
Character to Number Conversion
Enter the Option(Character):a
97
Enter the Option(Character):A
65
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);
}
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 √.
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
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);
}
Finding the square Root of a Number
• Output: 3.464102
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; }
OUTPUT
Square root of 50 is 7.071068
Square root of 17 is 4.123106
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;
}
The Smallest Divisor
• Given a number N, find the smallest
divisor of N
Examples:
• Input: 25
Output: 5
• Input: 31
Output: 31
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.
The Smallest Divisor
• Divisors of 305 = { 5, 7, 35, 61, 305,
427, 2135 }
• Smallest Divisor = 5
• Divisors of 49 = { 7, 7, 49 }
• Smallest Divisor = 7
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 );
}
• }
The Smallest Divisor
Output
Enter a Number: 2035
Smallest Divisor of 2035 = 5
Prime Numbers
• Any number which is divisible by 1 and itself is
known as prime number.
• Example:
1,2,3,5,7,11,13
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;
}
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.
Generation of Pseudo-random
Numbers
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int i;
for(i = 0; i<5; i++)
printf("%dt", rand()%10);
}
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.
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));}}
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; }
OUTPUT
• Please Enter any number to Find
Factors :6
• 2 is a Prime Factor
• 3 is a Prime Factor

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
Vishal Singh
 

Was ist angesagt? (20)

Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Progr2
Progr2Progr2
Progr2
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Progr3
Progr3Progr3
Progr3
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Cpl
CplCpl
Cpl
 
Program presentation
Program presentationProgram presentation
Program presentation
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Vcs17
Vcs17Vcs17
Vcs17
 
C Programming
C ProgrammingC Programming
C Programming
 

Ähnlich wie PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM

Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
LeahRachael
 

Ähnlich wie PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM (20)

L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
C important questions
C important questionsC important questions
C important questions
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
Vcs5
Vcs5Vcs5
Vcs5
 
Chapter 1 Programming Fundamentals Assignment.docx
Chapter 1 Programming Fundamentals Assignment.docxChapter 1 Programming Fundamentals Assignment.docx
Chapter 1 Programming Fundamentals Assignment.docx
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
C programs
C programsC programs
C programs
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 

Mehr von SaraswathiRamalingam

Mehr von SaraswathiRamalingam (20)

MACINTOSH
MACINTOSHMACINTOSH
MACINTOSH
 
XSL - XML STYLE SHEET
XSL - XML STYLE SHEETXSL - XML STYLE SHEET
XSL - XML STYLE SHEET
 
XML - SAX
XML - SAXXML - SAX
XML - SAX
 
DOM-XML
DOM-XMLDOM-XML
DOM-XML
 
X FILES
X FILESX FILES
X FILES
 
XML SCHEMAS
XML SCHEMASXML SCHEMAS
XML SCHEMAS
 
XML
XMLXML
XML
 
XML DTD DOCUMENT TYPE DEFINITION
XML DTD DOCUMENT TYPE DEFINITIONXML DTD DOCUMENT TYPE DEFINITION
XML DTD DOCUMENT TYPE DEFINITION
 
Georg scheutz - Charles babbage - Saraswathi Ramalingam
Georg scheutz - Charles babbage - Saraswathi RamalingamGeorg scheutz - Charles babbage - Saraswathi Ramalingam
Georg scheutz - Charles babbage - Saraswathi Ramalingam
 
Dennis ritchie - SARASWATHI RAMALINGAM
Dennis ritchie - SARASWATHI RAMALINGAMDennis ritchie - SARASWATHI RAMALINGAM
Dennis ritchie - SARASWATHI RAMALINGAM
 
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingamArithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAMPROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM

  • 1. C PROGRAMMING R SARASWATHI SRI AKILANDESWARI WOMENS COLLEGE
  • 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
  • 3. Exchanging the values of Two Variables
  • 4. Exchanging the values of Two Variables • Input : x = 10, y = 20; • Output : x = 20, y = 10 • Input : x = 200, y = 100 • Output : x = 100, y = 200
  • 5. Exchanging the values of Two Variables • Assign x to a temp variable : temp = x • Assign y to x : x = y • Assign temp to y : y = temp
  • 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; }
  • 13. ODD OR EVEN • OUTPUT Enter an integer: 15 15 Is Odd
  • 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); }
  • 19. • 4th loop i=8 i<=10(true) Sum=18+8(26) 5th loop i=9 i<=10(true) Sum=26+9(35) 6th loop i=10 i<=10(true) Sum=35+10(45)
  • 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);
  • 23. COUNTING if((str[i]==' ')&&(str[i-1]!=' ')) w++; } printf("nn The Entered string is:%s",str); printf("nn No.of vowels:%d",v); printf("nn No.of consonants:%d",c); printf("nn No.of spaces:%d",s); printf("nn No.of words:%d",w); getch(); }
  • 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; }
  • 28. case 4: { printf("DIVISION OPERATION PERFORMEDn"); printf("RESULT:%d",a/b); break; } case 5: { printf("MODULO DIVISION OPERATION PERFORMEDn"); printf("RESULT:%d",a%b); break; } } return 0; }
  • 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
  • 31. Factorial Computation • Factorial can be calculated using following recursive formula. • n! = n * (n-1)! n! = 1 if n = 0 or n = 1
  • 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); }
  • 33. 1.factorial(n=5) if (n == 0) return 5*factorial(4) 2.factorial(n=4) If(n==0) Return 4*factorial(3) 3.factorial(n=3) If(n==0) Return 3*factorial(2)
  • 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
  • 40. ITERATION • NPR n!/n-r! n=8 r=4 8!/4! Fact(n) fact(8) 8! Fact(x) fact(8) If(x==0) (false) Return (8*fact(8-1))
  • 41. Fact(7) If(x==0) (false) Return(7*fact(7-1) Fact(6) If(x==0) (false) Return(6*fact(6-1) Fact(5) If(x==0) (false) Return(5*fact(5-1) Fact(4) If(x==0) (false) Return(4*fact(4-1)
  • 42. Fact(3) if(x==0) (false) Return 3*fact(3-1) Fact(2) If(x==0) (false) Return 2*fact(2-1) Fact(1) If(x==0) (false) Return 1*fact(1-1) Fact(0) If(x==0) (true)return 1 1*2*3*4*5*6*7*8=40320 1*2*3*4=24 40320/24==1680
  • 43. EXERCISE - 2 ENTER THE INTEGER NUMBER TWO ENTERED 2
  • 44. Sine Function Computation #include <stdio.h> #include <math.h> int main() { double x; double result; x = 2.3; result = sin(x); printf("sin(%.2lf) = %.2lfn", x, result); x = -2.3; result = sin(x); printf("sin(%.2lf) = %.2lfn", x, result); x = 0; result = sin(x); printf("sin(%.2lf) = %.2lfn", x, result); return 0; }
  • 45. Sine Function Computation Output: sin(2.30) = 0.75 sin(-2.30) = -0.75 sin(0.00) = 0.00
  • 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
  • 52. Generation of the Fibonacci Sequence • S.no Seq Technique for fibonaci • 1- 0 (0) • 2- 1 (1) • 3- 1 (0+1) • 4- 2 (1+1) • 5- 3 (1+2) • 6- 5 (2+3) • 7- 8 (5+3) • 8- 13 (5+8) • 9- 21 (8+13) • 10- 34 (13+21) • 11- 55 (21+34)
  • 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; }
  • 54. Generation of the Fibonacci Sequence printf("n%d %d",n1,n2);//printing 0 and 1 1. for(i=2;2<15;++i(3)) { n3=n1+n2; (0+1) 1 printf(" %d",n3); 1 n1=n2; n1=1 n2=n3; n3=1 2. for(i=2;3<15;++i(4)) { n3=n1+n2; (1+1) 2 printf(" %d",n3); 2 n1=n2; n1=2 n2=n3; n3=1
  • 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); }
  • 65. Finding the square Root of a Number • Output: 3.464102
  • 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; }
  • 67. OUTPUT Square root of 50 is 7.071068 Square root of 17 is 4.123106
  • 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.
  • 71. The Smallest Divisor • Divisors of 305 = { 5, 7, 35, 61, 305, 427, 2135 } • Smallest Divisor = 5 • Divisors of 49 = { 7, 7, 49 } • Smallest Divisor = 7
  • 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 ); } • }
  • 73. The Smallest Divisor Output Enter a Number: 2035 Smallest Divisor of 2035 = 5
  • 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.
  • 77. Generation of Pseudo-random Numbers #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { srand(time(NULL)); int i; for(i = 0; i<5; i++) printf("%dt", rand()%10); }
  • 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