Module 2B
Module 2B
Programming in C
3:0:0
Textbook:
1. Schildt, Herbert." C the complete reference",4thEdition, Mc Graw Hill.
Mark>=75 Distinction
60 <= marks < 75 A grade
50 <= marks < 60 B grade
40 <= marks <50 C grade
else failed
Q4. Simulation of simple calculator.
Q5. Write a program to enter a character and then determine whether
it is a vowel or not
Q6. Largest among 3 numbers using && operator
case ‘A’:
printf(“Excellent”);
Output:
case ‘B’: Enter the grade : c
printf(“Good); Invalid Grade
case ‘C’:
Enter the grade : O
printf(“Fair);
Outstanding
break; Excellent
case ‘F: Good
printf(“Fail); Fair
break;
default:
Q1 : Write a program to read number between 1 to 7 and display the day
corresponding to the number.
#include <stdio.h> case 4:
int main() printf("Thursday");
break;
{
case 5:
int week;
printf("Friday");
printf("Enter week number(1-7): ");
break;
scanf("%d", &week); case 6:
switch(week) printf("Saturday");
{ break;
case 1: case 7:
printf("Monday"); printf("Sunday");
break; break;
default:
case 2:
printf("Invalid input.");
printf("Tuesday");
}
break; return 0;
case 3: }
Q2. Write a program to check the character is vowel or note
#include <stdio.h> case 'A':
int main() printf("%c is a vowel",ch);
{
break;
char ch;
case 'E':
printf("Enter any Alphabet\n");
scanf("%c",&ch); printf("%c is a vowel",ch);
switch(ch) break;
{ case 'I':
case 'a': printf("%c is a vowel",ch);
printf("%c is a vowel",ch);
break;
break;
case 'O':
case 'e':
printf("%c is a vowel",ch); printf("%c is a vowel",ch);
break; break;
case 'i': case 'U':
printf("%c is a vowel",ch); printf("%c is a vowel",ch);
break;
break;
case 'o':
printf("%c is a vowel",ch);
default:
break; printf("%c is a consonant",ch);
case 'u': break;
printf("%c is a vowel",ch); }
break; return 0; }
#include <stdio.h>
int main() case 'o':
{ case ‘O’
char ch; printf("%c is a vowel",ch);
printf("Enter any Alphabet\n"); break;
scanf("%c",&ch); case 'u':
switch(ch){ case ‘U’
case 'a': printf("%c is a vowel",ch);
case 'A': break;
printf("%c is a vowel",ch); default:
break; printf("%c is a consonant",ch);
case 'e': break;
case 'E': }
printf("%c is a vowel",ch); return 0;
break; }
case 'i':
case ‘I’
printf("%c is a vowel",ch);
break;
Q3. Write a program that accepts a number from 1 to 10. Print whether the
number is even or odd using a switch case construct.
#include <stdio.h>
case 2:
int main() case 4:
{ case 6:
int num; case 8:
printf("Enter a number"); case 10:
scanf("%d",&num); printf("%d is a even number",num);
switch(num){ break;
default:
case 1:
case 3: printf(“Invalid");
case 5: break;
case 7: }
case 9: return 0;
printf("%d is an odd number",num); }
break;
Simulation of a Simple Calculator.
#include<stdio.h> case'/':
int main() if(b==0)
{ {
int a,b,ans; printf("Divide by Zero error");
char op; }
scanf("%d\n%c\n%d",&a,&op,&b); else
switch(op) {
{ ans=a/b;
case '+': printf("The quotient is %d",ans);
ans=a+b; }
printf("The sum is %d",ans); break;
break; case'%':
case'-': ans=a%b;
ans=a-b; printf("The remainder is %d",ans);
printf("The difference is %d",ans); break;
break; default:
case'*': printf("Invalid Input");
ans=a*b; break;
printf("The product is %d",ans); }
break; return 0;
}
#include<stdio.h>
int main()
{
Q1. Write program to check whether the number is
int x,choice;
printf(“Enter the number”); zero, positive or negative using switch.
scanf(“%d”,&x);
if(x>0)
{
choice =1;
break;
}
else if(x<0)
{
choice=2;
}
else
{
choice=3;
}
switch(c)
{
case 1:
printf(“%d is +ve”,x);
break;
case 2:
printf(“%d is –ve”,x)
break;
case 3:
printf(“Zero”);
Iterative Statements
• Iterative or Looping statements are used to repeat the execution
of a block of code until the specified condition is met.
• A loop statement allows programmers to execute a statement or
group of statements multiple times without the repetition of
code.
•There are mainly two types of loops in C Programming:
1. Entry Controlled loops: Here the test condition is checked before entering the body
of the [Link] Loop and While Loopis Entry-controlled loops.
2. Exit Controlled loops: Here the test condition is evaluated at the end of the loop
body. The loop body will execute at least once, irrespective of whether the condition is
true or
while loop
• Where statement is either an empty statement, a single
• Syntax: statement, or a block of statements.
statement x; • The condition may be any expression, and true is any nonzero
value.
while (condition)
• The loop iterates while the condition is true.
{
statement block; • When the condition becomes false, program control passes to
the line of code immediately following the loop.
update condition;
} • While loops check the test condition at the top of the loop,
which means that the body of the loop will not execute if the
statement y; condition is false to begin with.
• This feature may eliminate the need to perform a separate
conditional test before the loop.
#include <stdio.h>
int main()
{
int i=0;
while(i<10)
{
printf( "Hello World\n");
i=i+1; //i++;
}
return 0;
}
Q1 : Program to print numbers 1 to 10
#include<stdio.h>
int main(void)
{
int i=1;
while(i<=10)
{
printf("%d\n",i);
i = i+1;
}
return 0;
}
Q2: Write a program to print numbers between m to n
#include < stdio.h >
void main()
{
int m,n;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &m, &n);
printf("Natural numbers between %d and %d are:\n", m, n);
while(m <= n)
{
printf("%d ", m);
m++;
Q3 : print 20 horizontal asterisk (*).
#include <stdio.h>
int main()
{
int i = 1;
while ( i <=20)
{
printf("*");
i += 1
}
}
Q4 : Write a program to calculate the sum of first 10 numbers.
#include<stdio.h>
void main()
{
int i=1, sum = 0;
while(i<=10)
{
sum = sum + i;
i++;
}
printf("\nSum of first 10 Natural Numbers is : %d", sum);
}
Q5 : Write a program to calculate the sum of numbers from m
to n.
#include <stdio.h>
void main()
{
int m,n,sum=0;
printf("Enter the value of m: ");
scanf("%d",&m);
printf("Enter the value of n: ");
scanf("%d",&n);
while(m<=n)
{
sum+=m;
m++;
}
printf("Sum = %d\n",sum);
}
Q6 : Write a program to read the numbers untill -1 is encountered. Also count the –
ve, +ve , and zero entered by the user.
do while
• The do-while loop is similar to a while loop but the only difference is it is exit
controlled loop.
• Here test condition is tested at the end of the body.
• The loop body will execute at least once irrespective of the test condition.
• Test condition is enclosed in parenthesis and followed by a semicolon.
• Statements in the statement bock are enclosed within curly bracket.
• The curly bracket is optional if there is only one statement in the body of the loop.
Syntax
statement x;
do
{
statement block;
}while(condition);
statement y;
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++);
{
printf( “%d“,i);
}
Compiler will not generate any error message
return 0; You will get an unexpected output.
}
3. Multiple initialization can be separated with a comma operator
for(i=1,sum=0;i<=10;i++)
{
sum = sum + i;
}
#include <stdio.h>
void main()
{
int i=0;
for(;i<10;i++)
{
printf( “%d“,i);
}
}
6. If the loop controlling statement is updated within the statement block, then
the third part can be skipped.
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;)
{
printf( “%d“,i);
i++;
}
return 0;
}
7. Multiple statement can be included in the third part by using comma
operator
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i+=2)
{
printf( “%d“,i);
}
return 0;
}
9. If the for loop containing nothing but two semicolons, the loop may
become an infinite loop.
#include<stdio.h>
void main()
{
int i;
for(i=1,sum=0;i<=10;i++)
{
sum = sum + i;
}
printf("\nSum of first 10 Natural Numbers is : %d", sum);
}
Q5 : Write a program to calculate the sum of numbers from m
to n.
#include <stdio.h>
void main()
{
int m,n,sum=0;
printf("Enter the value of m: ");
scanf("%d",&m);
printf("Enter the value of n: ");
scanf("%d",&n);
for(;m<=n;m++)
{
sum+=m;
}
printf("Sum = %d\n",sum);
}
Programs
1. Palindrome of a number using while loop
2. Factorial of a number using while loop
Nested Loop
• When a loop written inside the body of another loop then, it is known
as nesting of loop. (Loops that can be placed inside other loop).
• Any type of loop can be nested in any type such as while, do while,
for.
Programs
• Write a program to print numbers from 1 to 10.
• Write a program to print the sum of N natural numbers.
• Write a C program to print the sum of all even numbers between 1 and 50.
• Write a program to print the multiplication table of a given number.
• Write a program to print the factorial of a given number.
• Write a program to reverse a number.
• Write a program to check whether a number is a palindrome.
• Write a program to check whether a number is a prime number.
• Write a program to count the digits in a number.
• Write a program to find the sum of digits of a number
• Print a multiplication table(1-10) using nested for loop
• Write a program to accept numbers until the user enters 0, and display their sum
using do-while loop.
Jump Statements
C has four statements that perform an unconditional branch:
1. return statement
2. goto statement
3. break statement
4. continue statement
5. exit() function
return statement
• The return statement is used to return from a #include <stdio.h>
function.
// Function that adds two integers and returns sum
• It is categorized as a jump statement because it causes int add(int a, int b)
execution to return (jump back) to the point at which
{
the call to the function was made. int sum = a + b;
• A return may or may not have a value associated with return sum; // Returns the calculated sum
it. }
• The label must be in the same function as the goto goto even; // jump
to even
that uses it— you cannot jump between functions.
}
• It transfers program control from els{
the goto statement to the code block associated goto odd; // Jump to odd
with the designated label. }
• The general form of the goto statement is even:
return 0;
Amruthasree V M, Assistant Professor, NIE 81
}
The exit( ) Function
• The exit() function is a standard library function used to immediately
terminate the calling process (the program).
• The general form of the exit( ) function is
void exit(int return_code);
• The exit( ) function requires the header <stdlib.h>.
#include <stdio.h>
#include <stdlib.h> // Required for the exit() function
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number < 0)
{
printf("You entered a negative number. Exiting program.\n");
exit(1); // Exit the program with a non-zero status, indicating an
error or specific condition
}
printf("You entered a non-negative number: %d\n", number);
return 0; // Indicate successful program execution
}
#include <stdio.h>
int main()
{
printf("Numbers from 1 to 20 (excluding even numbers):\n");
for (int i = 1; i <= 20; i++)
{
if (i % 2 != 0)
{ // Check if the number is odd
printf("%d\n", i);
}
}
return 0;
}