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

CP Programs

Uploaded by

Vijay
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)
26 views

CP Programs

Uploaded by

Vijay
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/ 60

// hello world

#include <stdio.h> Output:


int main()
{
printf("Hello world");
return 0;
}

//back slash characters


#include <stdio.h>
int main() Output:
{
printf("\nnew line");
printf("\nsecond line\n");
printf("\t\"escape sequence\"");
printf("\nwatch\bcarefully");
return 0;
}

//swapping of two variables using 3rd variable


#include <stdio.h>
void main () Output:
{
inta,b,c;
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("a=%d,b=%d",a,b);
}
//swapping of two variables without using 3rd variable
#include <stdio.h>
void main () {
inta,b,c; Output:
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d,b=%d",a,b);
}

//area and perimeter of circle


#include<stdio.h> Output:
#define PI 3.14
void main()
{
float a, p,r;
printf("enter radius of a circle: ");
scanf("%f",&r);
a=PI*r*r;
p=2*PI*r;
printf("area of circle=%.2f",a);
printf("\nperimeter of circle=%.2f",p);
}

//area and perimeter of rectangle


#include<stdio.h> Output:
void main()
{
int a, p,l,b;
printf("enter length of a rectangle: ");
scanf("%d",&l);
printf("enter breadth of a rectangle: ");
scanf("%d",&b);
a=l*b;
p=2*(l+b);
printf("area of rectangle=%d",a);
printf("\nperimeter of rectangle=%d",p);
}

//area and perimeter of square


#include<stdio.h> Output:
void main()
{
int a, p,s;
printf("enter side of square: ");
scanf("%d",&s);
a=s*s;
p=4*s;
printf("area of square=%d",a);
printf("\nperimeter of square=%d",p);
}

//area of triangle
#include<stdio.h>
void main()
{
Output:
float a,h,b;
printf("enter height : ");
scanf("%f",&h);
printf("enter base length : ");
scanf("%f",&b);
a=0.5*b*h;
printf("area of triangle=%.2f",a);
}

//C to F
#include<stdio.h>
int main() Output:
{
float f, c;
printf("enter temperature in celsius: ");
scanf("%f",&c);
f =( (c*9)/5)+32;
printf("Temperature in fahrenheit is: %.2f",f);
return (0);
}

//F to C
#include<stdio.h>
int main() Output:
{
float f, c;
printf("enter temperature in fahrenheit : ");
scanf("%f",&f);
c = (f-32) * 5 / 9;
printf("Temperature in celsius is: %.2f",c);
return (0);
}

//character functions
#include<ctype.h>
#include<stdio.h>
int main()
{
char n;
printf("\nEnter a character=");
n=getchar();
if(isalnum(n)) {
printf("\nYou typed an alphabet or digit"); Output:
if(isalpha(n))
printf("\nYou typed an alphabet");
if(isdigit(n))
printf("\nYou typed a digit");
}
if(isspace(n))
printf("\nYou typed a blank space");
if(ispunct(n))
printf("\nYou typed punctuator");
return(0);
}
//converting lower case character to upper and vice versa
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("\nEnter an alphabet="); Output:
ch=getchar();
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
printf("\nNow alphabet=%c",ch);
return(0);
}
//arithmetic operators
#include <stdio.h> Output:
int main()
{
int a = 9,b = 4;
printf("a+b = %d \n",a+b);
printf("a-b = %d \n",a-b);
printf("a*b = %d \n",a*b);
printf("a/b = %d \n",a/b);
printf("Remainder when a divided by b = %d \n",a%b);
return 0;
}

//increment decrement operators


#include <stdio.h> Output:
int main()
{
int a = 10;
printf("++a = %d \n", ++a);
printf("a++ = %d \n", a++);
printf("--a = %d \n", --a);
printf("a-- = %d \n", a--);
return 0;
}

//assignment operator
#include <stdio.h>
int main() Output:
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}

//relational operators
#include <stdio.h>
int main() Output:
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}

//logical operators
#include <stdio.h>
int main()
{ Output:
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}

//bitwise operator
#include <stdio.h>
int main() { Output:
int a = 12, b = 25, c=-22;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("~c = %d\n", ~c);
printf("Right shift by 1 : %d\n", a >> 1);
printf("Left shift by 1: %d\n", b << 1);
return 0;
}

//conditional operator
#include <stdio.h>
int main() Output:
{
int age;
printf("Enter your age");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting"))
: (printf("not eligible for voting"));
return 0;
}
//special operator(size of)
#include <stdio.h>
int main() Output:
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}
//even or odd
#include <stdio.h>
int main() Output:
{
int num;
printf("enter a number");
scanf("%d",&num);
if(num%2==0)
{
printf(" %dis even number",num);
}
else
{
printf("%d is odd number",num);
}
return 0;
}
//simple if example
#include <stdio.h>
int main() Output:
{
int num=3;
if(num==3)
printf("entered value is 3");
}
//leap year
#include <stdio.h>
void main () Output:
{
int year;
printf("enter a year:");
scanf("%d",&year);
if(year%4==0)
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year");
}
}

// largest number between two numbers


#include <stdio.h>
void main () {
inta,b; Output:
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
if(a>b)
printf("%d is largest",a);
else
printf("%d is largest",b);
}

//largest among three numbers


//method-1(nested if else)
#include <stdio.h>
void main () { Output:
inta,b,c;
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
printf("enter c value:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
printf("%d is largest",a);
else
printf("%d is largest",c);
}
else
{
if(b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);
}
}

//method -2(else if)


#include <stdio.h>
void main () { Output:
inta,b,c;
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
printf("enter c value:");
scanf("%d",&c);
if(a>=b&&a>=c)
printf("%d is largest",a);
else if(b>=c&&b>=a)
printf("%d is largest",b);
else
printf("%d is largest",c);

}
//vowel or consonant
#include <stdio.h>
int main()
{
char ch;
Output:
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}

//simple calculator
#include<stdio.h>
int main ( ){ Output:
int a,b;
char ch;
printf("\n+ \n- \n* \n/\n%% ");
printf("\nenter any operator ");
scanf("%c",&ch);
printf("\nenter a,b values");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+' :printf("\nsum=%d",a+b);
break;
case '-' :printf("\ndiff=%d",a-b);
break;
case '*' : printf("\nmul=%d",a*b);
break;
case '/' : printf("\nquo=%d",a/b);
break;
case '%' : printf("\nrem=%d",a%b);
break;
default : printf(" invalid operator ");
break;
}
return 0;
}

//days of a week
#include <stdio.h>
int main()
{
int day;
printf("Enter number 1-7 :");
scanf("%d", &day);
switch (day)
{
case 1: printf("Sunday");
break; Output:
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("Invalid input");
}
return 0;
}
//factorial of a number
#include<stdio.h>
int main() {
int n,i,f=1; Output:
printf("enter the number");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
f = f * i;
}
printf("factorial of %d is %d",n,f);
return 0;
}
//sum of n natural numbers
#include<stdio.h>
int main() {
int n,i,s=0; Output:
printf("enter the number");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
s = s + i;
}
printf("sum of first %d natural numbers is %d",n,s);
return 0;
}

//all factors of a given number


#include <stdio.h>
int main() {
int num, i; Output:
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}

//sum of digits in a number


#include <stdio.h>
int main() {
int n, sum = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n); Output:
while (n != 0)
{
remainder = n % 10;
sum = sum+ remainder;
n /= 10;
}
printf("sum of digits = %d", sum);
return 0;
}

//fibonacci series
#include<stdio.h>
int main()
{
int t1=0,t2=1,t3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%4d%4d",t1,t2);
for(i=2;i<number;i++)
{ Output:
t3=t1+t2;
printf("%4d",t3);
t1=t2;
t2=t3;
}
return 0;
}

//prime number
#include<stdio.h>
int main()
{
int n,i,count=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
Output:
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count=1;
break;
}
}
if(count == 0)
printf("Number is prime");
else
printf("Number is not prime");
return 0;
}

//prime number between n1 and n2


#include<stdio.h>
int main() {
int n1,n2,n,i,count=0;
printf("Enter the range to check :");
scanf("%d%d",&n1,&n2);
printf("prime numbers between %d and %d:",n1,n2);
for(n=n1;n<=n2;n++) { Output:
for(i=2;i<=n/2;i++) {
if(n%i==0)
{
count=1;
break;
}
}
if(count == 0)
printf("\n %d is prime",n);
count = 0;
}
return 0;
}
//multiplication table
#include <stdio.h> Output:
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 5; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
//reverse of a number
#include <stdio.h>
int main() {
int n, reverse = 0, remainder; Output:
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
//palindrome
#include <stdio.h>
int main()
{ Output:
int n, reverse = 0, remainder,p;
printf("Enter an integer: ");
scanf("%d", &n);
p=n;
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
if(p==reverse)
printf("%d is palindrome", p);
else
printf("%d is not palindrome", p);
return 0;
}

//Armstrong number for three-digits


#include <stdio.h>
int main()
{
int num, n, r, result = 0;
printf("Enter a three-digit integer: "); Output:
scanf("%d", &num);
n = num;
while (n != 0)
{
r = n % 10;
result += r * r * r;
n = n/10;
}
if (result == num)
{
printf("%d is an Armstrong number.", num);
}
else
{
printf("%d is not an Armstrong number.", num);
}
return 0;
}

//febinacci
#include <stdio.h>
int main() { Output:
int i, n;
int t1 = 0, t2 = 1,t3;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d %d ", t1, t2);
for (i = 3; i <= n; ++i) {
t3 = t1 + t2;
printf("%d ",t3);
t1 = t2;
t2 = t3;
}
return 0;
}

//do while example


#include <stdio.h>
int main() { Output:
int i=1;
do{
printf("%d ",i);
i++;
}while(i<=5);
return 0;
}

//goto example
#include <stdio.h>
int main() { Output:
int i=1;
label: printf("%d ",i);
i++;
if(i<=5)
goto label;
return 0;
}

//continue
#include <stdio.h>
int main() { Output:
int i;
for(i=1;i<=10;i++){
if(i==5)
continue;
printf("%d ",i);
}
return 0;
}

//break
#include <stdio.h> Output:
int main() {
int i;
for(i=1;i<=10;i++){
if(i==5)
break;
printf("%d ",i);
}
return 0;
}

//patterns
#include <stdio.h> Output:
int main()
{
int i, j, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() Output:
{
int i, j, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main()
{
int i, j, rows; Output:
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main(){ Output:
int i, j, rows,n=1;
//also known asFloyd's Triangle.
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d\t",n++);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Enter the no. of rows: "); Output:
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main()
{
int i, j, k, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{ Output:
for (j = 1; j <= rows - i; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int i, j, k, rows;
printf("Enter the no. of rows: "); Output:
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
printf(" ");
for (k = 1; k <= i; k++)
printf("%d ",k);
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the no. of rows: ");
scanf("%d", &rows); Output:
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main()
{
int i, j,k, rows; Output:
printf("Enter the no. of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--)
{
for (j = rows; j > i; j--)
printf(" ");
for (k = 1; k <= i; k++)
printf("* ");
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int i, j, k, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows); Output:
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
printf(" ");
}
for (k=1; k <= (2 * i - 1);k++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h> Output:
void main()
{
int i,j,n;
printf("enter number of rows : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++){
printf(" ");
}
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++){
printf("%d ",j);
}
/* Display number in reverse order after middle */
for(j=i-1;j>=1;j--){
printf("%d ",j);
}
printf("\n");
}
}
// accepting elements into an array and displaying them
#include <stdio.h>
int main() Output:
{
inti, values[5];
printf("Enter 5 integers: ");
for( i = 0; i< 5; ++i)
{
scanf("%d", &values[i]);
}
printf("Displaying integers:\n");
for( i = 0; i< 5; ++i)
{
printf("%d\n", values[i]);
}
return 0;
}

//finding largest number in an array


#include <stdio.h> Output:
int main()
{
inti,l, values[5];
printf("Enter 5 integers: ");
for( i = 0; i< 5; ++i)
{
scanf("%d", &values[i]);
}
l=values[0];
for( i = 0; i< 5; ++i)
{
if(l<values[i])
l=values[i];
}
printf("%d is the largest element in the array\n", l);
return 0;
}

//finding smallest number in an array


#include <stdio.h>
int main() Output:
{
inti,s, values[5];
printf("Enter 5 integers: ");
for( i = 0; i< 5; ++i)
{
scanf("%d", &values[i]);
}
s=values[0];
for( i = 0; i< 5; ++i)
{
if(s>values[i])
s=values[i];
}
printf("%d is the smallest element in the array\n", s);
return 0;
}

//sum of elements in an array


#include <stdio.h>
int main() Output:
{
int values[5],i,sum=0;
printf("Enter 5 integers: ");
for( i = 0; i< 5; ++i)
{
scanf("%d", &values[i]);
}
for( i = 0; i< 5; ++i)
{
sum += values[i];
}
printf("sum of elements :%d ",sum);
return 0;
}

//arranging elements in an array in ascending order


#include <stdio.h>
int main()
{ Output:
inti, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);

printf("Enter %d numbers \n",n);


for (i = 0; i< n; ++i)
scanf("%d", &number[i]);
for (i = 0; i< n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order\n");
for (i = 0; i< n; ++i)
printf("%d\n", number[i]);
}

//arranging elements in an array in descending order


#include <stdio.h>
int main()
{ Output:
inti, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);

printf("Enter %d numbers \n",n);


for (i = 0; i< n; ++i)
scanf("%d", &number[i]);
for (i = 0; i< n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order\n");
for (i = 0; i< n; ++i)
printf("%d\n", number[i]);
}
//deleting duplicate elements in an array
#include <stdio.h>
#include <conio.h>
int main ()
{
int arr[20], i, j, k, size;
printf (" enter the number of elements in an array: ");
scanf ("%d", &size);
printf (" \n Enter %d elements of an array: \n ", size);
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);
}
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
if ( arr[i] == arr[j])
{
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
size--;
j--;
}
}
}
printf (" \n Array elements after deletion of the duplicate elements: ");
for ( i = 0; i < size; i++)
{
printf (" %d\t", arr[i]);
}
return 0;
}
Output:
//transpose of a matrix
#include <stdio.h>
int main() {
int r, c, a[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("\n Given matrix: \n");
for (i = 0; i< r; ++i) Output:
{
for (j = 0; j < c; ++j)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n transpose of a matrix: \n");
for (i = 0; i< c; ++i)
{
for (j = 0; j < r; ++j)
{
printf("%d ", a[j][i]);
}
printf("\n");
}
return 0;
}

// trace of a matrix
#include <stdio.h>
int main() {
int r, c, a[100][100], i, j,sum=0;
printf("Enter the number of rows & columns (between 1 and 100): ");
scanf("%d", &r);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < r; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("\n Given matrix: \n");
for (i = 0; i< r; ++i) Output:
{
for (j = 0; j < r; ++j)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (i = 0; i< r; ++i)
{
for (j = 0; j < r; ++j)
{
if(i == j)
sum += a[i][j];
}
}
printf("\n trace of a matrix: %d \n",sum);
return 0;
}

//addition of two matrices


#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of two matrices: \n");
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
}
Printf(“\n”);
}
return 0;
}
Output:

//subtraction of matrices
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], diff[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i< r; ++i){
for (j = 0; j < c; ++j) {
diff[i][j] = a[i][j] - b[i][j];
}
}
printf("\n difference between two matrices: \n");
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", diff[i][j]);
}
printf("\n");
}
return 0;
}
//multiplication of matrices
#include<stdio.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r1,r2,c1,c2,i,j,k;
printf("enter the number of row=");
scanf("%d",&r1);
printf("enter the number of column=");
scanf("%d",&c1);
printf("enter the first matrix element=\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the number of row=");
scanf("%d",&r2);
printf("enter the number of column=");
scanf("%d",&c2);
printf("enter the second matrix element=\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
if(c1==r2) {
printf("multiply of the matrix=\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mul[i][j]=0;
for(k=0;k<c1;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
else
{
printf("Multiplication of matrices is not possible");
}
return 0;
}
//3-d array
#include <stdio.h> Output:
int main()
{
int test[2][3][2];

printf("Enter 12 values: \n");

for (inti = 0; i< 2; ++i)


{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
}
}
}
printf("\nDisplaying values:\n");
for (inti = 0; i< 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}

//finding length of a string without using string handling functions


#include <stdio.h>
int main() {
char s[100]; Output:
int i,count=0;
printf("Enter a string: ");
gets (s);
for (i = 0; s[i] != '\0'; ++i)
{
count++;
}
printf("Length of the string: %d", count);
return 0;
}
// reverse of a string without using string handling functions
#include <stdio.h>
int main() {
char s[100];
int i,count=0; Output:
printf("Enter a string: ");
gets (s);
for (i = 0; s[i] != '\0'; ++i) {
count++;
}
printf("Reversed string: ");
for (i=count-1;i>=0;i--) {
printf("%c",s[i]);
}
return 0;
}

//string palindrome
//method-1
#include <stdio.h>
int main() {
char s[100],c[100];
int i,count=0,flag=0,j;
printf("Enter a string: ");
gets (s);
for (i = 0; s[i] != '\0'; ++i)
{
count++;
}
for (j=0,i=count-1;i>=0;i--,j++)
{ Output:
c[i] = s[j];
}
for (i = 0; s[i] != '\0'; ++i)
{
if(c[i]!= s[i])
{
flag++;
break;
}
}
if(flag==0)
printf("palindrome");
else
printf("not palindrome");
return 0;
}

//method-2
int main() {
char s[100];
int i,count=0,flag=0,j;
printf("Enter a string: ");
gets (s); Output:
for (i = 0; s[i] != '\0'; ++i)
{
count++;
}
for (j=0,i=count-1;j<=count/2;i--,j++)
{
if(s[j]!= s[i])
{
flag++;
break;
}
}
if(flag==0)
printf("palindrome");
else
printf("not palindrome");
return 0;
}

//string comparison without using string handling functions


#include <stdio.h>
int main() {
char s[100],c[100];
int i,count=0,flag=0,j; Output:
printf("Enter a string: ");
gets (s);
printf("Enter another string: ");
gets (c);
for (i = 0; s[i] != '\0' || c[i] !='\0'; ++i)
{
if(c[i]!= s[i])
{
flag++;
break;
}
}
if(flag==0)
printf("both strings are same");
else
printf("both strings are different");
return 0;
}

//string handling functions


//strlen()
#include<stdio.h> Output:
#include <string.h>
int main(){
char str[20];
printf("enter a string : ");
gets(str);
printf("Length of string is: %d",strlen(str));
return 0;
}

//strcpy
#include<stdio.h>
#include <string.h> Output:
int main(){
char str1[20],str2[20];
printf("enter a string : ");
gets(str1);
strcpy(str2,str1);
printf("Value of second string is: %s",str2);
return 0;
}

//strncpy
#include<stdio.h>
#include <string.h> Output:
int main(){
char str1[20],str2[20];
printf("enter a string : ");
gets(str1);
strncpy(str2,str1,5);
printf("Value of second string is: %s",str2);
return 0;
}
//strcat
#include<stdio.h> Output:
#include <string.h>
int main(){
char str1[20],str2[20];
printf("enter a string : ");
gets(str1);
printf("enter another string : ");
gets(str2);
strcat(str1,str2);
printf("string after concatination is: %s",str1);
return 0;
}

//strncat
#include<stdio.h> Output:
#include <string.h>
int main(){
char str1[20],str2[20];
printf("enter a string : ");
gets(str1);
printf("enter another string : ");
gets(str2);
strncat(str1,str2,5);
printf("string after concatination is: %s",str1);
return 0;
}

//strcmp
#include<stdio.h>
#include <string.h>
int main(){ Output:
char str1[20],str2[20];
printf("enter a string : ");
gets(str1);
printf("enter another string : ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}

//strncmp Output:
#include<stdio.h>
#include <string.h>
int main(){
char str1[20]="daddy",str2[20]="dad";
if(strncmp(str1,str2,3)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}

//strrev Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);
7. printf("String is: %s",str);
8. printf("\nReverse String is: %s",strrev(str));
9. return 0;
}

//strlwr
1. #include<stdio.h> Output:
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nLower String is: %s",strlwr(str));
9. return 0;
}

//strupr
1. #include<stdio.h>
Output:
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nUpper String is: %s",strupr(str));
9. return 0;
}
//program on structures
#include <stdio.h>
#include <string.h>
struct Person {
Output:
char name[50];
int citNo;
float salary;
} person1;
int main() {
strcpy(person1.name, "Mani");
person1.citNo = 5334;
person1. salary = 2500;
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);
return 0;
}

//nested structure
#include <stdio.h>
#include <string.h>
struct Employee
{
int employee_id;
char name[20]; Output:
int salary;
};
struct Organisation
{
char organisation_name[20];
char org_number[20];
struct Employee emp;
};
int main()
{
struct Organisation org;
org.emp.employee_id = 101;
strcpy(org.emp.name, "henry");
org.emp.salary = 40000;
strcpy(org.organisation_name, "Aditya");
strcpy(org.org_number, "404");
printf("Organisation Name : %s\n", org.organisation_name);
printf("Organisation Number : %s\n", org.org_number);
printf("Employee id : %d\n", org.emp.employee_id);
printf("Employee name : %s\n", org.emp.name);
printf("Employee Salary : %d\n", org.emp.salary);
}

//self referential structure


#include <stdio.h>
#include <conio.h>
struct ref
{
int data;
char val; Output:
struct ref* link;
};
int main()
{
struct ref object1;
struct ref object2;
object1.link = NULL;
object1.data = 10;
object1.val = 20;

object2.link = NULL;
object2.data = 30;
object2.val = 40;
object1.link = &object2;
printf ("%d \n", object1.link -> data);
printf ("%d \n", object1.link -> val);
return 0;
}
// finding topper in a class using array of structures and pointers
#include <stdio.h>

struct student {
char id[15];
char name[64];
int m1,m2,m3;
float avg;
};

int main(void)
{
struct student std[10],topper;
struct student *ptr ;
int i,n;
float g;
ptr = std;
printf("Enter no.of students: ");
scanf("%d",&n);
// get detail for user
for (i = 0; i < n; i++) {
printf("Enter detail of student #%d\n", (i + 1));
printf("Enter ID: ");
scanf("%s", ptr->id);
printf("Enter name: ");
scanf("%s", ptr->name);
printf("Enter 3 subjects marks: ");
scanf("%d%d%d",& ptr->m1,&ptr->m2,&ptr->m3);
ptr->avg = (ptr->m1+ptr->m2+ptr->m3)/3;
ptr++; // update pointer to point at next element
}

ptr = std;
g = ptr -> avg; //assigned 1st student average as greatest value
topper = *ptr; // we can write as topper = std[0];
for (i = 0; i < n; i++)
{
printf("\nDetail of student #%d\n", (i + 1));
printf("ID: %s\n", ptr->id);
printf(" Name: %s\n", ptr->name);
printf("m1-%d\tm2-%d\tm3-%d",ptr->m1,ptr->m2,ptr->m3);
printf("Average: %f\n", ptr->avg);
if(g < ptr->avg)
{
topper = std[i];
}
ptr++; // update pointer to point at next element
}

printf("\ntopper details\n");
printf("ID: %s\n", topper.id);
printf("Name: %s\n",topper.name);
printf("m1-%d\tm2-%d\tm3-%d",topper.m1,topper.m2,topper.m3);
printf("\nAverage: %f\n", topper.avg);
return 0;
}
//example program for unions
#include <stdio.h>
#include <string.h>
union data
{
int i; Output:
float f;
char str[20];
};
int main ()
{
union data d;
d.i = 10;
printf ("d.i : %d\n", d.i);
d.f = 220.5;
printf ("d.f : %f\n", d.f);
strcpy (d.str, "C Programming");
printf ("d.str : %s\n", d.str);
return 0;
}
// function with no arguments and no return values
#include<stdio.h>
void add();
int main()
Output:
{
add();
return (0);
}
void add()
{
int a,b;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
printf("sum of %d and %d is %d",a,b,a+b);
}

// function with arguments and no return values


#include<stdio.h>
void add(int x ,int y);
int main()
Output:
{
int a,b;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
add(a,b);
return (0);
}
void add(int x ,int y)
{
printf("sum of %d and %d is %d",x,y,x+y);
}

// function with no arguments and with return values


#include<stdio.h>
int add();
int main()
Output:
{
int c;
c=add();
printf("sum is %d",c);
return (0);
}
int add()
{
int a,b;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
return (a+b);
}

// function with arguments and return values


#include<stdio.h> Output:
int add(int x,int y);
int main()
{
int a,b,c;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
c=add(a,b);
printf("sum is %d",c);
return (0);
}
int add(int x,int y)
{
int z;
z=x + y;
return (z);
}

//passing array to functions


#include <stdio.h>
int calculateSum(int num[]);

int main() {
int result, num[] = {23, 55, 22, 3, 40, 18}; Output:
result = calculateSum(num);
printf("Result = %d", result);
return 0;
}
int calculateSum(int num[]) {
int sum = 0;
for (int i = 0; i < 6; ++i)
{
sum += num[i];
}
return sum;
}

//factorial using recursion(method-1)


#include<stdio.h>
long fact(int n)
{
Output:
if (n == 0)
return 1;
else
return(n * fact(n-1));
}
int main()
{
int number;
long fac;
printf("Enter a number: ");
scanf("%d", &number);
fac = fact(number);
printf("Factorial of %d is %ld\n", number, fac);
return 0;
}

//factorial using recursion(method-2)


#include<stdio.h>
long fact(int n)
{
Output:
static int f=1;
if (n > 0)
{
f = f * n;
fact(n-1);
}
else
return f ;
}
int main()
{
int number;
long fac;
printf("Enter a number: ");
scanf("%d", &number);
fac = fact(number);
printf("Factorial of %d is %ld\n", number, fac);
return 0;
}

// febinacci series using recursion(method-1)


#include <stdio.h>
int fibonacci(int num)
{
if (num == 0)
{ Output:
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
int main()
{
int num;
printf("Enter the number of terms : ");
scanf("%d", &num);
int i;
for (i = 0; i < num; i++)
{
printf("%d, ", fibonacci(i));
}
return 0;
}

// febinacci series using recursion(method-2)


#include<stdio.h>
int t1=0,t2=1,t3;
void feb(int n);
Output:
void main()
{
int n;
printf("Enter no.of terms: ");
scanf("%d",&n);
printf("%d\t%d\t",t1,t2);
feb(n-2);
}
void feb(int n)
{
if(n>0)
{
t3=t1+t2;
printf("%d\t",t3);
t1=t2;
t2=t3;
feb(n-1);
}
}

//call by value
#include <stdio.h>
void swap(int a, int b);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values, in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b) Output:
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping a = %d, b = %d\n",a,b);
}

//call by reference
#include <stdio.h>
void swap(int *x, int *y);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values, in main a = %d, b = %d\n",a,b);
}
void swap (int *x, int *y) Output:
{
int temp;
temp = *x;
*x=*y;
*y=temp;
}

//malloc example
#include <stdio.h> Output:
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; ++i) {
scanf("%d",& ptr[i]);
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
return 0;
}

//calloc example
#include <stdio.h> Output:
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; ++i) {
scanf("%d",& ptr[i]);
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
return 0;
}

//realloc example Output:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n,num,i;
printf("Enter number of elements:");
scanf("%d",&n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; ++i) {
scanf("%d",& ptr[i]);
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
printf("\n\nEnter the new size of the array: \n");
scanf("%d",&num);
ptr = realloc(ptr, num * sizeof(int));
printf("Memory successfully re-allocated using realloc.\n");
printf("Enter elements:\n", n);
for (i = n; i < num; ++i) {
scanf("%d",& ptr[i]);
}
printf("The elements of the array are: ");
for (i = 0; i < num; ++i) {
printf("%d, ", ptr[i]);
}
return 0;
}
//pointers
#include <stdio.h>
void example()
{
Output:
int var = 20;
int* ptr;
ptr = &var;
printf("Value at ptr = %p \n", ptr);
printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
}
int main() {
example();
return 0;
}
//storage classes
//auto
#include <stdio.h>
int main() Output:
{
auto int i=2;
{
auto int i=3;
printf("\n %d",i);
}
printf("\n %d",i);
return 0;
}
//static
#include <stdio.h>
void example();
int main()
{
example(); Output:
example();
example();
return 0;
}
void example()
{
static int i=5;
printf("\n %d",i);
i++;
}
//extern
#include <stdio.h>
extern int i=5;
void example();
Output:
int main()
{
i ++;
example();
printf("\n %d",i);
return 0;
}
void example()
{
printf("\n %d",i);
i++;
}

//register
#include <stdio.h>
int main() Output:
{
register int i;
for(i=1;i<=5;i++)\
{
printf(" %d\n",i);
}
}
//writing into a text file
#include <stdio.h>
#include <conio.h> Output:
int main()
{
char str[1000];
FILE *fptr;
fptr = fopen("program.txt","w");
printf("Enter text: ");
gets(str);
fputs(str,fptr);
fclose(fptr);
return 0;
}

//copying files
#include <stdio.h>
int main()
{
FILE *fptr1, *fptr2;
char c; Output:
fptr1 = fopen("sample.txt", "r");
// Open another file for writing
fptr2 = fopen("filename.txt", "w");
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied ");
fclose(fptr1);
fclose(fptr2);
return 0;
}
//reading contents in file
#include <stdio.h>
int main()
{
Output :
FILE *fptr1;
char c;
fptr1 = fopen("sample.txt", "r");
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
printf("%c",c);
c = fgetc(fptr1);
}
fclose(fptr1);
return 0;
}
//even or odd in files
#include <stdio.h>
int main()
{
FILE *f1, *f2 ;
int i;
f1 = fopen("ODD", "w");
f2 = fopen("EVEN", "w");
for(i = 1; i <= 30; i++)
{
if(i %2 == 0)
putw(i, f2);
else
putw(i, f1);
}
fclose(f1);
fclose(f2);
f1 = fopen("ODD","r");
f2 = fopen("EVEN", "r");
printf("\nContents of ODD file");
while((i = getw(f1)) != EOF){
printf("%4d", i);
}
printf("\nContents of EVEN file");
while((i = getw(f2)) != EOF){
printf("%4d", i);
}
fclose(f1);
fclose(f2);
return 0;
}

Output:

//macro
#include<stdio.h>
#define AGE_LIMIT 18 Output:
#define AGE 20
int main()
{
#if AGE >= AGE_LIMIT
printf("Eligible for vote");
#else
printf("Not eligible for vote");
#endif
return 0;
}

You might also like