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

Notes: Source Codes For Different C Programs

Uploaded by

damner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Notes: Source Codes For Different C Programs

Uploaded by

damner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

/* find if character is alphabet or number or special character */

#include<stdio.h>
int main()
{
char ch;
printf("enter a character\n");
scanf("%c",&ch);
if((ch>+'a' && ch<+'z')||(ch>='A' && ch<='Z'))
{
printf("character is an alphabet\n");
}
else if(ch>='0' && ch<='9')
{
printf("character is a number\n");
}
else
{
printf("it is a special character\n");
}
}

/* program to find area of circle given radius */

#include<stdio.h>
int main()
{
int radius;
float area;
printf("enter the radius of a circle:\n");
scanf("%d",&radius);
area=3.142*radius*radius;
printf("area of a circle=%f", area);
return 0;
}

/* find area of triangle given 3 sides */

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
double S,area;
printf("enter the value for a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
S=(a+b+c)/2;
area=sqrt(S*(S-a)*(S-b)*(S-c));
printf("area of a triangle=%f", area);
return 0;
}

/* calculate simple interest and compound interest */


#include<stdio.h>
#include<math.h>
int main()
{
float P,T,R,SI,CI;
printf("enter principal amount, year, and rate of interest \n");
scanf("%f%f%f",&P,&T,&R);
SI=(P*T*R)/100;
CI=P*(pow(1+R/100,T)-1);
printf("simple interest=%0.3f \ncompound interest=%0.3f\n",SI,CI);
}

/* convert temperature in fahrenheit to celcius and celcius to fahrenheit */

#include<stdio.h>
int main()
{
float celcius,fahrenheit;
printf("enter the temperature in fahrenheit;\n");
scanf("%f",&fahrenheit);
celcius=(fahrenheit-32)/1.8;
printf("temperature in celcius:%0.2f\n",celcius);
printf("enter the temperature in celcius;\n");
scanf("%f", &celcius);
fahrenheit=(1.8*celcius)+32;
printf("temperature in fahrenheit;%0.2f\n",fahrenheit);
}

/* find GCD and LCM of 2 integers */

#include<stdio.h>
int main()
{
int n1,n2,r,lcm,gcd,p,q;
printf("enter the 2 integers;\n");
scanf("%d%d",&n1,&n2);
p=n1,q=n2;
while(n2= 0)
{
r=n1%n2;
n1=n2;
n2=r;
}
gcd=n1;
lcm=(p*q/gcd);
printf("gcd=%d\n",gcd);
printf("lcm=%d\n",lcm);
}

/* check whether a number is even or odd */

#include<stdio.h>
int main()
{
int n,remainder;
printf("enter an integer to check if it is even or odd\n");
scanf("%d", &n);
remainder=n%2;
if(remainder==0)
{
printf("the number is even\n");
}
else
{
printf("the number is odd\n");
}
}

/* program to accept 2 integers and determine in which quadrant it lies using if


ladder */

#include<stdio.h>
int main()
{
int x,y;
printf("enter the value of x and y\n");
scanf("%d%d",&x,&y);
if(x>0 && y>0)
printf("point(%d%d) lies in the first quadrant\n",x,y);
else if(x<0 && y>0)
printf("point(%d%d) lies in the second quadrant\n",x,y);
else if(x<0 && y<0)

printf("point(%d%d) lies in the third quadrant\n",x,y);

else if(x==0 && y==0)

printf("point(%d%d) lies in the origin quadrant\n",x,y);


}

/* simulate a simple calculator with addition, subtraction, multiplication,


division and it should display the error message for zero using switch case */

#include<stdio.h>
int main()
{
char operator;
float n1,n2,result=0;
printf("simulation of simple calculator\n");
printf("enter the operator[+,-,*,/]\n");
scanf("%c",&operator);
printf("enter the 2 numbers\n");
scanf("%f%f",&n1,&n2);
switch(operator)
{
case'+':result=n1+n2;
break;
case'-':result=n1-n2;
break;
case'*':result=n1*n2;
break;
case'/':result=n1/n2;
break;
default:printf("error in operation\n");
break;
}
printf("\n%0.2f %c %0.2f=%0.2f\n", n1,operator,n2,result);
}

/* print all the numbers divisible by 7 between 100 to 200 */

#include<stdio.h>
int main()
{
int i,sum=0,count=0;
printf("program to print number from 100 to 200 divisible by 7\n");
for(i=100;i<200;i++)
{
printf("%5d",i);
sum+=i;
count++;
}
printf("\nsum=%d\n",sum);
printf("\ncount=%d\n",count);
}

/* reverse an integer and check it is palindrome or not */

#include<stdio.h>
int main()
{
int n1,n2,rem,rev=0;
printf("enter a integer\n");
scanf("%d",&n1);
n2=n1;
while(n1!=0)
{
rem=n1%10;
rev=rev*10+rem;
n1=n1/10;
}
printf("reverse of a number=%d\n",rev);
if(n2==rev)
printf("print the given in a palindrome\n");
else
printf("the given integer is not a palindrome\n");
}

print the following pattern

a)* * * * * b) 1
* * * * 1 2
* * * 1 2 3
* * 1 2 3 4
* 1 2 3 4 5

/* program a) /*

#include<stdio.h>
int main()
{
int i,j;
char ch='*';
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%c",ch);
}
printf("\n");
}
}

/* program b) */

#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}

/* find the sum of positive numbers,sum of negative numbers and average of all the
numbers */

/* find the sum of positive numbers,sum of negative numbers and average of all the
numbers */

#include<stdio.h>
int main()
{
int a[10],n,i,psum=0,nsum=0,average;
float sum;
printf("enter the array size:\n");sum;
scanf("%d",&n);
printf("enter %d elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
nsum+=a[i];
}
else
{
sum+=a[i];
}
}
printf("sum of all positive numbers:%d\n");psum;
printf("sum of all negative numbers:%d\n");nsum;
average=(psum+nsum)/2;
printf("average of all numbers:%d\n",average);
}
/* calculate factorial of a number using function */

#include<stdio.h>
int main()
{
int fact(int n);
int n,factorial;
printf("enter the number\n");
scanf("%d",&n);
factorial=fact(n);
printf("factorial of a given number =%d\n",factorial);
}
int fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}

/* find the length of a string using user defined function */

#include <stdio.h>
#include <string.h>
int findlength()
{
char str[100];
int i;
printf("Enter the string");
scanf("%s", str);
for (i = 0; str[i] != '\0'; ++i);
return i;
}
int main()
{
int l=findlength();
printf("Length of the string is %d", l);
return 0;
}

/* program to implement array using pointers */

#include<stdio.h>
int main()
{
int i,data[5];
printf("enter the elements\n");
for(i=0;i<5;++i)
{
scanf("%d",data+i);
}
printf("you entered:\n");
for(i=0;i<=5;++i)
{
printf("%d\n",*(data+i));
}
return 0;
}

Excluded
program to find addition and subtraction of 2 matrices

#include<stdio.h>
int main()
{
int a[10][10],b[10][10],sum[10][10],diff[10][10];
int i,j,n;
printf("enter the size of matrix:\n");
scanf("%d",&n);
print("enter the elements of matrix A;\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
print("enter the elements of matrix B;\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
diff[i][j]=a[i][j]-b[i][j];
}
}
printf("addition of matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", sum[i][j]);
}
printf("\n")
}
printf("difference of matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", diff[i][j]);
}
printf("\n")
}
}

count the number of vowels, consonants and special characters


#include<stdio.h>
int main()
{
char str[50];
int i,vowels,consonants,special,spaces,digits;
vowels=consonants=special=spaces=digits=0;
printf("enter a sentence\n");
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||
str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
{
++vowels;
}
else if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
{
++consonants;
}
else if(str[i]>='0' && str[i]<='0')
{
++digits;
}
else if(str[i]==' ')
{
++spaces;
}
else
{
++special;
}
}
printf("vowels=%d",vowels);
printf("\nconsonants=%d",consonants);
printf("\ndigits=%d",digits);
printf("\nwhitespace=%d",spaces);
printf("\nspecial characters=%d",special);
return 0;
}
accept a sentence and convert all the lowercase letter to uppercase and viceversa

#include<stdio.h>
#include<ctype.h>
int main()
{
char ch,str[80];
int i=0;
printf("enter a string\n");
while((ch=getchar())!='\n')
{
if(ch>='A' && ch<='Z')
{
ch=tolower(ch);
}
else if(ch>='a' && ch<='z')
{
ch=toupper(ch);
str[i]=ch;
i++;
}
}
str[i]='\0';
i=0;
printf("the converted string is\n");
while((ch=str[i++])!='\0')
{
putchar(ch);
}
}
accept goods with the number, price and date to purchase finally display them(using
structure)

#include<stdio.h>
int main()
{
struct goods
{
char name[20];
int no,price;
char date[20];
};
struct goods display[10];
int i;
printf("enter the list of goods\n");
for(i=1;i<=3;i++)
{
printf("enter the goods name\n");
scanf("%s",&display[i].name);
printf("enter the goods price\n");
scanf("%d",&display[i].price);
printf("enter the goods number\n");
scanf("%d",&display[i].no);
printf("enter the goods date\n");
scanf("%s",&display[i].date);
}
printf("****list of goods****\n");
printf("name\t price\t no\t data\t \n");
for(i=1;i<=3;i++)
{
printf("%s\t %d\t %d\t %s\n, display[i].name, display[i].price, display[i].no,
display[i].date);
}
}

You might also like