0% found this document useful (0 votes)
23 views18 pages

C Programs Unit I

Uploaded by

Nan Kumar
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)
23 views18 pages

C Programs Unit I

Uploaded by

Nan Kumar
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/ 18

Examples using printf()

#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}

#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}

#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;

printf("number1 = %f\n", number1);


printf("number2 = %lf", number2);
return 0;
}

#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}

Examples using Printf() and scanf()


#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
}

#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", num1);
printf("num2 = %lf", num2);

return 0;
}

#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
printf("You entered %c.", chr);
return 0;
}

#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);

// When %c is used, a character is displayed


printf("You entered %c.\n",chr);
// When %d is used, ASCII value is displayed
printf("ASCII value is %d.", chr);
return 0;
}
Output
Enter a character: g
You entered g.
ASCII value is 103.

I/O Multiple Values


#include <stdio.h>
int main()
{
int a;
float b;

printf("Enter integer and then a float: ");

// Taking multiple inputs


scanf("%d%f", &a, &b);

printf("You entered %d and %f", a, b);


return 0;
}

TO add 2 integers
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);


return 0;
}
Program to Compute Quotient and Remainder

#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
return 0;

Program to Find the Size of Variables


#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;

// sizeof evaluates the size of a variable


printf("Size of int: %ld bytes\n", sizeof(intType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));

return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Swapping of two numbers in C
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
}
The output of the program:
Enter two integers
23
45
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23

Simple If Statement
Example 1
// Program to display a number if it is negative

#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

// true if number is less than 0


if (number < 0) {
printf("You entered %d.\n", number);
}

printf("The if statement is easy.");


return 0;
}

EXAMPLE2
#include <stdio.h>
int main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest


if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest


if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest


if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}

If else Statement
EXAMPLE1
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}

EXAMPLE2)
#include <stdio.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
/* This statement will only execute if the
* above condition (age>=18) returns true
*/
printf("You are eligible for voting");
}
else
{
/* This statement will only execute if the
* condition specified in the "if" returns false.
*/
printf("You are not eligible for voting");
}
return 0;
}
Output:
Enter your age:14
You are not eligible for voting

nested if..else
Example1
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}

EXAMPLE2
#include <stdio.h>

void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}

Else if ladder
EXAMPLE1
// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal.


if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}

//checks if number1 is greater than number2.


else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}

//checks if both test expressions are false


else
{
printf("Result: %d < %d",number1, number2);
}

return 0;
}
Output
Enter two integers: 12
23
Result: 12 < 23
EXAMPLE2
#include <stdio.h>

void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}

Switch Statement
Example1
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Enter the values of a and b:");
scanf("%d%d" ,&a,&b);
printf(" \nEnter your choice Addition-1 Subtraction-2: ");
scanf("%d , &d);
switch(d)
{
case 1:
c=a+b;
printf(" Addition=%d", c);
break:
case 2:
c=a-b;
printf(" Substraction=%d" , c);
break:
default:
printf(" Invalid Choice");
break:
}
}
Output
Enter the values of a and b: 10 20
Enter your choice Addition-1 Subtraction-2: 1
Addition=30

EXAMPLE 2
int main()
{
int num;
printf(“enter a number”);
scanf(‘%d”,&num);
switch(num)
{
case 1:
printf(“Sunday”);
break;
case 2:
printf(“Monday”);
break;
case 3:
printf(“Tuesday”);
break;
case 4:
printf(“Wednesday”);
break;
case 5:
printf(“Thursday”);
break;
case 6:
printf(“Friday”);
break;
case 7:
printf(“Saturday”);
break;
default:
printf(“wrong choice”);
break;
}
return 0;
}

GOTO STATEMENT
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}

addition:
printf("%d", sum);

return 0;
}

For statement
#include <stdio.h>
int main ()
{
int intNum;
printf ("\n First 15 natural numbers are: \n");
for (intNum = 0; intNum < 15; intNum++)
{
printf ("%d ", intNum);
}
Return 0;
}
#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}
Enter a positive integer: 10
Sum = 55

WHILE STATEMENT
// Print numbers from 1 to 5

#include <stdio.h>
int main()
{
int i = 1;

while (i <= 5)
{
printf("%d\n", i);
++i;
}

return 0;
}

DO WHILE
#include<stdio.h>

void main()
{
int a, i;
a = 5;
i = 1;
do
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
OUTPUT
5 10 15 20 25 30 35 40 45 50

SUM OF DIGIT
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:"); 123=1+2+3=6
scanf("%d",&n);
while(n>0) 123>0 12>0 1>0 0>0
{
m=n%10; m=123%10=3 m=12%10=2 m=1%10=1
sum=sum+m; sum=3 sum3+2=5 sum=5+1=6
n=n/10; n=12 n=12/10=1 n=1/10=0
}
printf("Sum is=%d",sum);
return 0;
}

PREPROCESSOR DIRECTIVES
PREDEFINED MACRO IN C
EXAMPLE 1
#include <stdio.h>
int main()
{
printf("Line number is: %d\n",__LINE__);
return 0;
}

EXAMPLE 2
#include <stdio.h>
int main()
{
printf("File name of this Program is: %s\n",__FILE__);
return 0;
}

EXAMPLE 3

#include <stdio.h>
int main()
{
printf("Program Compilation Date: %s\n",__DATE__);
return 0;
}

EXAMPLE 4
#include <stdio.h>
int main()
{
printf("Compiler Standard Number: %d\n",
__STDC__);
return 0;
}

Conditional Compilation
EXAMPLE 1

#include <stdio.h>
#include <conio.h>
#define NUMBER 1
void main()
{
#if NUMBER==0
printf("Value of Number is: %d",NUMBER);
#else
print("Value of Number is non-zero");
#endif
getch();
}
EXAMPLE 2

#include <stdio.h>
int main()
{
#define COMPUTER "An amazing device"

#ifdef COMPUTER
printf(COMPUTER);
#endif

return 0;
}

EXAMPLE 3
#include<stdio.h>
void main()
{
#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif

printf("MIN number : %d",MIN);


}

EXAMPLE 4
#include <stdio.h>
#define A 10
#define B 40
int main()
{
#if A==B
printf("Hello");
#elif A>B
printf("World");
#else
printf("CodingFox");
#endif
return 0;
}
EXAMPLE 5
#include <stdio.h>
#define MAX
int main()
{
#ifdef MAX
printf("Hello");
#else
printf("World");
#endif
printf("\n");
#ifndef MIN
printf("Fox");
#else
printf("Duck");
#endif
return 0;
}

EXAMPLE 6
#include<stdio.h>
#define TEMP 10

#ifdef TEMP
#undef TEMP
#define TEMP 75
#else
#define TEMP 100
#endif

int main()
{
printf("%d",TEMP);
return 0;
}

EXAMPLE 7
#include <stdio.h>

// Define PI

#define PI 3.14
int main()

printf("Value of PI: %f\n", PI);

// Undefine PI

#ifdef PI

#undef PI

#endif

// Redefine value of PI

#define PI 3.14159

printf("Value of PI after redefinition: %f", PI);

return 0;

You might also like