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

C Programming Assignment 04 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Programming Assignment 04 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

NAME: TRINETRA BANERJEE

SEC : G ; CLASS ROLL : 58


UNIVERSITY ROLL NO : 10930621054
STREAM : AIML
C PROGRAMMING ASSIGNMENT 04
Q 1 ) Write a c program that will check whether a number is armstrong or not .

ALGORITHM :

1 , Start
2 . Take number , remainder , total = 0 , temp .
3 . Temp = number .
4 . While( number > 0 )
5. Remainder = number % 10 .
6. Number = number/10 .
7 . Use if else condition .
8 . If ( temp = total ) then print that " this is a armstrong number . "
9 . Or else print that " this is not a armstrong number " .
C CODE :

#include<stdio.h>
int main(){
int number,remainder,total=0,temp;
printf("enter the number=");
scanf("%d",&number);
temp=number;
while(number>0){
remainder=number%10;
total=total+(remainder*remainder*remainder);
number=number/10;
}
if(temp==total) {
printf("This number is Armstrong number");
}
else {
printf("This number is not Armstrong number");
}
return 0;
}
Start Stop
FLOWCHART :

Assign total = 0
Print " This is a armstrong Print " This is not a armstrong
number " . number " .

Read a num .
Yes No

If num==sum
Assign temp = num .

Whether Remainder = number%10


temp = 0 Total = total+(remainder*remainder*remainder)
Number = number/10 .
• REMARKS :

There are many other ways also to solve this problem


Q 2 ) Write a c program to print all the armstrong number between a given range .

ALGORITHM :
1 . Start
2 . Take n , i , t , c , d , s=0 .3 . Print all armstrong number from 1 to 700 .
4 . Use for loop .
5 . For( i = 1 ; i<=n ; i++ )
6 .t=i , s = 0 .
7 . While( t!=0)
8 .d=t%10
9 . c=d*d*d .
10 . S=s+c .
11 .t = t/10 .
12 . If(s==i) then it will show all the armstrong number for a selected range .
13 . Stop .
Start Stop
FLOWCHART :

Assign s = 0 .
Print the armstrong number
within a selected range .

Read the
range of the
number
If s==i

Assign t=i , s=0

d=t%10
c=d*d*d
Whether s=s+c
t!=0 t=t/10
C CODE :
#include<stdio.h>
#include<conio.h>
void main() {
int n,i,t,c,d,s=0;
clrscr();
printf("\n Enter the range from 0 to : ");
scanf("%d",&n);
printf("\n Armstrong Numbers are :");
for(i=0;i<=n;i++) {
t=i; s=0;
while(t!=0) {
d=t%10;
c=d*d*d;
s=s+c;
t=t/10;
}
if(s==i){
printf(" %d",i);
}
getch();
}
• REMARKS :
There are many other ways also to solve this problem .
Q3 ) Write a c program that will perform Ackermann function using recursion .

ALGORITHM :

1 . Start
2 . Take int A(int m , int n ) .
3 . Enter two numbers .
4 . Scan it .
5 . Print the output .
6 . If ( m==0) then return n+1 .
7 . Else if(n==0)then returnsA(m-1,1).
8 . Else return A( m-1,A(m,n-1)) .
9 . Stop .
C CODE :

#include<stdio.h>
int A(int m, int n);
Int main() { int m,n;
printf("Enter two numbers :: \n");
scanf("%d%d",&m,&n);
printf("\nOUTPUT :: %d\n",A(m,n));
}
int A(int m, int n) {
if(m==0)
return n+1;
else if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
}
REMARKS :

● There are many other ways also to solve this problem .


Q 4 ) Write a c program that will compute the roots of a quadratic equation .

ALGORITHM :
1 . Start .
2 . The value of a , b , c has to be entered .
3 . Calculate the value of discriment d = b^2 – 4ac .
4 . Check whether d is less than 0 or greater than 0 .
5 . d is imaginary if it is " less than 0 " .
6 . Roots are displayed as output .
7 . Stop .
#include<stdio.h> C CODE :
#include<conio.h>
#include<math.h>
void main()
{
float a=0,b=0,c=0,root=0,root1=0,root2=0,root3=0;
printf("\nEnter the value of a, b and c as in quadratic equation ax2 + bx + c = 0 :");
scanf("%f%f%f",&a,&b,&c);
root=b*b-(4*a*c);
if(root>0)
{
root of equation
root3=pow(root,0.5);
root1=(-b+root3)/(2*a);
root2=(-b-root3)/(2*a);
printf("\nBoth roots are different and unique\nRoot 1 = %.2f\nRoot 2 = %.2f",root1,root2);
}
else if(root<0)
{
root3=pow(-root,0.5)/(2*a);
root1=(-b)/(2*a);
printf("\nBoth roots are imaginary\nRoot 1 = %.2f + %.2fi\nRoot 2 = %.2f -
%.2fi",root1,root3,root1,root3);
}
else if(root==0)
{
root1=(-b)/(2*a);
printf("\nBoth roots are equal\n Root = %.2f",root1);
}
getch();
}
REMARKS :

● There are many other ways also to solve this problem .


Q 6 ) Write a c program that will perform quick sort using recursion .

ALGORITHM :
1 . Start .
2 . Print " how many elemnt you wanyt to sort " .
3 . Use for loop .
5 . If low<high then pivot = low , I = low .
6 . Use temp = list[i]
7 . List[I]=list[j]
8 . List[j]=temp .
9 . Stop .
#include <stdio.h>
void quicksort (int [], int, int);
int main() { int list[50];
int size, i;
C CODE :
printf("How many elements u want to Sort :: ");
scanf("%d", &size);
printf("\nEnter the elements below to be sorted :: \n");
for (i = 0; i < size; i++) {
printf("\nEnter [ %d ] element :: ",i+1);
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("\nAfter implementing Quick sort, Sorted List is :: \n\n");
for (i = 0; i < size; i++) {
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
void quicksort(int list[], int low, int high) {
int pivot, i, j, temp;
if (low < high) {
pivot = low; i = low;
j = high;
while (i < j) {
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low) {
j--;
}
if (i < j) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
REMARKS :

● There are many other ways also to solve this problem .

You might also like