0% found this document useful (0 votes)
5 views23 pages

WINSEM2024-25_Material-I (1)

The document provides an overview of various control statements in C programming, including break, continue, and goto statements, along with their syntax and examples. It also explains the for loop structure and its working mechanism. Additionally, it includes multiple sample programs demonstrating different programming tasks such as checking for prime numbers, displaying Fibonacci numbers, and generating patterns.

Uploaded by

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

WINSEM2024-25_Material-I (1)

The document provides an overview of various control statements in C programming, including break, continue, and goto statements, along with their syntax and examples. It also explains the for loop structure and its working mechanism. Additionally, it includes multiple sample programs demonstrating different programming tasks such as checking for prime numbers, displaying Fibonacci numbers, and generating patterns.

Uploaded by

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

Break statement

• The break statement ends the loop immediately when it is encountered.


Its syntax is: #include <stdio.h>

int main()
break; {
int i = 0;

while (i < 10)


{
if (i == 4)
{
break;
}
printf("%d\n", i);
i++;
} return 0;
}
Continue statement
• The continue statement skips the current iteration of the loop and
continues with the next iteration.
#includeIts<stdio.h>
syntax is:
continue; int main()
{
int i = 0;
while (i < 10)
{
if (i == 4)
{
i++;
continue;
}
printf("%d\n", i);
i++;
}
return 0;
}
goto statement
• The C goto statement is a jump statement which is sometimes also
referred to as an unconditional jump statement. The goto statement can
be used to jump from anywhere to anywhere within a function.

• The goto statement allows us to transfer control of the program to the


specified label. Syntax of goto Statement
goto label;
... .. ...
... .. ...
label:
statement;
• The label is an identifier. When the goto statement is encountered, the
control of the program jumps to label: and starts executing the code.
goto statement
include <stdio.h>

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:

for(initialization; check/test expression; updation)


{
// body consisting of multiple statements
}
For Loop
• The working of for loop is mentioned below:
• Step 1: Initialization is the basic step of for loop this step occurs only
once during the start of the loop. During Initialization, variables are
declared, or already existing variables are assigned some value.
• Step 2: During the Second Step condition statements are checked and
only if the condition is the satisfied loop we can further process
otherwise loop is broken.
• Step 3: All the statements inside the loop are executed.
• Step 4: Updating the values of variables has been done as defined in
the loop.
• Continue to Step 2 till the loop breaks.
Program 8
• Write a program to find whether the given number is PRIME or NOT.
Ex : Input : n = 23
Output : PRIME NUMBER
#include <stdio.h>
Program 8
int main()
{
int num1, count=0,i;
printf("Enter a number : ");
scanf("%d", &num1);
for(i=1;i<=num1;i++)
{
if(num1%i==0)
{
count =count+1;
if(count>2)
{
printf("%d is not a PRIME number ", num1);
break;
}
}
}
if(count<=2)
{
printf("%d is a PRIME number ", num1);
}

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

You might also like