C Programming Codes
13.4K subscribers
139 photos
2 videos
12 links
C Programming Codes || Quizzes || DSA

Learn along with the community

Any queries
admin - @Pradeep_saii
Download Telegram
//Program to perform sum of individual row and column of 3X3 matrix, read the elements of matrix.
#include<stdio.h>
int main()
{int m[3][3],i,j,sr,sc;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("Enter the element at m[%d][%d]:",i,j);
scanf("%d",&m[i][j]);
}
}
printf("The matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{ sr=0,sc=0;
for(j=0;j<3;j++)
{ sr=sr+m[i][j];
sc=sc+m[j][i];
}
printf("Th sum of row %d:%d\n",i,sr);
printf("The sum of column %d:%d\n",i,sc);
}
}
//#array
3👍3🥰1
//Program to do sum of 2 matrices and store the sum in third matrix, read the elements of matrix.
#include<stdio.h>
int main()
{int a[2][3],b[2][3],c[2][3],i,j;
printf("Enter elemnts of first matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{ printf("Enter the element at a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter elemnts of second matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{ printf("Enter the element at b[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{ c[i][j]=a[i][j]+b[i][j];
}
}
printf("The sum of two matices=\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{ printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
//#array
👍51
//Program to read a string and print it.
#include<stdio.h>
int main()
{char name[20];
printf("Enter name:");
gets(name);
puts(name);
return 0;
}
//contact us
//👇👇👇👇
//@c_programmerrr
👍2
// Program to find length of a string using strlen function
#include<stdio.h>
#include<string.h>
int main()
{ int len=0;
char string[100];
printf("Enter string:");
gets(string);
len=strlen(string);
printf("Length of the string is: %d",len);
return 0;
}
👍5😁1
// Program to find length of a string using a loop.
#include<stdio.h>
int main()
{ int len=0,i;
char string[100];
printf("Enter string:");
gets(string);
for(i=0;string[i]!='\0';i++)
{
len++;
}
printf("Length of the string is: %d",len);
return 0;
}
👍3
// Program to concatenate two strings using strcat function.
#include<stdio.h>
#include<string.h>
int main()
{ char s1[50]="C_";
char s2[50]="Programming";
strcat(s1,s2);
printf("Concateated string:\n%s",s1);
return 0;
}
// Program to concatenate two strings using a loop.
#include<stdio.h>
#include<string.h>
int main()
{ int l1,l2,i;
char s1[50]="C_";
char s2[50]="Programming";
l1=strlen(s1);
l2=strlen(s2);
for(i=0;i<=l2;i++)
{
s1[l1+i]=s2[i];
}
printf("Concatenated string:\n%s",s1);
return 0;
}
👍5
// Program to reverse a sring using strrev function.
#include<stdio.h>
#include<string.h>
int main()
{ char str[20]="coding";
strrev(str);
printf("Reversed string:\n%s",str);
return 0;
}
// Program to reverse a sring using a loop.
#include<stdio.h>
#include<string.h>
int main()
{ char str[50];
printf("Enter the string:");
gets(str);
int i,l=strlen(str);
for(i=0;i<l/2;i++)
{
char ch=str[i];
str[i]=str[l-1-i];
str[l-1-i]=ch;
}
printf("Reversed string:\n%s",str);
return 0;
}
👍2
// pointer is a variable which stores the adress of other variable.
// int *p :- p is a pointer variable which stores the adress of any other int type variable
// (&)adress of (*) indirection/dereferencing operator
#include<stdio.h>
void main()
{int a=5,b=6;
int *p,*q; //declaration of pointers p and q
p=&a; // pointer variable p,has the address of variable a
q=&b; // pointer variable q, has the adress of variable b
printf("Value of a:%d\n",*p); //*p will give the value of a i.e *(&a) value at adress of a
printf("Value of b:%d",*q); //*q will give the alue of b i.e *(&b) value at adress of b
}
👍2
//performing addition on pointer.
#include<stdio.h>
void main()
{
int a[5]={5,3,9,7,2};
int *p=&a[0];
printf("Value : %d\n",*p); //Value :5
printf("Address of element:%u\n",p); //Adress of element:6422280
p=p+2; //The pointer will point to a[2]
*p=4; //Now the value of a[2] is modified to 4
printf("Value :%d\n",*p); //Value:9
printf("Address of element:%u\n",p); //Adress of element:6422288
}
👍1
// performing subraction on pointers.
#include<stdio.h>
void main()
{
int a[5]={6,4,2,5,8};
int *p=&a[0];
int *q=&a[4];
printf("p-q=%d\n",p-q); //p-q=-4
*q=15; // now a[4]=15
printf("q-p=%d\n",q-p); //q-p=4
*p=32; //now ,a[0]=32
q=q-3; //now q is pointing to a[1]
printf("value:%d\n",*q); //value:4
p=p+3; //now p is pointing to a[3]
printf("value:%d\n",*p); //value:5
printf("p-q=%d\n",p-q); //p-q=2
}
👍2
//Performing increment and decrement on pointer
#include<stdio.h>
void main()
{
int a[5]={5,4,6,8,3};
int *p=&a[0];
printf("Value:%d\n",*p); // Value:5
p++; // now,p is pointing to a[1]
printf("Value:%d\n",*p); // Value:4
++p; // now p is pointing to a[2]
printf("Value:%d\n",*p); // value:6
p--; // now p is pointing to a[1]
printf("Value:%d\n",*p); // value:4
--p; // now p is pointing to a[0]
printf("Value:%d\n",*p); // Value:5
}
👍3
#include<stdio.h>
void main()
{
int a[7]={4,8,3,6,1,2,7};
int *p=&a[0],*q;
printf("%d\n",*p); //4
printf("%d,%d,%d\n",(*p)++,*p++,*++p); //3,8,8
printf("%d\n",*p); //4
q=p+3; //q is pointing to a[5]
printf("%d\n",*q-3); //-1
printf("%d\n",*--p+5); //13
printf("%d\n",*p+*q); //10
}
// void pointer- A void pointer is a special pointer type that can hold the
// address of any data type.
//dereferencing of void pointer can only be done after typecasting into other data type.
#include<stdio.h>
void main()
{ void *vp;
int a=10;
float b=5.5;
char ch='b';
vp=&a;
printf("%d\n",*(int*)vp);
vp=&b;
printf("%f\n",*(float*)vp);
vp=&ch;
printf("%c",*(char*)vp);
}
👍2
// NULL pointer-NULL pointer is a special value that represents a pointer
// that does not point to any valid memory address.
// dereferencing of a NULL pointer can't be done.
#include<stdio.h>
void main()
{ int *ptr=NULL;
if(ptr==NULL){
printf("ptr is a NULL pointer");
}
else{
printf("ptr is not a NULL pointer ");
}

}
👍3
Wild Pointer-A wild pointer refers to a pointer that is uninitialized or has been assigned an arbitrary value that does not point to a valid memory address
/* Performing sum of two numbers using ,
Function with no arguments and without return type.*/
#include<stdio.h>
void sum(void);
int main()
{
sum();
return 0;
}
void sum()
{
int a,b,sum=0;
a=4;
b=9;
sum=a+b;
printf("sum=%d",sum);
}
👍5
/* Performing sum of two numbers using ,
Function with no arguments and with return type.*/
#include<stdio.h>
int sum(void);
int main()
{ int res;
res=sum();
printf("Sum=%d",res);
return 0;
}
int sum()
{
int a,b,sum=0;
a=10;
b=45;
sum=a+b;
return sum;
}
👍3
/* Performing sum of two numbers using ,
Function with arguments and without return type.*/
#include<stdio.h>
void sum(int,int);
int main()
{ int a,b;
a=42;
b=32;
sum(a,b);
return 0;
}
void sum(int x,int y)
{
int sum=0;
sum=x+y;
printf("Sum=%d",sum);
}
👍3