c programs unit I
c programs unit I
#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;
#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
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);
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;
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
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;
EXAMPLE2
#include <stdio.h>
int main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &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);
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;
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
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()
// Undefine PI
#ifdef PI
#undef PI
#endif
// Redefine value of PI
#define PI 3.14159
return 0;