4
4
PRACTICAL NO. : 4
A
AB
ABC
ABCD
#include<stdio.h>
void main()
{
int i,j,n;
printf("enter the number of lines\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%c",(char)(j+64));
}
}
printf("\n");
}
Theoretical
Principles used : In
this initiate I, j, n .Ask
user for the number
of lines he wants the
pattern then start 2
for loops. Outer loop
for number of line
and inner for
alphabets.
1
01
101
0101
#include<stdio.h>
int main()
{
int i,j,rows;
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==1&&j%2==1)
{
printf("1");
}
else if(i%2==0&&j%2==0)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
return 0;
}
Theoretical Principles used : In this ask user for number of rows he want then make 2
loops outer loop is for row pattern and inner loop for number pattern .Make 2
condition that the one divided by 2 print 0 and not divided print 1.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=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;
}
Theoretical Principles used : In this outer loop is for number of lines and there are
two inner loops one for spaces and one for number patterns.
b.) To check whether the input number is an Armstrong
number .
#include<stdio.h>
int main()
int n,r,sum=0,temp;
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
sum=sum+(r*r*r);
n=n/10;
if(temp==sum)
else
return 0;}
Theoretical Principles used : It is a number such that the sum of its digit raised to the third power is
equal to the number itself. Ask user for the number then assign a temporary variable then take its modular
and store it in a variable named r then use the formula. If temporary variable is equal to sum then its
Armstrong otherwise it is not.
c.) To check whether input number is prime or not.
#include<stdio.h>
int main()
int n,i,c,d;
i=2;
c=0;
scanf("%d",&n);
for(i=2;i<n;i++)
d=n%i;
if(d==0)
c=1;
break;
if(c==0)
{
printf("prime number %d",n);
else
return 0;
Theoretical
Principles used : In
this a for loop is
iterated from i=2 to
i<n/2. If n is perfectly
divisible by I ,then its
not a prime number. In
this case, flag is set to 1
and loop is terminated
by break statement .
D.) To check whether the entered number is Palindrome.
#include<stdio.h>
int main()
{
int n,rem,sum,exe;
sum=0;
rem=0;
printf("Palindrome Number\n");
printf("enter the number");
scanf("%d",&n);
exe=n;
while(n!=0)
{
rem=n%10;
sum=sum*10+rem;
n=n/10;
}
printf("%d\n",sum);
if(exe==sum)
{
printf("its Palindrome");
}
else
{
printf("its not a palindrome");
}
return 0;
}
Theoretical Principles used : Palindrome number is a number which is same if you read it
either way. So initialize sum and remainder as 0 then ask user to enter a number and then in
while loop check whether the number is not equal to 0. Take its remainder and multiply the
sum by 10 and add remainder. Divide the number by 10 , if exe is equal to sum then the
number is palindrome otherwise it is not.
e.) Enhance the number guessing game developed earlier.
The program should now display more appropriate
message (Greater, Smaller or Correct). It should allow
maximum 5 attempts from the user and still if the user
cannot guess the number correctly, it should display
“sorry”.
#include<stdio.h>
#include<math.h>
int main()
float a,b,i=0,j=0;
int x;
a=rand();
x=a;
while(x!=0)
{
x=x/10;
j++;
while(i<6)
scanf("%f",&b);
if(a==b)
printf("Correct Number\n");
break;
else if(a>b)
printf("Not Correct\n");
else if(a<b)
{
printf("Not Correct\n");
i++;
return 0;}