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

C Program Output

The document contains code snippets of various C programs demonstrating different programming concepts like input/output, conditional statements, loops, functions, arrays, structures, files etc. The programs range from basic programs to calculate sum, average, check prime numbers, swap values etc. to more complex programs involving multi-dimensional arrays, structures, functions and file handling.

Uploaded by

sec23cj037
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)
46 views

C Program Output

The document contains code snippets of various C programs demonstrating different programming concepts like input/output, conditional statements, loops, functions, arrays, structures, files etc. The programs range from basic programs to calculate sum, average, check prime numbers, swap values etc. to more complex programs involving multi-dimensional arrays, structures, functions and file handling.

Uploaded by

sec23cj037
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/ 39

Program:

#include<stdio.h>
int main()
{
int ivar; floatfvar;
char svar[100];
printf("\nEnter the integer value: ");
scanf("%d",&ivar);
printf("\nEnter the float value : ");
scanf("%f",&fvar);
printf(“\nEnter the string value: ");
scanf("%s",&svar);
printf("\n");
printf("\nInteger value is : %d",ivar);
printf("\nFloat value is : %f",fvar);
printf("\nEntered string is : %s",svar);
return0;
}
Program:
/*To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)*/
#include<stdio.h>
#include<math.h>
voidmain()
{
int a,b,c;
floats,area;
printf("enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of a trangle is =%f",area); getch();
}
Program:
#include <stdio.h>
int main()
{
intnum;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);
if(num > 0)
{
printf("Number is POSITIVE");}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}
return 0;
}
Program:
#include <stdio.h>
void main( )
{
int x, y;
printf("Enter the value of x:\n");
scanf("%d",&x);
printf("Enter the value of y:\n");
scanf("%d",&y);
if (x >y )
{
printf("x is Greater");
}
else
{
printf("y is Greater");
}
}
Program:
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers..."); scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
Program:
#include <stdio.h>
void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{

printf("Divisible by both 5 and 8");


}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf(“Enter the Year(YYYY):”);
scanf(“%d”,&year);
if(year%4==0 && year%100!=0||year%400==0)
printf(“\nThe Given year %d is a Leap Year”);
else
printf(“\n The Given year %d is Not a Leap Year”);
getch();
}
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char op;
float num1, num2, result=0.0f;
/*Print welcome message*/
printf("\nWELCOME TO SIMPLE CALCULATOR");
printf(“\n \n”);
printf("\nEnter [number 1] [+ - * /] [number2]\n");
/*Input two numbers and operator from user*/
scanf("%f %c %f", &num1, &op, &num2);
/*Switch the value and perform action based on operator*/
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;

case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator");
}
/*Prints the result*/
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
getch();
}
Program:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d",&num);
originalNum =num;
while (originalNum!= 0)
{
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
//result += pow(remainder, n);
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return0;
}
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\nOdd or Even Number\n”);
printf(“\n\nEnter an integer you want to check:”);
scanf(“%d”,&num);
if((num%2)==0)
{
printf(“%d is an Even number”,num);
}
else
{
printf(“%d is an Odd number”,num);
}
getch();
}
Program:
#include<stdio.h>
#include<conio.h>
voidmain()
{
int i,num;
longint fact=1;
printf(“ENTER A NUMBER: “);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
fact*=i;
}
printf(“THE FACTORIAL OF %d IS %ld”,num,fact);
getch();
}
Program:
#include<stdio.h>
Intmain()
{
int avg = 0,sum =0,x=0,num[4];
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
for (x=0; x<4;x++)
{
sum = sum + num[x];
}
avg = sum/4;
printf("Average of entered number is: %d\n", avg);
return 0;
}
Program:
#include<stdio.h>
int main()
{
int disp[2][3]; int i,j;
for(i=0; i<2; i++)
{
for(j=0;j<3;j++)
{
printf("Enter value for disp[%d][%d]:",i,j);
scanf("%d",&disp[i][j]);
}
}
printf("Two Dimensional array elements:\n");
for( i=0;i<2;i++){ for(j=0;j<3;j++){
printf("%d",disp[i][j]); if(j==2){
printf("\n");
}
}
}
return 0;
}
Program:
#include<stdio.h>
#include<conio.h>
void swap(int*,int *);
void main()
{
int a,b;
printf(“Enter any two number:\n);
scanf(“%d\n%d”,&a,&b);
printf(“\n Before the swapping:a=%d,b=%d”,a,b);
swap(&a,&b);
printf(“\n After the swapping:a=%d,b=%d”,a,b);
getch();
}
void swap(int *p,int *q)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
}
Program:
#include<stdio.h>
#include<conio.h>
int checkPrimeNumber(int n);
int main()
{
int n1,n2,i,flag;
clrscr();
printf(“Enter two positive integers :\n”);
scanf(“%d\n%d”,&n1,&n2);
printf(“Prime numbers between %d and %d are :\n”,n1,n2);
for(i=n1+1;i<n2;++i)
{
//flag will be equal to 1 if i is prime
flag=checkPrimeNumber(i);
if(flag==1)
printf(“%d\n”,i);
}
return 0;
}
//user-defined function to check prime number
int checkPrimeNumber(int n) {
int j,flag=1;
for(j=2;j<n/2;++j)
{
if(n%j==0)
{
flag=0;
break;
}
}
return flag;
}
Program:
#include <stdio.h>
void reverseSentence();
int main()
{
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence()
{
char c;
scanf("%c", &c);
if (c != '\n')
{
reverseSentence();
printf("%c", c);

}
}
Program:
#include<stdio.h>
#define MAX 100
int findMaxElem(int []);
int n;
int main()
{
int arr1[MAX],mxelem,i;
printf("\n\n Function : get largest element of an array :\n");
printf(" \n");
printf(" Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf(" Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i);
scanf("%d",&arr1[i]);
}
mxelem=findMaxElem(arr1);
printf(" The largest element in the array is : %d\n\n",mxelem);
return 0;
}

int findMaxElem(int arr1[])


{
int i=1,mxelem;
mxelem=arr1[0];
while(i< n)
{
if(mxelem<arr1[i])
mxelem=arr1[i];
i++;
}
return mxelem;
}
Program:
#include<stdio.h>
int main()
{
char S1[100]=”Programming “,S2[]=”is awesome”;
int length,j;
length=0;
while(S1[length]!=’\0’)
{
++length;
}
for(j=0;S2[j]!=’\0’;++j,++length)
{
S1[length]=S2[j];
}
printf(“After concantenation:”);
puts(S1);
return 0;
}
Program:
#include <stdio.h>
intmain()
{
char s[1000];
int c =0;
printf("Input a string\n");
gets(s);
while (s[c] !='\0')
c++;
printf("Length of the string: %d\n", c);
return 0;
}
Program:
#include <stdio.h>
#include<conio.h>
intmain()
{
char str[1000], ch;
int i,count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for ( i = 0; str[i] != '\0'; ++i)
{
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
getch();
return 0;
}
Program:
#include <stdio.h>
struct student
{
charfirstName[50];
int roll;
int marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
for (i = 0; i< 5; ++i)
{
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%d", &s[i].marks);
}
printf("Displaying Information:\n\n");
for (i = 0; i< 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %d", s[i].marks);
printf("\n");
}
return 0;
}
Program:
#include<stdio.h>
#define SIZE 50
struct student
{
char name[30];
introllno;
int sub[3];
};
void main()
{
int i, j, max, count, total, n, a[SIZE], ni;
struct student st[SIZE];
clrscr();

printf("Enter how many students: ");


scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i< n; i++)
{
printf("\nEnter name and roll number for student %d : ", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno);
}
/* for loop to read ith student's jth subject*/
for (i = 0; i< n; i++)
{
for (j = 0; j <= 2; j++)
{
printf("\nEnter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]);
}
}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i< n; i++)
{
total = 0;
for (j = 0; j < 3; j++)
{
total = total + st[i].sub[j];
}
printf("\nTotal marks obtained by student %s are %d", st[i].name,total);
a[i] = total;
}
/* (ii) for loop to list out the student's roll numbers who have secured the highest marks in each
subject */
/* roll number who secured the highest marks */

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


{
max = 0;
for (i = 0; i< n; i++)
{
if (max <st[i].sub[j])
{
max = st[i].sub[j];
ni = i;
}
}
printf("\nStudent %s got maximum marks = %d in Subject : %d",st[ni].name, max, j);
}
max = 0;
for (i = 0; i< n; i++)
{
if (max < a[i])
{
max = a[i];
ni = i;
}
}
printf("\n%s obtained the total highest marks.", st[ni].name);
getch();
}
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct person
{
char name[20];
long telno;
};
void appendData()
{
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}
void showAllData()
{
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t==========\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();
}
void findData()
{
FILE *fp;
struct person obj; char name[20];
int totrec=0; clrscr();
fp=fopen("data.txt","r");
printf("*****Display SpecificRecords*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0)
{
printf("\n\nName : %s",obj.name);
printf("\nTelephone No : %ld",obj.telno);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main()
{
char choice;
while(1)
{
clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) exit\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice)
{
case'1' :
//call append record
appendData();
break;
case'2' :
//call find record
findData();
break;
case'3' :
//Read all record
showAllData();
break;
case'4':
exit(1);
}
}
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10];
char name[20];
char balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt; char choice;
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New
Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or
Withdraw\n");
printf("4. Number of
Account Holder Whose
Balance is less than the
Minimum Balance\n");
printf("5. Delete All\n");
printf("6. Stop\n");
printf("Enter your choice :
");
choice=getchar();
switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("acc.dat","a");
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance);
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder Name Balance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t%-20s\t%s\n",acc.no,acc.name,acc.balance);
fclose(fp);
}

break;
case '3':
fflush(stdin);
flag=0;
fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number : ");
gets(ano);
for(pos1=ftell(fp);
fread(&acc,sizeof(acc),1,fp)==1;
pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 for withdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is : %s",acc.balance);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else
{
bal = atof(acc.balance) - atof(amt);
if(bal<0)
{
printf("\nRs.%s Not available in your A/c\n",amt);
flag=2;
break;
}
}
flag++; break;
}

{ }

if(flag==1)
pos2=ftell(fp);
pos = pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");
fclose(fp);
break;
case '4':
fp=fopen("acc.dat","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum Balance
:%d",flag); fclose(fp);
break;
case '5' :
remove("acc.dat");
break;
case '6' :
fclose(fp);
exit(0);
}
printf("\nPress any keytocontinue ");
getch();
} while (choice!='6');
}

You might also like