You are on page 1of 13

1) Using operator

#include <stdio.h>
int main()
{ int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

2.Manage input and output


#include<stdio.h>
Int main()
{
int a,b,c;

printf("Please enter any two numbers: \n");


scanf("%d %d", &a, &b);
c = a + b;
printf("The addition of two number is: %d", c);
Return 0;
}

3.Decision making and branching


statements
#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");
}

}
4 Decision making and looping statements

#include <stdio.h>

int main()
{
int i;

for (i = 1; i < 11; ++i)


{
printf("%d ", i);
}
return 0;
}

5 Array ;

#include <stdio.h>

int main() {
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

for(int i = 0; i < 5; ++i) {


printf("%d\n", values[i]);
}
return 0;
}

6 user defined functions

#include <stdio.h>
int addNumbers(int a, int b);

int main()
{
int n1,n2,sum;

printf("Enters two numbers: ");


scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2);
printf("sum = %d",sum);

return 0;
}

int addNumbers(int a, int b)


{
int result;
result = a+b;
return result;
}

7 Program using Pointers


#include <stdio.h>
int main()
{
int* pc, c;

c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22

pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11

*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}

FILE
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
FILE *fptr;

if ((fptr =
fopen("C:\\program.txt","r")) == NULL){
printf("Error! opening file");

exit(1);
}

fscanf(fptr,"%d", &num);

printf("Value of n=%d", num);


fclose(fptr);

return 0;
}

Output

Area of Room = 1309


Volume of Room = 25132.8

12
C++ program to demonstrate
inheritance

#include <iostream>
using namespace std;
// base class
class Animal {

public:
void eat() {
cout << "I can eat!" << endl;
}

void sleep() {
cout << "I can sleep!" << endl;
}
};

// derived class
class Dog : public Animal {

public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};

int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();

// Calling member of the derived class


dog1.bark();

return 0;
}

UNION
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
union Job;
};

struct structJob
{
char name[32];
float salary;
int workerNo;
struct job;
};

int main()
{
printf("size of union=%sbytes","size of (union job)");
printf("\nsize of struct=%sbytes","size of (struct
job)");
return 0;
}

11)Illustrate the concept of


constructor and destructor
#include<iostream>

using namespace std;

class Demo {

private:

int num1, num2;


public:

Demo(int n1, int n2) {

cout<<"Inside Constructor"<<endl;

num1 = n1;

num2 = n2;

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

~Demo() {

cout<<"Inside Destructor";

};

int main() {

Demo obj1(10, 20);

obj1.display();

return 0;

}
9 th program - file handling

#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
FILE *fptr;

if ((fptr =
fopen("C:\\program.txt","r")) == NULL){
printf("Error! opening file");

exit(1);
}

fscanf(fptr,"%d", &num);

printf("Value of n=%d", num);


fclose(fptr);

return 0;
}

You might also like