CP Programs
CP Programs
//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;
}
//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;
//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");
}
}
}
//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;
}
//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;
}
//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;
}
//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;
}
// 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;
}
//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];
//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;
}
//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);
}
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);
}
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;
}
//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;
}
//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;
}