0% found this document useful (0 votes)
10 views

Programs

The document contains C program code to find the factors of a number, sum of n natural numbers using for, while, do-while loops and recursion, and find the largest, smallest, second largest, second smallest numbers in an array. It includes the algorithms and programs to solve each problem using different approaches like loops, functions, pointers, and recursion.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Programs

The document contains C program code to find the factors of a number, sum of n natural numbers using for, while, do-while loops and recursion, and find the largest, smallest, second largest, second smallest numbers in an array. It includes the algorithms and programs to solve each problem using different approaches like loops, functions, pointers, and recursion.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Programs

C and C++
Program to find factors of a number using loops

1 * 15 = 15
3 * 5 = 15
5 * 3 = 15
15 * 1 = 15
1, 3, 5, 15 are the factors of 15.
#include <stdio.h>

int main()
{
int num;
printf(“\nEnter the number : “);
scanf(“%d”,&num);
int i,count = 0;
printf(“\nThe factors of %d are : “,num);
for(i = 1;i <= num; i++)
{
if(num % i == 0)
{
++count;
printf(“%d “,i);
}
}
printf(“\n\nTotal factors of %d : %d\n”,num,count);
}
1 C Program to Find Sum of n Natural Numbers For Loop
1.1 Algorithm
1.2 Program
2 C Program to Find the Sum of n Natural Numbers Using While Loop
2.1 Algorithm
2.2 Program
3 C Program to Find the Sum of n Natural Numbers Using Do While Loop
3.1 Program
3.2 Output
4 C Program to Find the Sum of n Natural Numbers Using Function
4.1 Algorithm
4.2 Program
5 C Program to Find the Sum of n Natural Numbers Using Recursion
5.1 Algorithm
5.2 Program
//C Program to Find the Sum of n Natural Numbers Using For
loop

#include<stdio.h>
int main()
{
//Declaring Variable
int n, i, sum = 0 ;

//Input Number
printf("Enter a Number\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf("\nSum of %d Natural Numbers = %d",n, sum);
return 0;
}
//C Program to Find the Sum of n Natural Numbers Using While
Loop

#include<stdio.h>
void main()
{
int n, i=1, sum = 0 ;
printf("Enter a Number\n");
scanf("%d",&n);

while(i<=n)
{
sum = sum + i;
i++;
}
printf("\nSum of %d Natural Numbers = %d",n, sum);
}
//C Program to Find the Sum of n Natural Numbers Using Do
While Loop

#include<stdio.h>
int main()
{
int n, i=1, sum = 0 ;
printf("Enter A Number to Calculate Sum\n");
scanf("%d",&n);
do
{
sum = sum + i;
i++;
}while(i<=n);

printf("\nSum of %d Natural Numbers = %d",n, sum);

return 0;
}
//C Program to Find the Sum of n Natural Numbers Using function

#include<stdio.h>
int sum(int);
void main()
{
int n, i=1, s;

printf("Enter A Number\n");
scanf("%d",&n);

s=sum(n);
printf("\nSum of %d Natural Numbers = %d",n, s);
}
int sum(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
{
sum = sum + i;
}
return (sum);
}
//C Program to Find the Sum of n Natural Numbers Using Recursion

#include<stdio.h>
int sum(int);
void main()
{
int n, s;

printf("Enter A Number\n");
scanf("%d",&n);

s=sum(n);
printf("\nSum of %d Natural Numbers = %d",n, s);
}
int sum(int n)
{
int s=0;

if(n==1)
return (n);

s = n + sum(n-1);
return (s);
}
1 C Program To Find Largest And Smallest Number In An Array Using Pointer

2 C Program To Find Largest And Smallest Number In An Array Using Recursion

3 C Program To Find 2nd Largest And Smallest Number In An Array


//C Program To Find Largest and Smallest Number In An Array for(i=0;i<n;i++,*ptr++)
Using Pointer {
if(*ptr>larg)
#include<stdio.h> {
void main() larg=*ptr;
{ }
int i,*ptr, n,a[100],larg,small; }
ptr = &a[0];
printf("Enter How many number you want?:\n") ; small= *ptr;
scanf("%d",&n) ; for(i=0;i<n;i++,*ptr++)
{
printf("Enter %d numbers\n",n) ; if(*ptr<small)
for(i=0;i<n;i++) {
{ small=*ptr;
scanf("%d",&a[i]) ; }
} }
ptr = &a[0]; printf("%d is Largest Number\n",larg);
larg= *ptr; printf("%d is Smallest Number",small);
}
//C Program To Find Largest and Smallest Number In An Array Using Recursion

#include<stdio.h> int smallest(int a[],int n)


int Largest(int a[],int n)
int Largest(int a[],int n); {
{
int smallest(int a[],int n); int min;
int max;
void main()
{ if(n==1)
if(n==1)
int i,j,n,a[20]; return a[0];
return a[0];
printf("Enter a Number :");
scanf("%d",&n); else {
else {
printf("Enter Values : "); min=smallest(a,n-1);
max=Largest(a,n-1);
for(i=0;i<n;i++) if(min<a[n-1])
if(max>a[n-1])
{ return min;
return max;
scanf("%d",&a[i]); else
else
}
return a[n-1]; return a[n-1];
printf("\%d is Largest Number\n",Largest(a,n)); }
}
printf("\%d is Smallest Number\n",smallest(a,n));
}
}
}
//C Program To Find the 2nd Largest and smallest
Number In An Array
for (i = 2; i < n; i++) {
if (array[i] > Largest) { }
#include<stdio.h> else if (array[i] < secsmallest) {
void main() secLargest = Largest;
Largest = array[i]; secsmallest = array[i];
{ }
int i,n,array[100],Largest, secLargest, smallest, }
else if (array[i] > secLargest) { }
secsmallest; printf(" \nSecond Largest
printf("Enter How many number you want?:\n") ; secLargest = array[i];
} Element is %d", secLargest);
scanf("%d",&n) ; printf(" \nSecond Smallest
printf("Enter %d Values \n",n) ; }
if (array[0] < array[1]) { Element is %d", secsmallest);
for(i=0;i<n;i++) }
{ smallest = array[0];
scanf("%d",&array[i]) ; secsmallest = array[1];
} }
if (array[0] > array[1]) { else {
Largest = array[0]; smallest = array[1];
secLargest = array[1]; secsmallest = array[0];
} }
else { for (i = 2; i < n; i++) {
Largest = array[1]; if (array[i] < smallest) {
secLargest = array[0]; secsmallest = smallest;
} smallest = array[i];

You might also like