-:PROGRAM TO FIND SUM AND PRODUCT OF ALL DIGITS OF A NUMBER:-
#include <stdio.h>
intmain()
{
int n;
int dig,sum,pro;
printf("\nEnter an integer number :");
scanf("%d",&n);
/*Calculating Sum and Product*/
sum=0;
pro=1;
while(n>0)
{
dig=n%10;/*get digit*/
sum+=dig;
pro*=dig;
n=n/10;
}
printf("\nSUM of all Digits is : %d",sum);
printf("\nPRODUCT of all digits: %d",pro);
return 0;
}
1
OUTPUT:-
-:PROGRAM TO REVERSE OF A NUMBER:-
#include<stdio.h>
#include<conio.h>
void main()
{
int s=0,r,n;
clrscr();
2
printf("Enter any number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
n=n/10;
s=s*10+r;
}
printf("The reverse of the number is=%d",s);
getch();
}
OUTPUT:-
3
-:PROGRAM TO FIND SUM OF SERIES:-
#include <stdio.h>
// Function to return sum of 1/1 + 1/2 + 1/3 + ..+ 1/n
doublesum(intn)
{
doublei, s = 0.0;
for(i = 1; i<= n; i++)
s = s + 1/i;
returns;
}
intmain()
{
intn = 5;
printf("Sum is %f", sum(n));
return0;
}
4
OUTPUT:-
5
-:PROGRAM TO CHECK A STRING IS PALINDROME OR NOT:-
#include<stdio.h>
#include<conio.h>
void main()
{
intl,i=0,p=0;
char st[30];
clrscr();
printf("Enter any string: ");
scanf("%[^\n]",st);
while(st[i]!='\0')
i++;
l=i;
for(i=0;i<l/2;i++)
if(st[i]!=st[l-i-1])
{
p=1;
break;
}
if(p==0)
printf("The string is palindrome");
else
printf("The string is not palindrome");
getch();
}
6
7
-:A FUNCTION TO FIND WHETHER A GIVEN NO. IS PRIME OR NOT:-
#include<stdio.h>
int checkprimenumber(int n);
int main()
{
int n1, n2, i, flag;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("prime numbers between %d and %d are : ",n1,n2);
for(i=n1+1; i<n2; ++i)
{
// i is a prime number, flag will be equal to 1
flag = checkprimenumber(i);
if(flag ==1)
printf("%d", i);
}
return 0;
}
// user-defined function to check prime number
int checkprimenumber(int n)
{
int j, flag=1;
for(j=2; j<=n/2; ++j)
{
if(n%j==0)
{
flag = 0;
break;
}
}
return flag;
}
8
OUTPUT:-
9
-:C++ PROGRAM TO DISPLAY THE FACTORS OF THE ENTERED NUMBER:-
#include<iostream.h>
using namespace std;
intmain()
{
int number, temp =1;
cout<<"Enter the number to determine its factors : "<<endl;
cin>> number;
cout<<"The factors of "<< number <<" are : "<<endl;
while(temp <= number)
{
if(not(number % temp))
cout<< temp <<" ";
temp++;
}
cout<<endl;
}
10
OUTPUT:-
11
-:PROGRAM TO SWAP TWO NUMBERS:-
#include<iostream.h>
#include<conio.h>
#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}
void main()
{
clrscr();
intx,y;
cout<<“Enter two numbers:”;
cin>>x>>y;
cout<<“x=”<<x<<” y=”<<y;
SWAP(x,y);
cout<<“nx=”<<x<<” y=”<<y;
getch();
}
12
OUTPUT:-
-:PROGRAM TO PRINT A TRIANGLE OF STARS:-
#include<stdio.h>
intmain()
{
inti, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i, k=0)
{
for(space=1; space<=rows-i;++space)
{
13
printf(" ");
}
while(k !=2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
return0;
}
OUTPUT:-
14
-: PROGRAM TO SWAP TWO NUMBERS USING POINTERS:-
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
15
OUTPUT:-
16
#include<stdio.h>
intmain()
{
intradius;
floatPI=3.14,area,ci;
printf(" Enter the Radius of Circle: ");
scanf("%d",&radius);
area = PI * radius * radius;
printf("\n Area of a Circle : %f ",area);
ci = 2 * PI * radius;
printf("\n Circumference of a Circle is : %f ",ci);
return(0);
}
Assignment: 12
17
OUTPUT:-
18
-:PROGRAM FIND SUM OF N ELEMENTS:-
#include <stdio.h>
#include <stdlib.h>
intmain()
{
intn,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL){
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i){
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d\n",sum);
free(ptr);
return 0;
}
19
OUTPUT:-
20
-:C++ PROGRAM TO MERGE TWO SORTED ARRAYS:-
#include<iostream>
using namespace std;
// Merge arr1[0..n1-1] and arr2[0..n2-1] into
// arr3[0..n1+n2-1]
void mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[])
{
int i = 0, j = 0, k = 0;
// Traverse both array
while (i<n1 && j <n2)
{
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
// Store remaining elements of first array
while (i < n1)
arr3[k++] = arr1[i++];
// Store remaining elements of second array
while (j < n2)
arr3[k++] = arr2[j++];
}
// Driver code
int main()
{
int arr1[] = {1, 3, 5, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr2[] = {2, 4, 6, 8};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
int arr3[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
cout << "Array after merging" <<endl;
for (int i=0; i < n1+n2; i++)
cout << arr3[i] << " ";
21
return 0;
}
-:PROGRAM TO DISPLAY FIBONACCI SERIES:-
I) Using recurtion
#include<stdio.h>
int f(int);
int main()
{
int n, i = 0, c;
clrscr();
printf("Enter a Total Number to be Generated : ");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for (c = 1; c <= n; c++)
{
printf("%d\n", f(i));
i++;
}
return 0;
}
int f(int n)
{
if (n == 0 || n == 1)
return n;
else
return (f(n-1) + f(n-2));
}
22
OUTPUT:-
23
-:PROGRAM TO DISPLAY FACTORIAL OF A NUMBER:-
II) Using recurtion
#include <stdio.h>
int factorial(int);
intmain()
{
intnum;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d",&num);
if(num<0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n",num, result);
}
return0;
}
intfactorial(intnum)
{
if(num==0||num==1)
{
return1;
}
else
{
return(num* factorial(num-1));
}
}
24
OUTPUT:-
25
-: PROGRAM TO CALCULATE GCD OF TWO NUMBERS:-
WITH RECURTION-
#include <stdio.h>
int gcd(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}}
26
OUTPUT-
27