- To print the word "APPLE"
n
times.
- Begin the program.
- Declare a variable
n
of type int. - Input the value of
n
. - Use a for loop to iterate
n
times:- Print "APPLE" followed by a newline.
- End the program.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("APPLE\n");
}
return 0;
}
Thus the required program is written and executed successfully.
To create a program that prints a sequence of numbers from 1 to n
, separated by colons.
- Begin the program.
- Declare a variable
n
of type int to store the upper limit of the number sequence. - Prompt the user to input the value of
n
. - Read the input value for
n
usingscanf()
function. - Use a for loop with loop variable
i
initialized to 1, terminating conditioni <= n
, and incrementingi
by 1 in each iteration:- Inside the loop, print the value of
i
. - If
i
is less thann
, print a colon followed by a space.
- Inside the loop, print the value of
- End the program.
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
printf("%d",i);
if(i<n)
printf(" : ");
}
return 0;
}
Thus the required program is written and executed successfully.
To develop a program that generates a numerical pattern.
- Begin the program.
- Declare variables
i
,j
, andn
of type int. - Assign the value 10 to variable
n
. - Use a nested for loop structure to iterate over rows and columns:
- Outer loop (
i
loop) iterates from 1 ton
. - Inner loop (
j
loop) iterates from 1 toi
, printing values incrementally. - After the inner loop, use another loop to print values in descending order, iterating from
i-1
to 1. - Print a newline after each row.
- Outer loop (
- End the program.
#include<stdio.h>
int main(){
int i,j,n;
n=10;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
}
Thus the required program is written and executed successfully.
To create a program that prints a diamond pattern using the character '%' as the pattern element.
- Begin the program.
- Declare variables
n
,i
,j
,s
, andl
of type int, wheren
is assigned the value 3. - Declare a character variable
x
and assign it the value '%'. - Use a for loop with loop variable
i
initialized to-n
, terminating conditioni <= n
, and incrementingi
by 1 in each iteration:- Inside the loop:
- Print leading spaces.
- Determine the value of
l
based on the absolute value ofi
. - Print spaces to adjust the position of the pattern element.
- Print the '%' character for each row.
- Print a newline character.
- Inside the loop:
- End the program.
#include<stdio.h>
int main(){
int n=3,i,j,s,l;
char x='%';
for(i=-n;i<=n;i++){
printf(" ");
if(i<0)
l=-i;
else
l=i;
for(s=0;s<n-l;s++){
printf(" ");
}
for(j=0;j<n-l+1;j++){
printf("%c ",x);
}
printf("\n");
}
}
Thus the required program is written and executed successfully.
To create a program that prints the Fibonacci series up to the a
th term.
- Begin the program.
- Declare a function
fibo(int a)
to calculate and print the Fibonacci series up to thea
th term.- Initialize variables
x
andy
to 0 and 1 respectively. - Use a for loop with loop variable
i
initialized to 0, terminating conditioni < a
, and incrementingi
by 1 in each iteration:- Print the value of
x
. - Calculate the next Fibonacci number
z
by addingx
andy
. - Update the values of
x
andy
for the next iteration.
- Print the value of
- Initialize variables
- In the
main()
function:- Declare a variable
a
of type int to store the term up to which Fibonacci series is to be printed. - Prompt the user to input the value of
a
. - Read the input value for
a
usingscanf()
function. - Call the
fibo()
function with argumenta
.
- Declare a variable
- End the program.
#include <stdio.h>
void fibo(int a)
{
int x=0,y=1,z;
for(int i=0;i<a;i++)
{
printf("%d ",x);
z=x+y;
x=y;
y=z;
}
}
int main()
{
int a;
scanf("%d",&a);
fibo(a);
}
Thus the required program is written and executed successfully.
To create a program that generates an addition series starting from 100 up to 500, with each term incremented by the input value a
.
- Begin the program.
- Declare a function
sub()
to generate the addition series.- Declare variables
a
andc
of type int. - Prompt the user to input the value of
a
. - Read the input value for
a
usingscanf()
function. - Use a for loop with loop variable
i
initialized to 100, terminating conditioni <= 500
, and incrementingi
by 100 in each iteration:- Calculate the value of
c
as the sum ofi
anda
. - Print the value of
c
.
- Calculate the value of
- Declare variables
- In the
main()
function:- Call the
sub()
function to generate the addition series.
- Call the
- End the program.
#include<stdio.h>
void sub()
{
int a,c;
scanf("%d",&a);
for(int i=100;i<=500;i+=100)
{
c=i+a;
printf("%d ",c);
}
}
int main()
{
sub();
}
Thus the required program is written and executed successfully.
To create a program that checks whether the given number is a palindrome or not.
- Begin the program.
- Declare variables
n
anda
of type int to store the input number and its copy. - Prompt the user to input a number.
- Read the input value for
n
usingscanf()
function. - Assign the value of
n
toa
. - Declare variables
r
anddigit
of type int to store the reversed number and individual digits. - Use a do-while loop to reverse the number
n
:- Inside the loop:
- Extract the last digit of
n
using the modulo operator and store it indigit
. - Update the value of
r
by multiplying it by 10 and adding the extracted digit. - Update the value of
n
by removing the last digit.
- Extract the last digit of
- Continue the loop until
n
becomes 0.
- Inside the loop:
- Check if the original number
a
is equal to the reversed numberr
:- If true, print "Palindrome Number".
- If false, print "Not a Palindrome Number".
- End the program.
#include<stdio.h>
int main(){
int n,a;
scanf("%d",&n);
a=n;
int r=0,digit;
do{
digit=n%10;
r=r*10+digit;
n/=10;
}
while(n>0);
if(a==r){
printf("Palindrome Number");
}
else{
printf("Not a Palindrome Number");
}
}
Thus the required program is written and executed successfully.
To create a program that prints a pattern.
- Begin the program.
- Use nested for loops to generate the desired pattern:
- Outer loop (
i
loop) iterates from 1 to 5. - Inner loop (
j
loop) prints numbers from 1 to the current value ofi
. - Second inner loop (
k
loop) prints the character 'A' in a decreasing manner from 4 toj-1
. - Third inner loop (
l
loop) prints numbers fromi
to 1 in a decreasing manner. - Print a newline character after each iteration of the outer loop.
- Outer loop (
- End the program.
#include<stdio.h>
int main()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(k=4;k>=j-1;k--)
{
printf("A");
}
for(l=i;l>=1;l--)
{
printf("%d",l);
}
printf("\n");
}
}
Thus the required program is written and executed successfully.