0% found this document useful (0 votes)
41 views41 pages

#Include #Include Void Int Float

The document contains 14 code snippets demonstrating the use of arrays, functions, and pointers in C programming. The snippets cover topics like inputting and outputting array elements, defining and calling functions to perform operations on arrays like addition, averaging, finding minimum values, and calculating quadratic roots. The last snippet demonstrates passing pointer variables to a function to return calculated values.

Uploaded by

Waqas Ahmed Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views41 pages

#Include #Include Void Int Float

The document contains 14 code snippets demonstrating the use of arrays, functions, and pointers in C programming. The snippets cover topics like inputting and outputting array elements, defining and calling functions to perform operations on arrays like addition, averaging, finding minimum values, and calculating quadratic roots. The last snippet demonstrates passing pointer variables to a function to return calculated values.

Uploaded by

Waqas Ahmed Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Q1:

#include <stdio.h>
#include <conio.h>

void main()
{
int age;
float amount;
printf("Enter your age & amount=");
scanf_s("%d %f", &age, &amount);
printf("\nYou are %d years old & you have %f rupees.", age, amount);

_getch();

Output

Q2= Simple Addition


#include<stdio.h>
#include<conio.h>

float add(float x, float y);


float add(float x, float y){
float z = x + y;
return z;
}
void main()
{
float a, b, c;
printf("Enter A Value");
scanf_s("%f", &a);
printf("Enter B Value");
scanf_s("%f", &b);
c = add(a, b);
printf("Sum Of The Number Is =%f",c);
_getch();
}

OUTPUT

Q3= ADD Mul Sub Div


#include<stdio.h>
#include<conio.h>

float add(float x, float y);


float add(float x, float y){
float z = x + y;
return z;
}
float min(float x, float y);
float min(float x, float y){
float z = x - y;
return z;
}
float mul(float x, float y);
float mul(float x, float y){
float z = x*y;
return z;
}
float div(float x, float y);
float div(float x, float y){
float z = x / y;
return z;
}
void main(){
float a, b, c, d, e, f;
printf("enter a\n");
scanf_s("%f", &a);
printf("enter b\n");
scanf_s("%f", &b);
c = add(a, b);
d = min(a, b);
e = mul(a, b);
f = div(a, b);
printf("Sum Of The Number Is =%f\nSub Of The Number Is =%f\nMul Of The Number Is
=%f\nDiv Of The Number Is =%f\n", c, d, e, f);
_getch();
}

OutPut

Q4

#include<stdio.h>

#include<conio.h>

int factorial(int n);

int factorial(int n)

if (n < 0)

n = -n;
int ans = 1;

for (int i = 1; i <= n; i++)

ans = ans*i;

return -ans;

if (n > 0)

int ans = 1;

for (int i = 1; i <= n; i++)

ans = ans*i;

return ans;

if (n == 0)

return 1;

void main()

int a, b;
printf("Enter Number which factorial is to be determined=");

scanf_s("%d", &a);

b = factorial(a);

printf("\n%d", b);

_getch();

Q=5

#include<stdio.h>

#include<conio.h>

float pow(int x, int y);

float pow(int x, int y)

if (y < 0)

{
float ans = 1;

y = -y;

for (int i = 1; i <= y; i++)

ans = ans*x;

return 1 / ans;

if (y > 0)

float ans = 1;

for (int i = 1; i <= y; i++)

ans = ans*x;

return ans;

if (y == 0)

return 1;

void main()
{

float a, b, c;

printf("Enter base & power");

scanf_s("%f %f", &a, &b);

c = pow(a, b);

printf("\n result= %.0f", c);

_getch();

Output

Q=6

#include<stdio.h>

#include<conio.h>
void primenumber(int num);

void primenumber(int num)

int i;

for (i = 2; i < num; i++)

if (num%i == 0)

printf("Not prime number");

break;

if (i == num)

printf("%d is a prime number", num);

void main()

int p;

printf("Enter a number");

scanf_s("%d", &p);

primenumber(p);

_getch();

}
Output

Q=7

#include<stdio.h>

#include<conio.h>

void main(){

float marks[5];

//float marks[5] = { 55, 35, 85, 25, 10 };

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

printf("marks[%d]?", i);

scanf_s("%f", &marks[i]);

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

printf("%.0f\t", marks[i]);
_getch();

Output

Q=8

#include<stdio.h>

#include<conio.h>

void printArray(float nums[], int length);

void printArray(float nums[], int length) {

for (int i = 0; i < length; i++)

printf("%.0f\t", nums[i]);

nums[0] = 99;

printf("\n");

}
void main() {

int length = 3;

float x[] = { 12, 13, 14 };

printArray(x, length);

printArray(x, length);

_getch();

Output

Q=9
#include<stdio.h>
#include<conio.h>

void printarray(float nums[], int length);


void printarray(float nums[], int length){
for (int i = 0; i < length; i++)
printf("%f\t", nums[i]);
printf("\n");
}
void addarray(float nums1[], float nums2[], float nums3[], int length);
void addarray(float nums1[], float nums2[], float nums3[], int length){
for (int i = 0; i < length; i++)
nums3[i] = nums1[i] + nums2[i];
}
void main(){
int length = 3;
float x[3] = { 12, 13, 14 };
float y[3] = { 2, 3, -2 };
float z[3];

addarray(x, y, z, length);
printarray(x, length);
printarray(y, length);
printarray(z, length);
_getch();
}

Output

Q=10

#include<stdio.h>

#include<conio.h>

void inputarray(float nums[], int length);

void inputarray(float nums[], int length){

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

printf("nums[%d}=?", i);
scanf_s("%f", &nums[i]);

void printarray(float nums[], int length);

void printarray(float nums[], int length){

for (int i = 0; i < length; i++)

printf("%f\t",nums[i]);

printf("\n");

void addarray(float nums1[], float nums2[], float nums3[], int length);

void addarray(float nums1[], float nums2[], float nums3[], int length){

for (int i = 0; i < length; i++)

nums3[i] = nums1[i] + nums2[i];

void main(){

int length = 3;

float x[3];

float y[3];

float z[3];

inputarray(x, length);

inputarray(y, length);

addarray(x, y, z, length);

printarray(x, length);

printarray(y, length);

printarray(z, length);

_getch();

Output
Q=11

#include<stdio.h>

#include<conio.h>

float sumarray(float nums[], int length);

float sumarray(float nums[], int length){

float ans = 0;

for (int i = 0; i < length; i++)

ans = ans + nums[i];

return ans;

float avgarray(float nums[], int length);

float avgarray(float nums[], int length){

return sumarray(nums, length) / length;

void main(){

float x[4] = { 4, 3, 4, 5 };
printf("avg= %.2f", avgarray(x, 4));

_getch();

Output

Q=12

#include<stdio.h>

#include<conio.h>

void inputarray(float nums[], int length);

void inputarray(float nums[], int length)

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

printf("nums[%d}=?", i);

scanf_s("%f", &nums[i]);

}
float sumarray(float nums[], int length);

float sumarray(float nums[], int length){

float ans = 0;

for (int i = 0; i < length; i++)

ans = ans + nums[i];

return ans;

float avgarray(float nums[], int length);

float avgarray(float nums[], int length){

return sumarray(nums, length) / length;

void main(){

int length = 3;

float x[3];

inputarray(x, length);

float b;

b = avgarray(x, length);

printf("avg= %.2f",b);

_getch();

Output
Q=13

#include<stdio.h>

#include<conio.h>

void printarray(float nums[], int length);

void printarray(float nums[], int length){

for (int i = 0; i < length; i++)

printf("%f\t",nums[i]);

printf("\n");

float min(float nums[], int length);

float min(float nums[], int length){

float minimums = nums[0];

for (int i = 1; i < length; i++)

if (nums[i] < minimums)

minimums = nums[i];

return minimums;
}

void main(){

int length = 5;

float nums[] = { 5, 7, 3, 2, 4 };

printarray(nums, length);

printf("minimum =%.2f\n", min(nums, length));

_getch();

Output

Q 14

#include<stdio.h>

#include<conio.h>

#include<math.h>

void quadroots(float a, float b, float c, float roots[]);

void quadroots(float a, float b, float c, float roots[]){


float disc = b*b - 4 * a*c;

if (disc < 0)

return;

roots[0] = (-b + sqrt(disc)) / (2 * a);

roots[1] = (-b - sqrt(disc)) / (2 * a);

void main(){

float a, b, c;

printf("enter a");

scanf_s("%f", &a);

printf("enter b");

scanf_s("%f", &b);

printf("enter c");

scanf_s("%f", &c);

float x[2];

quadroots(a, b, c, x);

printf("x1= %.2f\t x2 = %.2f", x[0], x[1]);

_getch();

Output
Q=

#include<stdio.h>

#include<conio.h>

void main()

float x = 2;

float *p;

p = &x;

printf("%x %x\n", p, &x);

_getch();

Output
Q= Quadratic formula with pointers

#include<stdio.h>
#include<conio.h>
#include<math.h>
void quadroots(float a, float b, float c, float *p1, float *p2);
void quadroots(float a, float b, float c, float *p1, float *p2)
{
float disc = b*b - 4 * a*c;
if (disc < 0)
return;
*p1 = (-b + sqrt(disc)) / (2 * a);
*p2 = (-b - sqrt(disc)) / (2 * a);
}
void main()
{
float a = 1;
float b = 0;
float c = -4;
float x1, x2;
quadroots(a, b, c, &x1, &x2);
printf("a=%f\tb=%f\tc=%f", a, b, c);
printf("\nx1=%f\tx2=%f", x1, x2);
_getch();
}

Output
Q: Swapping
#include<conio.h>
#include<stdio.h>
void swap(float *px, float *py);
void swap(float *px, float *py)
{
float z = *px;
*px = *py;
*py = z;
}
void main()
{
float x = 2;
float y = 3;
printf("x=%f\ty=%f", x, y);
swap(&x , &y);
printf("\nx=%f\ty=%f",x, y);
_getch();
}

Output
Q=19

#include<stdio.h>

#include<conio.h>

float min(float a, float b, float c, float d);

float min(float a, float b, float c, float d)

float m = a;

if (b < m)

m = b;

if (c < m)

m = c;

if (d < m)

m = d;

return m;

}
void main()

float w, x, y, z;

w = 7;

x = 5;

y = 4;

z = 3;

printf("%f", min(w, x, y, z));

_getch();

Q=20

#include<stdio.h>

#include<conio.h>

float min(float a, float b);

float min(float a, float b)


{

if (a < b)

return a;

else

return b;

float min(float a, float b, float c, float d);

float min(float a, float b, float c, float d)

return min(a, min(b, min(c, d)));

void main()

float w, x, y, z;

w = 7;

x = 5;

y = 4;

z = 3;

printf("%f", min(w, x, y, z));

_getch();

Output
Q=21

#include<stdio.h>

#include<conio.h>

#include<math.h>

int factorial(int n);

int factorial(int n)

if (n < 0)

n = -n;

int ans = 1;

for (int i = 1; i <= n; i++)

ans = ans*i;

return -ans;

}
if (n > 0)

int ans = 1;

for (int i = 1; i <= n; i++)

ans = ans*i;

return ans;

if (n == 0)

return 1;

float anyseries(float x);

float anyseries(float x){

float sum = 0;

for (int i = 1; i <= 2; i++)

sum = sum + pow(-1.0, i + 1)*pow(x, i) / factorial(i);

return sum;

void main()
{

float x = 1;

printf("%f", anyseries(x));

_getch();

Q=22

#include<stdio.h>

#include<conio.h>

void swap(float *px, float *py);

void swap(float *px, float *py){

float z = *px;

*px = *py;

*py = z;

}
void printarray(float nums[], int length);

void printarray(float nums[], int length){

for (int i = 0; i < length; i++)

printf("%f\t", nums[i]);

printf("\n");

void sortarray(float nums[], int length);

void sortarray(float nums[], int length)

for (int j = 0; j < length - 1; j++)

for (int i = 0; i < length - 1; i++)

if (nums[i]>nums[i + 1])

swap(&nums[i], &nums[i + 1]);

void main()

float nums[] = { 5, 2, 9, 3 };

printarray(nums, 4);

sortarray(nums, 4);

printarray(nums, 4);

_getch();
}

Output

Q=23

#include<stdio.h>

#include<conio.h>

void swap(float *px, float *py);

void swap(float *px, float *py){

float z = *px;

*px = *py;

*py = z;

void printarray(float nums[], int length);

void printarray(float nums[], int length){

for (int i = 0; i < length; i++)

printf("%f\t", nums[i]);
printf("\n");

void sortarray(float nums[], int length,bool asc);

void sortarray(float nums[], int length,bool asc)

for (int j = 0; j < length - 1; j++)

for (int i = 0; i < length - 1; i++)

if (asc == true)

if (nums[i]>nums[i + 1])

swap(&nums[i], &nums[i + 1]);

else

if (nums[i] < nums[i + 1])

swap(&nums[i], &nums[i + 1]);

}
void main()

float nums[] = { 5, 2, 9, 3 };

printarray(nums, 4);

sortarray(nums, 4,true);

printarray(nums, 4);

sortarray(nums, 4, false);

printarray(nums, 4);

_getch();

Output

Q=24 relation array and pointer

#include<stdio.h>

#include<conio.h>

void inputarray(float *nums, int length);


void inputarray(float *nums, int length){

for (int i = 0; i < length; i++)

printf("nums[%d}=?", i);

scanf_s("%f", &nums[i]);

void printarray(float nums[], int length);

void printarray(float nums[], int length)

for (int i = 0; i < length; i++)

printf("%f\t", nums[i]);

printf("\n");

void main()

int length;

printf("Define length?");

scanf_s("%d", &length);

float *nums;

nums = new float[length];

inputarray(nums, length);

printarray(nums, length);

_getch();
}

Output

Q=25 Matrix
#include<stdio.h>
#include<conio.h>

float **creatmatrix(int rows, int cols);


float **creatmatrix(int rows, int cols){
float **mat = new float*[rows];
for (int i = 0; i < rows; i++)
mat[i] = new float[cols];
return mat;
}
void inputmatrix(float **mat, int rows, int cols);
void inputmatrix(float **mat, int rows, int cols){
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{

printf("mat[%d][%d]?", i, j);
scanf_s("%f", &mat[i][j]);
}
}
}
void printmatrix(float **mat, int rows, int cols);
void printmatrix(float **mat, int rows, int cols){
for (int i = 0; i < rows; i++)
{
printf("\n");
for (int j = 0; j < cols; j++)
printf("%.2f\t", mat[i][j]);
}
printf("\n");
}

void main()
{
int rows, cols;
printf("Rows?");
scanf_s("%d", &rows);
printf("cols?");
scanf_s("%d", &cols);

float **matA = creatmatrix(rows, cols);

inputmatrix(matA, rows, cols);

printmatrix(matA, rows, cols);

_getch();
}

Q=26

#include<stdio.h>

#include<conio.h>

float **creatmatrix(int rows, int cols);

float **creatmatrix(int rows, int cols){


float **mat = new float*[rows];

for (int i = 0; i < rows; i++)

mat[i] = new float[cols];

return mat;

void inputmatrix(float **mat, int rows, int cols);

void inputmatrix(float **mat, int rows, int cols){

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

printf("mat[%d][%d]?", i, j);

scanf_s("%f", &mat[i][j]);

void printmatrix(float **mat, int rows, int cols);

void printmatrix(float **mat, int rows, int cols){

for (int i = 0; i < rows; i++)

printf("\n");

for (int j = 0; j < cols; j++)

printf("%.2f\t", mat[i][j]);

printf("\n");

float **addmatrix(float **matA, float **matB, int rows, int cols);

float **addmatrix(float **matA, float **matB, int rows, int cols){


float **matC = creatmatrix(rows, cols);

for (int i = 0; i < rows;i++)

for (int j = 0; j < cols; j++)

matC[i][j] = matA[i][j] + matB[i][j];

return matC;

void main()

int rows, cols;

printf("Rows?");

scanf_s("%d", &rows);

printf("cols?");

scanf_s("%d", &cols);

float **matA = creatmatrix(rows, cols);

float **matB = creatmatrix(rows, cols);

inputmatrix(matA, rows, cols);

inputmatrix(matB, rows, cols);

float **matC = addmatrix(matA, matB, rows, cols);

printmatrix(matA, rows, cols);

printmatrix(matB, rows, cols);

printmatrix(matC, rows, cols);

_getch();

Output
Q=27

#include<stdio.h>

#include<conio.h>

float **creatmatrix(int rows, int cols);

float **creatmatrix(int rows, int cols){

float **mat = new float*[rows];

for (int i = 0; i < rows; i++)

mat[i] = new float[cols];

return mat;

void inputmatrix(float **mat, int rows, int cols);

void inputmatrix(float **mat, int rows, int cols){

for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++)

{
printf("mat[%d][%d]?", i, j);

scanf_s("%f", &mat[i][j]);

void printmatrix(float **mat, int rows, int cols);

void printmatrix(float **mat, int rows, int cols){

for (int i = 0; i < rows; i++)

printf("\n");

for (int j = 0; j < cols; j++)

printf("%.2f\t", mat[i][j]);

printf("\n");

float **multiplymatrix(float **matA, float **matB, int rowsA, int colsA, int rowsB, int colsB);

float **multiplymatrix(float **matA, float **matB, int rowsA, int colsA, int rowsB, int colsB){

if (colsA != rowsB) return NULL;

int rowsC = rowsA;

int colsC = rowsB;

float **matC = creatmatrix(rowsC, colsC);

for (int i = 0; i < rowsC; i++)

for (int j = 0; j < colsC; j++)

matC[i][j] = 0;

for (int k = 0; k < rowsB; k++)

matC[i][j] = matC[i][j] + matA[i][k] * matB[k][j];


}

return matC;

void main()

int rowsA, colsA, rowsB, colsB;

printf("RowsA?");

scanf_s("%d", &rowsA);

printf("colsA?");

scanf_s("%d", &colsA);

printf("rowsB?");

scanf_s("%d", &rowsB);

printf("colsB?");

scanf_s("%d", &colsB);

float **matA = creatmatrix(rowsA, colsA);

float **matB = creatmatrix(rowsB, colsB);

inputmatrix(matA, rowsA, colsA);

inputmatrix(matB, rowsB, colsB);

float **matC = multiplymatrix(matA, matB, rowsA, colsA, rowsB, colsB);

printmatrix(matA, rowsA, colsA);

printmatrix(matB, rowsB, colsB);


if (matC == NULL)

printf("can not multiply these matrices due to order mismatch");

else

printmatrix(matC, rowsA, colsB);

_getch();

Output

You might also like