0% found this document useful (0 votes)
17 views

Sample Codes For Learning

These will help to learn the basics

Uploaded by

sifatmazib625
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)
17 views

Sample Codes For Learning

These will help to learn the basics

Uploaded by

sifatmazib625
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

Sample Codes for Mid-Preparation

Basic Input-Output Operations:

1. Hello World

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

printf("Hello world");

return 0;
}

2. Arithmetic Operation

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

int a,b ;
a=9;
b=3;

int s;
s=a+b;
printf("Result: %d",s);

s=a-b;
printf("\nResult: %d",s);

s=a*b;
printf("\nResult: %d",s);

s=a/b;
printf("\nResult: %d",s);

return 0;
}
3. Arithmatic Oeration with User Input

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

int a,b ;

printf("Enter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);

int s;
s=a+b;
printf("Result: %d",s);

s=a-b;
printf("\nResult: %d",s);

s=a*b;
printf("\nResult: %d",s);

s=a/b;
printf("\nResult: %d",s);

return 0;
}
4. Area of Rectangle

#include <stdio.h>
int main()
{
int length, width, area;

printf("Enter length of rectangle: ");


scanf("%d", &length);
printf("Enter width of rectangle: ");
scanf("%d", &width);

area = length * width;

printf("Area of rectangle = %d sq. units ", area);

return 0;
}

5. Float example

#include <stdio.h>

int main() {

float a,b ;
a=9.62;
b=3.114;

float s;
s=a+b;
printf("Result: %f",s);

s=a-b;
printf("\nResult: %f",s);

s=a*b;
printf("\nResult: %f",s);

s=a/b;
printf("\nResult: %f",s);

return 0;
}

[ %.2f if we only want two decimal digits , %.3f if we want three. %.f will
return the integer value only. ]

6. Triangle Area

#include <stdio.h>
int main()
{
float base, height, area;

printf("Enter base of the triangle: ");


scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);

area = (base * height) / 2;


printf("Area of the triangle = %f sq. units", area);

return 0;
}
7. Find Angles of Triangle

#include <stdio.h>

int main()
{
int a, b, c;

printf("Enter first angle: ");


scanf("%d", &a);
printf("Enter second angle: ");
scanf("%d", &b);

c = 180 - (a + b);

printf("Third angle of the triangle = %d", c);

return 0;
}

8. Temp Conversion

#include <stdio.h>
int main()
{
float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");


scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("%f Celsius = %f Fahrenheit", celsius,


fahrenheit);

return 0;
}
9. Length Conversion

#include <stdio.h>

int main()
{
float cm, meter, km;

printf("Enter length in centimeter: ");


scanf("%f", &cm);

meter = cm / 100.0;
km = cm / 100000.0;

printf("Length in Meter = %f m \n", meter);


printf("Length in Kilometer = %f km", km);

return 0;
}

Increment Operator example-1

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

int num, a=5;


num=4*a++;

printf("%d",num);
printf("\n%d",a);

return 0;
}
Increment Operator example-2

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

int num,a=5;
num=4* ++a;

printf("%d",num);
printf("\n%d",a);

return 0;
}
User Defined Function:

User Defined Function in C example-1

#include <stdio.h>

int sum(int p, int q)


{
int ans;
ans = p + q;
return ans;
}

int main()
{
int p;
p=sum(3,4);
printf("%d",p);

return 0;
}

User Defined Function in C example-2

#include <stdio.h>

int sum(int p, int q)


{
int ans;
ans = p + q;
return ans;
}

int main()
{
int result;
int x,y;
scanf("%d",&x);
scanf("%d",&y);

result=sum(x,y);
printf("%d",result);

return 0;
}

User Defined Function in C example-3

#include <stdio.h>

int sum()
{
int ans;
int p,q ;
scanf("%d",&p);
scanf("%d",&q);
ans = p + q;
return ans;
}

int main()
{
int result;

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

return 0;
}
User Defined Function in C example-4

#include <stdio.h>

void sum()
{
int ans;
int p,q ;
scanf("%d",&p);
scanf("%d",&q);
ans = p + q;
printf("%d",ans);
return;
}

int main()
{

sum();

return 0;
}
Multi function Program With Prototype Declaration:

#include <stdio.h>

int sum(int p, int q);


int product(int p, int q);

int main()
{
int result;
int x,y;
scanf("%d",&x);
scanf("%d",&y);

result=sum(x,y);
printf("Sum: %d",result);

result=product(x,y);
printf(" Product: %d",result);

return 0;
}

int sum(int p, int q)


{
int ans;
ans = p + q;
return ans;
}

int product(int p, int q)


{
int ans;
ans = p * q;
return ans;
}
Multi function Program Without Prototype Declaration:

#include <stdio.h>

int sum(int p, int q)


{
int ans;
ans = p + q;
return ans;
}

int product(int p, int q)


{
int ans;
ans = p * q;
return ans;
}

int main()
{
int result;
int x,y;
scanf("%d",&x);
scanf("%d",&y);

result=sum(x,y);
printf("Sum: %d",result);

result=product(x,y);
printf(" Product: %d",result);

return 0;
}
Library Function:

POW function example

#include <stdio.h>
#include <math.h>

int main() {

int num=3;
int p;
p=pow(num,3);
printf("%d",p);

return 0;
}

SQRT function example

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

int num=16;
int p;
p=sqrt(num);
printf("%d",p);

return 0;
}
PDF Lecture 5 Exercise

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

int h,v;
printf("Enter base: ");
scanf("%d",&v);
printf("\nEnter height: ");
scanf("%d",&h);

float area;
float hype;
area= 0.5 * h *v;
hype= pow(h,2) + pow(v,2);
hype = sqrt(hype);

printf("\nArea=%.2f \n",area);
printf("\nHyptenous=%.2f", hype);

return 0;
}
If-Else Condition:

Zero or Pos or Neg number

#include <stdio.h>

int main()
{
int num;

printf("Enter any number: ");


scanf("%d", &num);

if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}

return 0;
}
Zero or Pos or Neg number Alternate way

#include <stdio.h>

int main()
{
int num;

printf("Enter any number: ");


scanf("%d", &num);

if(num > 0)
{
printf("Number is POSITIVE");
}
else if(num < 0)
{
printf("Number is NEGATIVE");
}
else
{
printf("Number is ZERO");
}

return 0;
}
Check divisibility by 5

#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num % 5 == 0)
{
printf("Number is divisible by 5 ");
}
else
{
printf("Number is not divisible by 5");
}
return 0;
}

Check even or odd

#include <stdio.h>
int main()
{
int num;
printf("Enter any number to check even or odd: ");
scanf("%d", &num);

if(num % 2 == 0)
{
printf("Number is Even.");
}
else
{
printf("Number is Odd.");
}

return 0;
}
Greater Number check

#include <stdio.h>

int main()
{
int num1, num2;

printf("Enter number1: ");


scanf("%d", &num1);
printf("Enter number2: ");
scanf("%d", &num2);

if(num1 > num2)


{
printf("%d is maximum", num1);
}

else if(num2 > num1)


{
printf("%d is maximum", num2);
}

else
{
printf("Both are equal");
}

return 0;
}
Grade from Subjects Obtained Marks (Using If only)

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

int p ;
printf("Enter obtained marks: ");
scanf("%d", &p);

if (p>=80 && p<=100)


{
printf("A");
}
if(p>=70 && p<=79)
{
printf("B");
}
if(p>=60 && p<=69)
{
printf("C");
}
if(p>=50 && p<=59)
{
printf("D");
}
if(p>=0 && p<=49)
{
printf("Fail");
}

return 0;
}
Grade from Subjects Obtained Marks Alternate solution

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

int p ;
printf("Enter obtained marks: ");
scanf("%d", &p);

if (p>=80)
{
printf("A");
}
else if(p>=70)
{
printf("B");
}
else if(p>=60)
{
printf("C");
}
else if(p>=50 )
{
printf("D");
}
else
{
printf("Fail");
}

return 0;
}
If-Else example to check character input

#include <stdio.h>

int main() {
char character;

printf("Enter a character: ");


scanf(" %c", &character);

if (character >= 'a' && character <= 'z') {


printf("\nIt's lowercase letter.");
}
else if (character >= 'A' && character <= 'Z') {
printf("\nIt's uppercase letter.");
}
else if (character >= '0' && character <= '9') {
printf("\nIt's a digit.");
}
else {
printf("I don't know.\n");
}

return 0;
}
Show maximum between three numbers

#include <stdio.h>

int main() {

int a,b,c;
a=5;
b=3;
c=2;

if(a>b && a>c)


printf("%d is maximum",a);
else if(b>a && b>c)
printf("%d is maximum",b);
else
printf("%d is maximum",c);

return 0;
}
Loop :

Checkout lecture pdf and class records for preparation.

You might also like