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

Aayush Malviya Report On C

The document is a report on C programming language by Aayush Malviya from Oriental College of Technology. It covers the features of C, including its procedural nature, portability, speed, and general-purpose capabilities, along with various sample programs demonstrating basic C programming concepts. Additionally, it includes examples of functions, data types, and control structures, showcasing the language's versatility in programming applications.

Uploaded by

aaymal202
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)
23 views46 pages

Aayush Malviya Report On C

The document is a report on C programming language by Aayush Malviya from Oriental College of Technology. It covers the features of C, including its procedural nature, portability, speed, and general-purpose capabilities, along with various sample programs demonstrating basic C programming concepts. Additionally, it includes examples of functions, data types, and control structures, showcasing the language's versatility in programming applications.

Uploaded by

aaymal202
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/ 46

ORIENTAL COLLEGE OF TECHNOLOGY

BHOPAL(M.P)

STUDENT NAME: AAYUSH MALVIYA

ENROLLMENT NO. : 0126IT191003

SUBJECT: REPORT ON C PROGRAMMING


LANGUAGE

BRANCH: IT

SECTION: A

OUR INSTRUCTOR:

MR . KULDEEP MISHRA
ABOUT C
 Procedural Language - Instructions in a C program are executed step by step.
 Portable - You can move C programs from one platform to another, and run it
without any or minimal changes.
 Speed - C programming is faster than most programming languages like Java,
Python, etc.
 General Purpose - C programming can be used to develop operating systems,
embedded systems, databases, and so on.

The main features of C language include


low-level access to memory, a simple set of
keywords, and clean style, these features
make C language suitable for system
programmings like an operating system or
compiler development.
Many later languages have borrowed
syntax/features directly or indirectly from C
language. Like syntax of Java, PHP,
JavaScript, and many other languages are
mainly based on C language. C++ is nearly
a superset of C language (There are few
programs that may compile in C, but not in
C++).
C is a general-purpose programming
language that is extremely popular,
simple and flexible. It is machine-
independent, structured programming
language which is used extensively in
various applications.
C was the basic language to write
everything from operating systems
(Windows and many others) to complex
programs like the Oracle database, Git,
Python interpreter and more.
It is said that 'C' is a god's
programming language. One can say, C
is a base for the programming. If you
know 'C,' you can easily grasp the
knowledge of the other programming
languages that uses the concept of 'C'
It is essential to have a background in
computer memory mechanisms
because it is an important aspect when
dealing with the C programming
language.

Important features of c:
1.It is a robust language with rich set of built-in
functions and operators that can be used to write any
complex program.

2. The C compiler combines the capabilities of an


assembly language with features of a high-level
language.
3.Programs Written in C are efficient and fast. This
is due to its variety of data type and powerful
operators.
3. It is many time faster than BASIC.
4. C is highly portable this means that programs once
written can be run on another machines with little
or no modification.
5. Another important feature of C program, is its
ability to extend itself.
6. A C program is basically a collection of functions
that are supported by C library. We can also create
our own function and add it to C library.
7. C language is the most widely used language in
operating systems and embedded system
development today.

QUESTIONS:

Write a C program to print your


name.

SOL:

include <stdio.h>

int main()
{
printf("Name : AAYUSH MALVIYA\n");
return(0);
}
2. Write a C Program to Find Sum of 5
Subjects and Percentage
SOL:

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,total=500,sum=0;
float per;
clrscr();
printf("Enter Marks of 5 Subjects: ");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf("Sum of 5 Subjects is: %d\n",sum);
per=(sum*100)/total;
printf("Percentage is: %f",per);
getch();
}

OUTPUT

Enter Marks of 5 Subjects: 80 70 85 83 90


Sum of 5 Subjects is: 408
Percentage is: 81.000000

Another que on calculating percentage:

#include <stdio.h>

int side(int n);

int main()

int arr[5];

int i,total=0;

float percent;

printf("enter the marks of five subjects out of 100\n");

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

printf("%d : ",i);

scanf("%d",&arr[i]);

printf("\nthe marks are as follows\n");

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

printf("%5d",arr[i]);

printf("\nthe sum total is: ");

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

total+=arr[i];

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

printf("\nthe percentage is: ");

percent=total/5;

printf("%f",percent);

return 0;

3. write a program to calculate area


of square
#include<stdio.h>

int main() {
int side, area;

printf("\nEnter the Length of Side : ");


scanf("%d", &side);

area = side * side;


printf("\nArea of Square : %d", area);

return (0);
}

OUTPUT

1 Enter the Length of Side : 5


2 Area of Square : 25

4.write a C program to compute the


sum and average of a 5 integer
number?
void main()

{
int a,b,c,d,e,sum,avg;

printf(“Enter the 5 numbers:”);

scanf(“%d%d%d%d%d”,&a,&b,&c,&d,&e);

sum=a+b+c+d+e;

avg=(a+b+c+d+e)/5;

printf(“\n Sum is:”,sum);

printf(“\n Average is:”,avg);

5. WRITE A PROGRAM TO ADD TWO


NUMBERS USING FUNCTION.
#include<stdio.h>

int main() {
int num1, num2, res;

printf("\nEnter the two numbers : ");


scanf("%d %d", &num1, &num2);

//Call Function Sum With Two Parameters


res = sum(num1, num2);

printf("nAddition of two number is : ");


return (0);
}

int sum(int num1, int num2) {


int num3;
num3 = num1 + num2;
return (num3);
}

OUTPUT:

Enter the two numbers : 12 15


Addition of two number is : 27
6. FUNCTION CALL BY VALUE IN C
PROGRAMMING
#include <stdio.h>

int sum(int a, int b)

{ int c=a+b;

return c;}

int main()

{ int var1 =10;

int var2 = 20;

int var3 = sum(var1, var2);

printf("%d", var3);

return 0;

7.FUNCTION CALL BY REFERENCE


VALUE IN C PROGRAMMING

#include <stdio.h>
int increment(int var)

var = var+1;

return var;

int main()

int num1=20;

int num2 = increment(num1)

printf("num1 value is: %d", num1);

printf("\nnum2 value is: %d", num2);

return 0;

Output:
num1 value is: 20

num2 value is: 21

PROGRAM USING ALL DATA TYPES:

#include <stdio.h>

int main()

{ int a = 1; char b ='G'; double c = 3.14;

printf("Hello World!\n");

printf("Hello! I am a character. My value is %c and "

"my size is %lu byte.\n", b,sizeof(char));

printf("Hello! I am an integer. My value is %d and "

"my size is %lu bytes.\n", a,sizeof(int));

printf("Hello! I am a double floating point variable."

" My value is %lf and my size is %lu bytes.\

n",c,sizeof(double));

printf("Bye! See you soon. :)\n");

return 0;
}

Output:

Hello World!

Hello! I am a character. My value is G and my size is 1 byte.

Hello! I am an integer. My value is 1 and my size is 4 bytes.

Hello! I am a double floating point variable. My value is

3.140000 and my size i

s 8 bytes.

Bye! See you soon. :)

PROGRAM USING STRING

#include <stdio.h>

#include <string.h>

int main()

char nickname[20];
printf("Enter your Nick name:");

scanf("%s", nickname);

printf("%s",nickname);

return 0;

Output:

RAHUL

PROGRAM USING CONDITIONAL


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

int mark;

printf("Enter mark: ");

scanf("%d", &mark);

puts(mark >= 40 ? "Passed" : "Failed");

return 0;

Output

Enter mark: 39

Failed

Write a C program to check whether a character is

alphabet or not.

#include <stdio.h>
int main()

char ch;

printf("Enter any character: ");

scanf("%c", &ch);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

printf("Character is an ALPHABET.");

else

printf("Character is NOT ALPHABET.");

return 0;

}
Program to check uppercase or
lowercase alphabets

#include <stdio.h>

int main(){

char ch;

printf("Enter any character: ");

scanf("%c", &ch);

if(ch >= 'A' && ch <= 'Z')

printf("'%c' is uppercase alphabet.", ch);

else if(ch >= 'a' && ch <= 'z')

printf("'%c' is lowercase alphabet.", ch);

else

{
printf("'%c' is not an alphabet.", ch);

return 0;

Write a C program to find all roots of a quadratic equation using


if else. How to find all roots of a quadratic equation using if else
in C programming. Logic to find roots of quadratic equation in C
programming.

#include <stdio.h>
#include <math.h> /* Used for sqrt() */

int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;

printf("Enter values of a, b, c of quadratic


equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);

/* Find discriminant of the equation */


discriminant = (b * b) - (4 * a * c);
if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) /
(2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);

printf("Two distinct and real roots


exists: %.2f and %.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);

printf("Two equal and real roots exists:


%.2f and %.2f", root1, root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 *
a);

printf("Two distinct complex roots


exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2,
imaginary);
}

return 0;
}
OUTPUT:
Output
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 8
-4 -2
Two distinct and real roots exists: 0.81 and -0.31
C program to check positive
negative or zero using switch
case
#include <stdio.h>

int main()
{
int num;

printf("Enter any number: ");


scanf("%d", &num);

switch (num > 0)


{
// Num is positive
case 1:
printf("%d is positive.", num);
break;

// Num is either negative or zero


case 0:
switch (num < 0)
{
case 1:
printf("%d is negative.",
num);
break;
case 0:
printf("%d is zero.", num);
break;
}
break;
}

return 0;
}

Output:
Output
Enter any number: 23
23 is positive.
Program to create calculator using
switch case.
C program to create Simple
Calculator using switch case
#include <stdio.h>

int main()
{
char op;
float num1, num2, result=0.0f;
printf("WELCOME TO SIMPLE CALCULATOR\
n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number
2]\n");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{ case '+':
result = num1 + num2;
break;

case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;

case '/':
result = num1 / num2;
break;

default:
printf("Invalid operator");
}

printf("%.2f %c %.2f = %.2f", num1, op,


num2, result);
return 0;
}

C program to check vowel or


consonant using switch case
#include <stdio.h>

int main()
{
char ch;
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}

C program to print all natural


numbers from 1 to n
#include <stdio.h>

int main()
{
int i, n;
printf("Enter any number: ");
scanf("%d", &n);

printf("Natural numbers from 1 to


%d : \n", n);

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


{
printf("%d\n", i);
}

return 0;
}

Output:
Enter any number: 10
Natural numbers from 1 to 10 :
1
2
3
4
5
6
7
8
9
10

C program to swap first and


last digit of a number
#include <stdio.h>
#include <math.h>

int main()
{
int num, swappedNum;
int firstDigit, lastDigit, digits;
printf("Enter any number: ");
scanf("%d", &num);
lastDigit = num % 10;
digits = (int)log10(num);
firstDigit = (int)(num / pow(10, digits));
swappedNum = lastDigit;
swappedNum *= (int) pow(10, digits);
swappedNum += num % ((int) pow(10,
digits));
swappedNum -= lastDigit;
swappedNum += firstDigit;
printf("Original number = %d", num);
printf("Number after swapping first and
last digit: %d", swappedNum);
return 0;
}

OUTPUT:
Output
Enter any number: 1234
Original number = 1234
Number after swapping first and last digit: 4231
Write a C program to input a number
from user and calculate product of
its digits. How to find product of
digits of a number using loop in C
programming. Logic to find product
of digits of a given number in C
program.

#include <stdio.h>
int main()
{
int num;
long long product=1ll;
printf("Enter any number to calculate
product of digit: ");
scanf("%d", &num);
product = (num == 0 ? 0 : 1ll);

while(num != 0) {
product = product * (num % 10);
num = num / 10;
}

printf("Product of digits = %lld", product);

return 0;
}
OUTPUT:tput

Enter any number to calculate product of digit: 1234


Product of digits = 24

Program to print Pascal triangle

#include <stdio.h>

long long fact(int n);

int main()
{
int n, k, num, i;
long long term;
printf("Enter number of rows : ");
scanf("%d", &num);
for(n=0; n<num; n++)
{
/* Prints 3 spaces */
for(i=n; i<=num; i++)
printf("%3c", ' ');

/* Generate term for current row */


for(k=0; k<=n; k++)
{
term = fact(n) / (fact(k) * fact(n-k));
printf("%6lld", term);
}

printf("\n");
}

return 0;
}

long long fact(int n)


{
long long factorial = 1ll;
while(n>=1)
{
factorial *= n;
n--;
}

return factorial;
}
OUTPUT:
Output
Enter number of rows : 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

C program to search all


occurrences of a character in
a string

#include <stdio.h>
#define MAX_SIZE 100 // Maximum
string size

int main()
{
char str[MAX_SIZE];
char toSearch;
int i;

/* Input string and character to


search from user */
printf("Enter any string: ");
gets(str);
printf("Enter any character to
search: ");
toSearch = getchar();

/* Run loop till the last character of


string */
i=0;
while(str[i]!='\0')
{
if(str[i] == toSearch)
{
printf("'%c' is found at index
%d\n", toSearch, i);
}

i++;
}

return 0;
}

OUTPUT:

Output
Enter any string: I love programming. I love Codeforwin.
Enter any character to search: o
'o' is found at index 3
'o' is found at index 9
'o' is found at index 23
'o' is found at index 28
'o' is found at index 32

Program to get memory address using


address of operator

#include <stdio.h>

int main()
{
/* Simple declarations */
char character = 'C';
int integer = 1;
float real = 10.4f;
long long biginteger = 989898989ll;
printf("Value of character = %c,
Address of character = %u\n",
character, &character);
printf("Value of integer = %d,
Address of integer = %u\n", integer,
&integer);
printf("Value of real = %f, Address of
real = %u\n", real, &real);
printf("Value of biginteger = %lld,
Address of biginteger = %u",
biginteger, &biginteger);

return 0;
}

Output
Value of character = C, Address of character = 6356751
Value of integer = 1, Address of integer = 6356744
Value of real = 10.400000, Address of real = 6356740
Value of biginteger = 989898989, Address of biginteger = 6356728
C program to sort an array
using pointers

#include <stdio.h>

#define MAX_SIZE 100

/* Function declaration */
void inputArray(int * arr, int size);
void printArray(int * arr, int size);

/* Sort function declaration */


int sortAscending(int * num1, int *
num2);
int sortDescending(int * num1, int *
num2);

void sort(int * arr, int size, int (*


compare)(int *, int *));

int main()
{
int arr[MAX_SIZE];
int size;

/*
* Input array size and elements.
*/
printf("Enter array size: ");
scanf("%d", &size);
printf("Enter elements in array: ");
inputArray(arr, size);

printf("\n\nElements before sorting:


");
printArray(arr, size);

// Sort and print sorted array in


ascending order.
printf("\n\nArray in ascending order:
");
sort(arr, size, sortAscending);
printArray(arr, size);
// Sort and print sorted array in
descending order.
printf("\nArray in descending order:
");
sort(arr, size, sortDescending);
printArray(arr, size);

return 0;
}

/**
* Function to take input in array
elements.
*
* @arr Array to store input.
* @size Size of the array.
*/
void inputArray(int * arr, int size)
{
// Pointer to last element of array
int * arrEnd = (arr + size - 1);
// (arr++) Input in current array
element and move to next element.
// Till last array element (arr <=
arrEnd)
while(arr <= arrEnd)
scanf("%d", arr++);
}

/**
* Function to print all array elements.
*
* @arr Array to print.
* @size Size of the array.
*/
void printArray(int * arr, int size)
{
// Pointer to last element of array
int * arrEnd = (arr + size - 1);

// *(arr++) Print current array


element and move to next element.
// Till last array element (arr <=
arrEnd)
while(arr <= arrEnd)
printf("%d, ", *(arr++));
}

/**
* Function to compare two succesive
elements.
* The function returns difference of
first and second integer.
*
* @num1 First integer to compare.
* @num2 Second integer to compare.
*
* @return Difference of num1 and
num2.
*/
int sortAscending(int * num1, int *
num2)
{
return (*num1) - (*num2);
}
/**
* Function to compare two successive
elements.
* The function returns difference of
second and first parameter.
*
* @num1 First integer to compare.
* @num2 Second integer to compare.
*
* @return Difference of num2 and
num1.
*/
int sortDescending(int * num1, int *
num2)
{
return (*num2) - (*num1);
}

/**
* Function to sort an array in
ascending or descending order.
* This function is used to sort array in
both order ascending or
* descending.
*
* @arr Array to sort.
* @size Size of the array.
* @compare Function pointer returning
integer and takes two int *
* parameter. The function is
called to get arrangement of
* two successive array elements.
*/
void sort(int * arr, int size, int (*
compare)(int *, int *))
{
// Pointer to last array element
int * arrEnd = (arr + size - 1);

// Pointer to current array element


int * curElem = arr;
int * elemToSort;

// Iterate over each array element


while(curElem <= arrEnd)
{
elemToSort = curElem;

while(elemToSort <= arrEnd)


{
if(compare(curElem,
elemToSort) > 0)
{
*curElem ^= *elemToSort;
*elemToSort ^= *curElem;
*curElem ^= *elemToSort;
}

elemToSort++;
}

curElem++;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy