CP Practical 4
CP Practical 4
Practical 4(a)
Aim: print
A
AB
ABC
ABCD
Method followed:
#include <stdio.h>
int main()
{
int i,j,n=4;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%c ",'A'+j-1);
}
printf("\n");
}
return 0;
}
Output:
Pracctical4(b)
Aim: print
1
01
101
0101
Method followed:
#include <stdio.h>
int main()
{ int n,i,j;
printf("enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",(j+i-1)%2);
}
printf("\n");
}
return 0;
}
Output:
Practical 4(c)
Aim: print
1
121
12321
1234321
Method followed:
#include <stdio.h>
int main()
{ int i,j,n=4;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
Practical 4(d)
Aim : to check whether the input number is an Armstrong number.
Method followed:
int main()
{ int n,count=0,cnt,rem,result=0,mul=1,q;
printf("enter the number :");
scanf("%d",&n);
q=n;
while(q!=0)
{
q=q/10;
count++;
}
cnt=count;
q=n;
while(q!=0)
{
rem=q%10;
while(cnt!=0)
{
mul=mul*rem;
cnt--;
}
result=result+mul;
cnt=count;
q=q/10;
mul=1;
}
if(result == n)
{
printf("armstrong");
}
else if(result != n)
{
printf("not armstrong");
}
return 0;
}
Output:
Practical 4(e)
Aim: To check whether the entered number is prime.
Method followed:
#include <stdio.h>
int main()
{ int n,i,a=1;
printf("Enter the number:");
scanf("%d",&n);
for(i=2;i*i<n;i++)
{
if(n%i==0)
{
a=0;
}
}
if(a==1)
{
printf("the number is prime");
}
else if(a==0)
{
printf("the number is not prime");
}
return 0;
}
Output:
Practical 4(f)
Aim: To check whether the entered number is palindrome.
Method followed:
#include <stdio.h>
int main()
{ int n,r,sum=0,p;
printf("enter the number :");
scanf("%d",&n);
p=n;
while(p!=0)
{
r=p%10;
sum=sum*10+r;
p=p/10;
}
if(sum==n)
{
printf("palindroma");
}
else
{
printf("not palindroma");
}
return 0;
}
Output:
Practical 4(g)
Aim: To check whether the entered number is palindrome.
Method followed:
#include <stdio.h>
int main()
{
int n,b=18,count=0;
printf("you have maximum 5 attemps\n");
do{
scanf("%d",&n);
if(n==b)
{
printf("the number is correct\n");
printf("you won the game\n");
break;
}
else if(n!=b)
{
if(n>b)
{
printf("entered number greater than guessing number\n");
}
else
{
printf("entered number smaller than guessing number\n");
}
}
count=count+1;
}
while(count<5);
if(n!=b){
printf("sorry");
}
return 0;
}
Output: