C Programming Notes
C Programming Notes
“Hello” 5
Output
printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //statements
printf("Hello\n"); Hello }
printf("Hello\n"); Hello
if v/s while
False False
condition condition
True True
… …
Looping or Iterative Statements in C
Looping Statements are
While loop
While Loop
while is an entry controlled loop
Statements inside the body of while are repeatedly executed till the condition is
true
while is keyword
Syntax
while(condition)
{
// Body of the while
// true part
}
Example print sum of n natural no
#include <stdio.h>
int main()
{
int i=1,n,sum=0;
printf("Enter no");
scanf("%d", &n);
while (i <= n)
{
sum = sum+i;
i++;
}
printf("%d ",sum);
return 0;
}
WAP to print 1 to n(while loop)
Program Output
1 #include <stdio.h> Enter n:10
2 void main() 1
3 { 2
4 int i,n; 3
5 i=1; 4
6 printf("Enter n:"); 5
7 scanf("%d",&n); 6
8 while(i<=n) 7
9 { 8
10 printf("%d\n",i); 9
11 i=i+1; 10
12 }
13 }
WAP to print multiplication table(while loop)
Program Output
1 #include<stdio.h> Enter n for multiplication table:5
2 void main() 5 * 1 = 5
3 { 5 * 2 = 10
5 * 3 = 15
4 int i=1,n;
5 * 4 = 20
5 printf("Enter n for multiplication table:"); 5 * 5 = 25
6 scanf("%d",&n); 5 * 6 = 30
7 while(i<=10) 5 * 7 = 35
8 { 5 * 8 = 40
9 printf("%d * %d = %d\n",n,i,n*i); 5 * 9 = 45
10 i=i+1; 5 * 10 = 50
11 }
12 }
WAP to Sum of n natural numbers entered by user(while loop)
Program Output
1 #include<stdio.h> Enter a number=10
2 void main() Enter a number=20
3 { Enter a number=30
Enter a number=40
4 int sum=0, i=1,n;
Enter a number=50
5 printf("Enter a number="); Sum is=150
6 scanf("%d",&n);
7
8 while(i<=n)
9 {
10 sum=sum+n;
11 i=i+1;
12 }
13 printf("Sum is=%d",sum);
}
WAP to find factors of a number(while loop)
Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i=1,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 while(i<=n)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 i=i+1;
12 }
13 }
WAP to print reverse a number(while loop)
Program Output
1 #include <stdio.h> Enter a number=1234
2 void main() 4321
3 {
4 int n;
5 printf("Enter a number=");
6 scanf("%d",&n);
7 while(n!=0)
8 {
9 printf("%d",n%10);
10 n=n/10;
11 }
12 }
WAP to check given number is perfect or not(while loop)
1 void main(){
2 int i=1,n,sum=0;
3 printf("Enter a number:"); Output
4 scanf("%d",&n); Enter a number:6
5 while(i<n) 1+2+3=6
6 is a perfect number
6 {
7 if(n%i==0)
Output
8 {
9 printf("%d+",i); Enter a number:8
1+2+4+=7
10 sum=sum+i;
8 is not a perfect number
11 }
12 i=i+1;
Output
13 }
14 printf("=%d",sum); Enter a number:496
1+2+4+8+16+31+62+124+248+=496
15 if(sum==n) 496 is a perfect number
16 printf("\n%d is a perfect number",n);
17 else
18 printf("\n%d is not a perfect number",n);
19 }
for loop
for Loop
for is an entry controlled loop
Statements inside the body of for are repeatedly executed till the condition is true
for is keyword
Syntax
for (initialization; condition; updateStatement)
{
// statements
}
Program Output
1 #include<stdio.h> Enter a number:5
2 void main() 1
3 { 2
4 int i,n; 3
5 printf("Enter a number:"); 4
6 scanf("%d",&n); 5
7 for(i=1;i<=n;i++)
8 {
9 printf("%d\n",i);
10 }
11 }
WAP to find factors of a number (for loop)
Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 for(i=1;i<=n;i++)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 }
12 }
WAP to check given number is perfect or not(for loop)
1 void main(){
2 int i,n,sum=0;
3 printf("Enter a number:"); Output
4 scanf("%d",&n); Enter a number:6
5 for(i=1;i<n;i++) 1+2+3=6
6 is a perfect number
6 {
7 if(n%i==0)
Output
8 {
9 printf("%d+",i); Enter a number:8
1+2+4+=7
10 sum=sum+i;
8 is not a perfect number
11 }
12 }
Output
13 printf("=%d",sum);
14 if(sum==n) Enter a number:496
1+2+4+8+16+31+62+124+248+=496
15 printf("\n%d is a perfect number",n); 496 is a perfect number
16 else
17 printf("\n%d is not a perfect number",n);
18 }
do while loop
do while Loop
do while is an exit controlled loop.
Statements inside the body of do while are repeatedly executed till the condition
is true.
Do and while are keywords. Syntax
do
{
// statement
}
while (condition);
Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(i%2!=0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }
WAP to find factors of a number(do while loop)
Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(n%i==0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }
WAP to print reverse a number(do while loop)
Program Output
1 void main() Enter a number=1234
2 { 4321
3 int n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 printf("%d",n%10);
9 n=n/10;
10 }
11 while(n!=0);
12 }
goto statement
WAP to print Odd numbers between 1 to n(goto)
Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(i%2!=0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }
WAP to find factors of a number(goto)
Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(n%i==0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }
Types of loops
Entry Control Loop Entry Control Loop Exit Control Loop Virtual Loop
int i=1; int i; int i=1; int i=1;
while(i<=10) for(i=1;i<=10;i++) do labelprint:
{ { { printf("%d",i++);
printf("%d",i++); printf("%d",i); printf("%d",i++); if(i<=10)
} } } goto labelprint;
while(i<=10);
False
Loop Body Label Statement
condition
True True
condition condition
Loop Body
False True False
goto
Pattern
Always detect pattern in pattern
Pattern
There are important points to note in pattern
1. Determine, how many rows?
2. Determine, how many numbers/characters/columns in a row?
3. Determine, Increment/Decrement among the number of rows.
4. Determine, starting in each row
1 1 1 *
11 12 23 * *
111 123 456 * * *
1111 1234 78910 * * * *
11111 12345 * * *
* *
*
WAP to print given pattern (nested loop)
* Program
** 1 void main()
*** 2 {
**** 3 int i,j;
***** 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of characters 7 {
Row-1: * 8 printf("*");
Row-2: ** 9 }
Row-3: *** 10 printf("\n");
Row-4: **** 11 }
Row-5: ***** 12 }
Starting: *
WAP to print given pattern (nested loop)
1 Program
12 1 void main()
123 2 {
1234 3 int i,j;
12345 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of values 7 {
Row-1: 1 8 printf("%d",j);
Row-2: 12 9 }
Row-3: 123 10 printf("\n");
Row-4: 1234 11 }
Row-5: 12345 12 }
Starting: 1
WAP to print given pattern (nested loop)
5 Program
54 1 void main()
543 2 {
5432 3 int i,j;
54321 4 for(i=5;i>0;i--)
5 {
No. of rows: 5
6 for(j=5; j>=i ; j--)
No. of values 7 {
Row-1: 5 8 printf("%d",j);
Row-2: 54 9 }
Row-3: 543 10 printf("\n");
Row-4: 5432 11 }
Row-5: 54321 12 }
Thank you