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

Practical No3

Uploaded by

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

Practical No3

Uploaded by

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

COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.1
AIM:To check whether the Number is Armstrong or Not.
Algorithm :
1. Start the program.
2. Read an integer num from user.
3. Intialize variable temp,x,sum=0.
4. If num>0,Repeat step 5 to 7.
Else go to step 8.
5. x=num%10.
6. num=num/10.
7. sum=x*x*x+sum.
8. Print:sum.
9. If sum==temp
Print :The number is Armstrong.
Else
Print:The number is not Armstrong.
10.End the program.

CSE B1 | 1
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Flowchart :

CSE B1 | 2
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code (While):
#include<stdio.h>
int main()
{
int a,b,c,d,e=0;
printf("Enter a Number : ");
scanf("%d",&a);
b=a;
while(a>0)
{
c=a%10;
a=a/10;
d=c*c*c;
e=e+d;
}
if(b==e)
{
printf("Armstrong");

}
else
{
printf("No");
}
return 0;
}

CSE B1 | 3
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code (Do-While):
#include<stdio.h>
int main()
{
int a,b,c,d,e=0;
printf("Enter a Number : ");
scanf("%d",&a);
b=a;
do
{
c=a%10;
a=a/10;
d=c*c*c;
e=e+d;
}while(a>0);
if(b==e)
{
printf("Armstrong");

}
else
{
printf("No");
}
return 0;
}

CSE B1 | 4
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code (For):
#include<stdio.h>

int main()

int a,b,c,d,e=0;

printf("Enter a Number : ");

scanf("%d",&a);

b=a;

for(;a>0;)

c=a%10;

a=a/10;

d=c*c*c;

e=e+d;

printf("\n%d",e);

if(b==e)

printf("\nThe %d is Armstrong");

else

printf("No");

return 0;

Output:

CSE B1 | 5
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.2
AIM:To check whether the Number is Prime or Not.
Algorithm :
1.Start the program.
2.Read an integer num from user.
3.Initialize variable i=1,count=0.
4.If i<=num.
Repeat step 5 and 6.
Else go to step 7.
5.If num%i==0
count++.
6. i++.
7.Now,If count=2.
Print : Prime Number.
8.Else
Print : Composite Number.
9.End the program.

CSE B1 | 6
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Flowchart :

CSE B1 | 7
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code(while) :
#include<stdio.h>
int main()
{
int i=1,a,count=0;
printf("Enter a number : ");
scanf("%d",&a);
while(i<=a)
{
if(a%i==0)
{
count++;
}
i++;
}
if(count==2)
{
printf("\nPrime");

}
else
{
printf("\Composite");

}
return 0;
}

CSE B1 | 8
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code(Do-while) :
#include<stdio.h>
int main()
{
int i=1,a,count=0;
printf("Enter a number : ");
scanf("%d",&a);
do
{
if(a%i==0)
{
count++;
}
i++;
}while(i<=a);
if(count==2)
{
printf("\nPrime");

}
else
{
printf("\Composite");

}
return 0;
}

CSE B1 | 9
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( For) :
#include<stdio.h>
int main()
{
int i=1,a,count=0;
printf("Enter a number : ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==2)
{
printf("\n %d is Prime number",a);
}
else
{
printf("\n %d is composite number",a);
}
return 0;
}

Output:

CSE B1 | 10
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.3
AIM: Print the Fibonacci series.
Algorithm :

1.Start
2. Declare variables:
• i (to store the first Fibonacci number)
• j (to store the next Fibonacci number)
• k (to store the second Fibonacci number)
• n (to store the user-defined limit)
3.Output: Prompt the user to enter the limit for the Fibonacci series.
4.Input: Read the limit n.
5.Initialize:
• Set i to 0 (the first Fibonacci number)
•Set k to 1 (the second Fibonacci number)
6.Output: Print the first two Fibonacci numbers (0 and 1).
7.Loop:
• While j is less than n:
1. Calculate j as the sum of i and k (i.e., j = k + i).
2. Print j.
3. Update i to the value of k.
4. Update k to the value of j.
8.End Loop
9.End

Flowchart :
CSE B1 | 11
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

CSE B1 | 12
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code(while) :
#include<stdio.h>
int main()
{
int a,b,c,i,z,sum=0;
printf("Enter the 1st no. :");
scanf("%d",&a);
printf("\nEnter the 2nd no. :");
scanf("%d",&b);
printf("Enter the limit : ");
scanf("%d",&c);
i=0;
while(i<c)
{
printf("%d ",a);
sum=a+b;
a=b;
b=sum;
i++;
}
return 0;
}

CSE B1 | 13
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( do-while) :
#include<stdio.h>
int main()
{
int a,b,c,i,sum=0;
printf("Enter the 1st no. :");
scanf("%d",&a);
printf("\nEnter the 2nd no. :");
scanf("%d",&b);
printf("Enter the limit : ");
scanf("%d",&c);
i=0;
do
{
printf("%d ",a);
sum=a+b;
a=b;
b=sum;
i++;
}
while(i<c);
return 0;
}

CSE B1 | 14
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( for) :
#include<stdio.h>
int main()
{
int a,b,c,i,z,sum=0;
printf("Enter the 1st no. :");
scanf("%d",&a);
printf("\nEnter the 2nd no. :");
scanf("%d",&b);
printf("Enter the limit : ");
scanf("%d",&c);
for(i=0;i<c;i++)
{
printf("%d ",a);
sum=a+b;
a=b;
b=sum;
}
return 0;
}

Output:

CSE B1 | 15
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.4
AIM:To find the factorial of the inserted number.
Algorithm :
1. Start the Program.
2. Input num,i,fact=1
3. If(i<=num)
Then repeat step 4,5 else step 6
4. fact=fact*i
5. i++.
6. Print : fact.
7. End the program.
Flowchart :

CSE B1 | 16
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( while) :
#include<stdio.h>

int main()

int num,fact=1,i;

printf("Enter a Number :");

scanf("%d",&num);

i=1;

while(num>=i)

fact=fact*i;

i++;

printf("The factorial of %d is %d",num,fact);

return 0;

CSE B1 | 17
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code(do-while) :
#include<stdio.h>

int main()

int num,fact=1,i;

printf("Enter a Number :");

scanf("%d",&num);

i=1;

do{

fact=fact*i;

i++;

} while(num>=i);

printf("The factorial of %d is %d",num,fact);

return 0; }

Code(for) :
#include<stdio.h>
int main()
{
int num,fact=1,i;
printf("Enter a Number :");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d",num,fact);
return 0;
}

CSE B1 | 18
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Output:

CSE B1 | 19
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.5
AIM: Print the Table.
Algorithm :
1. Start the Program.
2. input num,i=1,y.
3. if(i<=10)
Then repeat step 4,5,6 else step 7.
4. y=num*i.
5.Print:num x i =y.
6. i++.
7. End the program.
Flowchart :

CSE B1 | 20
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( while) :
#include<stdio.h>

int main()

int num,b;

printf("Enter a number :");

scanf("%d",&num);

b=1;

while(b<11)

printf("\n%d*%d =%d",num,b,num*b);

b++;

return 0;

Code( do-while) :
CSE B1 | 21
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

#include<stdio.h>
int main()
{
int num,b;
printf("Enter a number :");
scanf("%d",&num);
b=1;
do{
printf("\n%d*%d =%d",num,b,num*b);
b++;
}while(b<11);
return 0;}

Code( for) :
#include<stdio.h>
int main()
{
int num,b;
printf("Enter a number :");
scanf("%d",&num);

for(b=1;b<11;b++)
{
printf("\n%d*%d =%d",num,b,num*b);
}
return 0;
}

Output:
CSE B1 | 22
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.6
AIM: Check whether the number is Palindrome or not.
Algorithm :
1. Start
2. Initialize Variables:
- n: the input number.
- k: to store the original value of n.
- j: initialized to 0 (will hold the reversed number).
- i: a temporary variable to hold the last digit of n.
3. Input:
- Print a message to tell the user to enter a number.
- Read the number into n.
4. Store Original Value:
- Assign the value of n to k (to use later for comparison).

CSE B1 | 23
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

5. Reverse the Number:


- While n is not equal to 0:
- Calculate the last digit of n using i = n % 10.
- Update j to build the reversed number: j = (j * 10) + i.
- Remove the last digit from n: n = n / 10.
6. Check for Palindrome:
- If j (the reversed number) is equal to k (the original number):
- Print "The number is a palindrome".
- Else:
- Print "The number isn't a palindrome".
7. End

CSE B1 | 24
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Flowchart :

CSE B1 | 25
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( while) :
#include<stdio.h>
int main()
{
int a,b,c,d=0,e,f;
printf("Enter a number :");
scanf("%d",&a);
b=a;
while(a>0)
{
c=a%10;
a=a/10;
d=d*10+c;
}
if(d==b)
{
printf("\n%d is Palindrome",a);
}
else
{
Printf(“\n%d is not a Palindrome”,a);
}
return 0;
}

CSE B1 | 26
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( do-while) :
#include<stdio.h>
int main()
{
int a,b,c,d=0,e,f;
printf("Enter a number :");
scanf("%d",&a);
b=a;
do
{
c=a%10;
a=a/10;
d=d*10+c;
}
while(a>0)
if(d==b)
{
printf("\n%d is Palindrome",a);
}
else
{
Printf(“\n%d is not a Palindrome”,a);
}
return 0;
}
CSE B1 | 27
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code(for) :
#include<stdio.h>
int main()
{
int a,b,c,d=0,e,f;
printf("Enter a number :");
scanf("%d",&a);
b=a;
for(;a>0;)
{
c=a%10;
a=a/10;
d=d*10+c;
}
if(d==b)
{
printf("\n%d is Palindrome",a);
}
else
{
Printf(“\n%d is not a Palindrome”,a);
}
return 0;
}

Output:

CSE B1 | 28
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

PRACTICAL NO : 3.7
AIM: To print Odd and Even numbers.
Algorithm :

CSE B1 | 29
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Flowchart :

CSE B1 | 30
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( while) :
#include<stdio.h>
int main()
{
int i;
i=1;
printf("\nodd numbers");
while(i<=20)
{
if(i%2!=0)
{
printf(" %d,",i);
}
i++;
}
printf("\nEven numbers");
i=1;
while(i<=20);
{
if(i%2==0)
{
printf(" %d,",i);
}
i++;
}
return 0;
}

CSE B1 | 31
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( do-while) :
#include<stdio.h>
int main()
{
int i;
i=1;
printf("\nodd numbers");
do
{
if(i%2!=0)
{
printf(" %d,",i);
}
i++;
}
while(i<=20);
printf("\nEven numbers");
i=1;
do
{
if(i%2==0)
{
printf(" %d,",i);
}
i++;
}
while(i<=20);
return 0;
}

CSE B1 | 32
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

Code( for) :
#include<stdio.h>
int main()
{
int i;
printf("\nodd numbers");
for(i=1;i<=20;i++)
{
if(i%2!=0)
{
printf(" %d,",i);
}
}
printf("\nEven numbers");
for(i=1;i<=20;i++)
{
if(i%2==0)
{
printf(" %d,",i);
}
}
return 0;
}

Output:

CSE B1 | 33
COMPUTER PROGRAMMING SAMKIT DHAMI (CS 078)

CSE B1 | 34

You might also like