Notes
![]() ![]() Notes - notes.io |
1)write data 2)read & display contents 3)append
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num; FILE *fptr;
fptr = fopen("program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter number to be added to file: n");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
if ((fptr = fopen("program.txt","r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Values on the file are n=%dn", num);
fclose(fptr);
fptr = fopen("program.txt","a");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter number to be appended to file : ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
if ((fptr = fopen("program.txt","r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Values on the file are n=%dn", num);
fclose(fptr);
return 0;
}
2)OPEN TEXT INPUT
COUNT CHARECTERS , WORDS , LINES & STORE OUTPUT
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file; char ch;
int characters, words, lines;
file = fopen("text1.txt","r");
if (file == NULL)
{
printf("nUnable to open file.n");
printf("Please check if file exists and you have read privilege.n");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
if (ch == 'n' || ch == ' ')
lines++;
if (ch == ' ' || ch == 't' || ch == 'n' || ch == ' ')
words++;
}
if (characters > 0)
{
words++;
lines++;
}
printf("n");
printf("Total characters = %dn", characters);
printf("Total words= %dn", words);
printf("Total lines= %dn", lines);
fclose(file);
return 0;
}
String reverse using function
#include <stdio.h>
void stringreverse(char* s)
{
char r[100];
int begin, end, count = 0;
printf("Input a stringn");
scanf("%s",s);
while (s[count] != ' ')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] = ' ';
printf("Reverse of entered string is %sn", r);
}
void main()
{
char s[100];
stringreverse(s);
}
Factorial of a number using loop
#include<stdio.h>
int main()
{
int num, i, fact = 1;
printf("Enter a number to find factorialn");
scanf("%d", &num);
for(i = 1; i <= num; i++)
{
fact = fact * i;
}
printf("Factorial of %d is %dn", num, fact);
return 0;
}
8) EMPLOYEE DETAILS
#include <stdio.h>
struct employee
{
char name[30];
int empid;
float salary;
};
void main ()
{
struct employee emp;
printf("nEnter details:n");
printf("Enter name:");
scanf("%s",emp.name);
printf("enetr ID:");
scanf("%d",&emp.empid);
printf("Enter salary:");
scanf("%f",&emp.salary);
printf("nEntered details is:n");
printf("Name:%sn",emp.name);
printf("ID:%dn",emp.empid);
printf("Salary:%fn",emp.salary);
}
9) PERSONAL DETAILS USING UNION
#include <stdio.h>
#include <string.h>
#define C_SIZE 20
union test
{
char name[C_SIZE];
char house[C_SIZE];
char city[C_SIZE];
char state[C_SIZE];
char pincode[C_SIZE];
};
void main()
{
union test t;
printf("Enter the name of person :n");
scanf("%s",t.name);
printf("The name of the person is : n");
puts(t.name);
printf("Enter the house name of person :n");
scanf("%s",t.house);
printf("The house of the person is : n");
puts(t.house);
printf("Enter the city of person :n");
scanf("%s",t.city);
printf("The city of the person is : n");
puts(t.city);
printf("Enter the state of person :n");
scanf("%s",t.state);
printf("The state of the person is : n");
puts(t.state);
printf("Enter the pincode of person :n");
scanf("%s",t.pincode);
printf("The pincode of the person is : n");
puts(t.pincode);
}
10) StRING REVERSE
#include <stdio.h>
void stringreverse(char* s)
{
char r[100];
int begin, end, count = 0;
printf("Input a stringn:n");
scanf("%s",s);
while (s[count] != '')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] = '';
printf("Reverse of entered string is %sn",r);
}
void main()
{
char s[100];
stringreverse(s);
}
11) FACTORIAL OF NUMBER USING LOOP
#include <stdio.h>
int main()
{
int num, i, fact = 1;
printf("Enter a number to find factorialn");
scanf("%d",&num);
for(i = 1; i <= num; i++)
{
fact = fact * i;
}
printf("Factorial of %d is %dn", num, fact);
return 0;
}
1)SUM USING POINTERS
#include <stdio.h>
int main ()
{
int array [5], i, n, sum = 0,*ptr;
printf (" Enter the number of elements in the array:n");
scanf("%d",&n);
printf ("Enter array elements :n");
for ( i = 0; i<n; i++)
scanf ( "%d",& array [i]);
ptr = array;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
printf("the sum is :%d",sum);
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team