WINSEM2024-25_Material-I (1)
WINSEM2024-25_Material-I (1)
int main()
break; {
int i = 0;
int main()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
return 0;
}
For Loop
• The for loop in C Language provides a functionality/feature to repeat a
set of statements a defined number of times. The for loop is in itself a
form of an entry-controlled loop.The syntax of the for loop is:
return 0;
}
Program 9
• Write a program to display the staircase model based on the number of
rows entered by the user.
Ex : Input : n = 5 #include <stdio.h>
int main()
Output : {
* int num1, j,i;
char c='*';
** printf("Enter a number : ");
*** scanf("%d", &num1);
for(i=1;i<=num1;i++)
**** {
***** for(j=1;j<=i;j++)
{
printf("%c ", c);
}
printf("\n");
}
return 0;
}
Program 10
• Write a program to display the staircase model based on the number of
rows entered by the user.
Ex : Input : n = 5 #include <stdio.h>
int main()
Output : {
***** int num1, j,i;
char c='*';
**** printf("Enter a number : ");
*** scanf("%d", &num1);
for(i=num1;i>=1;i--)
** {
* for(j=1;j<=i;j++)
{
printf("%c ", c);
}
printf("\n");
}
return 0;
}
Sample Program 11
• Write a program to display first N PRIME numbers based on the user
input. Display the numbers in the same line.
Ex : Input : n = 6
Output : 2 3 5 7 11 13
#include <stdio.h>
int main()
{
Sample Program 11
int num1,i,j=1, count,num2=2;
• Writescanf("%d",
a program&num1);
printf("Enter atonumber
display: ");
first N PRIME numbers based on the user
input. Display the numbers in the same line.
while(j<=num1)
Ex : Input { :count=0;
n=6
Output : for(i=1;i<=num2;i++)
2 3 5 7 11 13
{
if(num2%i==0)
{
count =count+1;
}
}
num2++;
if(count==2)
{
printf("%d ", num2-1);
j++;
}
}
return 0;
}
Sample Program 12
• Write a program to display first N Fibonacii numbers based on the user
input. Display the numbers in the same line.
Ex : Input : n = 7 #include <stdio.h>
int main()
Output : 0 1 1 2 3 5 8 {
int n, num1=0,num2=1, sum=0,i;
printf("Enter a number : ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
printf("%d ", num1);
sum=num1+num2;
num1=num2;
num2=sum;
}
return 0;
}
Sample Program 13
• Write a program to find the number of digits for the number entered by
the user.
Ex : Input : 235 #include <stdio.h>
int main()
Output : 3 {
int num1, i;
printf("Enter a number : ");
scanf("%d", &num1);
for(i=1;num1>9;i=i+1)
{
num1=num1/10;
}
printf("%d ", i);
return 0;
}
Sample Program 14
• Write a program to print the sum of digits of an integer number
Ex: Input : 12345
Output : Sum is 15 #include <stdio.h>
int main()
{
int num1, i,num2,sum=0;
printf("Enter a number : ");
scanf("%d", &num1);
while(num1>0)
{
num2=num1%10;
num1=num1/10;
sum=sum+num2;
}
printf("%d ", sum);
return 0;
}
Sample Program 15
• Write a program to find the given number is a power of 2 or not
Ex: Input : 8
Output : Yes
#include <stdio.h>
#include <math.h>
int main()
Sample Program 15
{
• int num1, count,num2,i;
Write a program to find the given number is a power of
printf("Enter a number : ");
2 or not
Ex: Inputscanf("%d",
:8 &num1);
Output : Yeswhile(num2<=num1)
{
num2=pow(2, i);
if(num2==num1)
{
count=1;
}
i++;
}
if(count==1)
{
printf("Yes ");
}
else
{
printf("No");
}
return 0;
Sample Program 16
• Write a program to print the following based on the number of rows
entered by the user.
Ex: Input : n=5 #include <stdio.h>
int main()
Output : {
1 int num1, j,i;
printf("Enter a number : ");
12 scanf("%d", &num1);
123 for(i=1;i<=num1;i++)
{
1234 for(j=1;j<=i;j++)
12345 {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Sample Program 17
• Write a program to print the following based on the number of rows
entered by the user.
Ex: Input : n=5 #include <stdio.h>
int main()
Output : {
54321 int num1, j,i;
printf("Enter a number : ");
4321 scanf("%d", &num1);
321 for(i=num1;i>=1;i--)
{
21 for(j=i;j>=1;j--)
1 {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Sample Program 18
• Write a program to print the floyd’s triangle
Ex: Input : 5
Output : 1 #include <stdio.h>
int main()
23 {
456 int num1, j,i,k=1;
printf("Enter a number : ");
7 8 9 10 scanf("%d", &num1);
11 12 13 14 15 for(i=1;i<=num1;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ", k);
k++;
}
printf("\n");
}
return 0;
}
Sample Program 19
• Write a program to the following pattern based on user input
Ex: Input : 5
Output : * #include <stdio.h>
int main()
*** {
***** int n,i,j,k;
printf("Enter the no. of rows to print:");
******* scanf("%d", &n);
********* for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
printf(" ");
for(j=1;j<=i;j++)
printf(" *");
printf("\n");
}
return 0;
}
Sample Program 20
• Write a program to find the given number is an Armstrong number or
not
Ex: 153 = 13 + 53 + 33 = 1+125+27
1634= 14 + 64 + 34 + 44
The powers of their digits (a finite sequence) are called Armstrong
numbers or plus perfect number and are given by 1, 2, 3, 4, 5, 6, 7,
8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748