Problem Solving Using c
Problem Solving Using c
FLOWCHART:
PROGRAM:
#include<stdio.h>
int main( )
{
int a,b,c;
int sum,average;
printf("Enter any three integers: ");
scanf("%d%d %d",&a,&b,&c);
sum = a+b+c;
average=sum/3
printf("Sum and average of three integers: %d %d",sum,average);
return 0;
}
INPUT:
Enter any three integers: 2 4 5
OUTPUT:
Sum and average of three integers: 11 3
2. Write a C program to find the sum of individual digits of positive
integer.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0)
Begin
Step 5: r←n%10
Step 6: sum←sum+r
Step 7: n←n/10
End
Step 8: Print “sum”
Step 9: Stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,r,sum=0;
clrscr( );
printf("ENTER A POSITIVE INTEGER \n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUMOF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..
%d",sum);
getch( );
}
INPUT:
ENTER A POSITIVE INTEGER
5321
OUTPUT:
THE SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..11
3). Write a C program to generate the first n terms of the Fibonacci
Sequence.
ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4 :i=0
Step 5 : while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1; If not goto step 7
Step 6 : Stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int f0,f1,f,n,i;
clrscr( );
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d TERMS:\n",n);
i=0;
while(i<n)
{
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
}
INPUT:
ENTER THE VALUE FOR n
10
OUTPUT:
FIBONACCI SEQUENCE FOR THE FIRST 10 TERMS:
0 1 1 2 3 5 8 13 21 34
4) Write a C program to generate all prime numbers between 1 and n.
Where n is the value supplied by the user.
Description:
Prime number is a number which is exactly divisible by one and itself
only Ex: 2, 3,5,7,………;
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize i=1,c=0
Step 4:if i<=n goto step 5
If not goto step 10
Step 5: initialize j=1
Step 6: if j<=i do the following. If no goto step 7
i)if i%j= =0 increment c
ii) increment j
iii) goto Step 6
Step 7: if c= = 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,fact,j;
clrscr( );
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("\n %d",i);
}
getch( );
}
INPUT:
Enter the number : 5
OUTPUT:
2 3 5
4) Write a C program to Check whether given number is Armstrong
Number or Not.
FLOWCHART:
PROGRAM:
#include<stdio.h>
int main( )
{
int n, n1,rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num= =n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}
INPUT:
Enter a positive integer: 371
OUTPUT:
371 is an Armstrong number.
5) Write a C program to check whether given number is perfect number or
Not.
AIM: To Check whether given number is perfect number or not
Description:
A perfect number is a positive integer that is equal to the sum of its
proper positive divisors, that is, the sum of its positive divisors excluding the
number itself.
Ex:
6X1=6
3x2=6
2x3=6
1,2,3 are factors of 6
So 1+2+3=6
ALGORITHM:
Step 1: read n
Step 2: assign i=1,sum=0
Step 3: while(i<n) goto step 4
Step 4: if(n%i==0)
sum=sum+i
i++
Step 5: if(sum==n) print given number is perfect number otherwise not a perfect
number.
PROGRAM:
#include<stdio.h>
int main( )
{
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum= =n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
INPUT:
Enter a number:6
OUTPUT:
6 is a perfect number
6) Write a C program to check whether a number is strong number or not.
AIM: To check whether given number is strong number or not
Description: Strong numbers are the numbers whose sum of factorial of digits
is equal to the original number. Example: 145 is a strong number. Ex:1!+4!+5!
=1+24+120=145.
ALGORITHM:
Step 1:read num,i,f,r,sum=0,temp
Step 2: assign num to temp
Step 3: while(num) goto step 4
Step 4: i=1,f=1
r=num%10
while(i<=r) goto step 5
Step 5: f=f*i
i=i+1
Step 6: sum=sum+f;
Step 7: num=num/10;
Step 8: if sum and temp are equal got step 9
Step 9: print strong number otherwise not a strong number
PROGRAM:
#include<stdio.h>
int main( )
{
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
i=1,f=1;
r=num%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
INPUT:
Enter a number:145
OUTPUT:
145 is a strong number
7) Write a C program which takes two integer operands and one operator
from the user, performs the operation and then prints the result.(Consider
the operators +,-,*,/,% and use Switch Statement.)
AIM: To perform arithmetic operations using switch statement.
Description: It is also called as multi-way decision statement. The switch
statement successively tests the value of an expression against a given list for
matching .If match is found ,then that block is executed.
ALGORITHM:
Step 1: Read a,b
Step 2: Print “Menu Options”
Step 3: do Begin
Step 4: Read ch
Step 5: switch(ch)
Begin Step 6:
case 1:
Begin
Calculate c = a+b
Print “c”
break;
End
case 2:
Begin
Calculate c = a-b
Print “c”
break;
End
case 3:
Begin
Calculate c = a*b
Print “c”
break;
End
case 4:
Begin
Calculate c = a/b
Print “c”
break;
End
case 5:
Begin
Calculate c = a%b
Print “c”
break;
End
default:
Print “Invalid choice”
End
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,ch;
clrscr( );
printf("ENTER TWO VALUES FOR a & b\n");
scanf("%d %d",&a,&b);
while(1)
{
printf("MENU OPTIONS \n");
printf("************\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("5.Modulus\n");
printf(“6.Exit\n”);
printf("\n");
printf("ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("The addition of %d and %d is..%d\n",a,b,c); break;
case 2: c=a-b;
printf("The subtraction of %d and %d is..%d\n",a,b,c); break;
case 3: c=a*b;
printf("The multiplication of %d and %d is..%d\n",a,b,c); break;
case 4: c=a/b;
printf("The division of %d and %d is..%d\n",a,b,c); break;
case 5: c=a%b;
printf("The modulus of %d and %d is..%d\n",a,b,c); break;
case 6:exit(0); default:printf("INVALID CHOICE\n"); }
}
getch();
}
INPUT:
ENTER TWO VALUES FOR a & b: 20 16
OUTPUT:
MENU OPTIONS
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulus
6.Exit
ENTER UR CHOICE 1
The addition of 20 and 16 is..36
ENTER UR CHOICE 2
The subtraction of 20 and 16 is..4
ENTER UR CHOICE 3
The multiplication of 20 and 16 is..320
ENTER UR CHOICE 4…
ENTER UR CHOICE 5…