0% found this document useful (0 votes)
51 views13 pages

1) Swap Two Nos. Using Call by Value /: #Include #Include

The document contains 7 code snippets demonstrating different concepts in C programming: 1) Swapping two integers using call by value and call by reference. 2) Finding the frequency of a character in a string. 3) Checking if a string is a palindrome. 4) Adding two complex numbers using a structure. 5) Passing a structure to a function to add two complex numbers. 6) Defining an array of structures to store records of multiple students. 7) Taking user input to populate an array of structures with student records.

Uploaded by

Veenu Chhabra
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)
51 views13 pages

1) Swap Two Nos. Using Call by Value /: #Include #Include

The document contains 7 code snippets demonstrating different concepts in C programming: 1) Swapping two integers using call by value and call by reference. 2) Finding the frequency of a character in a string. 3) Checking if a string is a palindrome. 4) Adding two complex numbers using a structure. 5) Passing a structure to a function to add two complex numbers. 6) Defining an array of structures to store records of multiple students. 7) Taking user input to populate an array of structures with student records.

Uploaded by

Veenu Chhabra
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/ 13

/* 1) Swap two nos.

using call by value */

#include<stdio.h>

#include<conio.h>

void swap(int a,int b);

main()

int x,y;

printf("\n Enter two nos. : ");

scanf("%d%d",&x,&y);

printf("\n value before swap: x=%d y=%d",x,y);

swap(x,y);

getch();

void swap(int a,int b)

int temp;

temp=a;

a=b;
b=temp;

printf("\n Value after swap: x=%d y=%d",a,b);

/* 2) Swap two nos. using call by reference */

#include<stdio.h>

#include<conio.h>

void swap(int *,int *);

main()

int x,y;

printf("\n Enter two nos. : ");

scanf("%d%d",&x,&y);

printf("\n value before swap: x=%d y=%d",x,y);

swap(&x,&y);

printf("\n value after swap: x=%d y=%d",x,y);

getch();
}

void swap(int *a,int *b)

int temp;

temp=*a;

*a=*b;

*b=temp;

/* 3) C program to find the frequency of a character in a string */

#include<stdio.h>

#include<conio.h>

int main()

char str[1000],ch;

int count=0,i;

printf("\n\t Enter a string: ");


gets(str);

printf("\n\t Enter a character to find it's frequency: ");

scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)

if(ch==str[i])

count++;

printf("\n\t Frequency of %c = %d",ch,count);

getch();

/* 4) C program to check a string is Palindrome or Not */

#include<stdio.h>

#include<conio.h>

#include<string.h>
int main()

char str[20];

int i,len,flag=0;

printf("\n\t Enter a String: ");

scanf("%s",str);

len=strlen(str);

for(i=0;i<len/2;i++)

if(str[i]!=str[len-i-1])

flag=1;

break;

if(flag)

printf("\n\t %s is not a palindrome",str);

else

printf("\n\t %s is palindrome",str);

getch();
}

Run1:

Run2

Run3:

/*5) C program to add two complex numbers using structure */

#include<stdio.h>

typedef struct

float real;

float imag;

}complex;
main()

complex cnum1,cnum2,sum;

printf("Enter real and imaginary part of 1st complex number: ");

scanf("%f%f",&cnum1.real,&cnum1.imag);

printf("Enter real and imaginary part of 2nd complex number: ");

scanf("%f%f",&cnum2.real,&cnum2.imag);

sum.real=cnum1.real+cnum2.real;

sum.imag=cnum1.imag+cnum2.imag;

printf("SUM = %0.2f + i %0.2f",sum.real,sum.imag);

/* 6) C program to add two complex numbers by passing structure to a function */

#include<stdio.h>
typedef struct complex

float real;

float imag;

}complex;

complex add(complex n1,complex n2);

main()

complex n1,n2,result;

printf("\n For 1st complex number: ");

scanf("%f %f",&n1.real,&n1.imag);

printf("\n For 2nd complex number: ");

scanf("%f %f",&n2.real,&n2.imag);

result=add(n1,n2);

printf("SUM = %0.2f + i %0.2f",result.real,result.imag);

}
complex add(complex n1,complex n2)

complex temp;

temp.real=n1.real+n2.real;

temp.imag=n1.imag+n2.imag;

return (temp);

/* 7) Array of structure */

#include<stdio.h>

#include<string.h>

struct student
{

int id;

char name[30];

float percentage;

};

int main()

int i;

struct student record[2];

// First student's record

record[0].id=1;

strcpy(record[0].name,"Raju");

record[0].percentage=86.5;

// Second student's record

record[1].id=2;

strcpy(record[1].name,"Priyanka");

record[1].percentage=90.0;

// Third student's record

record[2].id=3;

strcpy(record[2].name,"Ajay");

record[2].percentage=81.5;
int j;

for(i=0,j=1;i<3;i++,j++)

printf("Records of Student: %d\n",j);

printf("Id: %d\n",record[i].id);

printf("Name: %s\n",record[i].name);

printf("Percentage: %f\n",record[i].percentage);

/* Array of structure2 */

#include<stdio.h>

#include<string.h>
struct student

int id;

char name[30];

float percentage;

};

int main()

int i;int j;

struct student record[2];

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

printf("\n Enter Records: ");

printf("\n Enter Id, Name, Percentage: ");

scanf("%d %s %f",&record[i].id,&record[i].name,&record[i].percentage);

for(i=0,j=1;i<3;i++,j++)

printf("Records of Student: %d\n",j);

printf("Id: %d\n",record[i].id);

printf("Name: %s\n",record[i].name);

printf("Percentage: %f\n",record[i].percentage);
}

You might also like