C Introduction Programs Part 1
write a c program to read three
sides of a triangle,calculate and print the perimeter and area usng formatted
IO statements.
#include<stdio.h>
main()
{
int a,b,c;
int s,area,perimeter;
printf(“enter values of a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
s=(a+b+c)/2;
perimeter=a+b+c;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“AREA=%d\nPERIMETER=%d”,area,perimeter);
}
Write a c program to count the
number of words in a sentence entered from the keyboard at run time.
#include<stdio.h>
main()
{
int i=0,count=0;
char str[30];
printf(“enter a sentence:”);
gets(str);
for(i=0;str[i]!=’\0’;i++)
{
if(str[i]==’ ‘||str[i]==’.’
‘||str[i]==’?’ ‘||str[i]==’!’)
count++;
}
printf(“no of words in given sentence=%d”,count);
}
Earth takes a period of revolution of 3155150 seconds.Write a c
program to convert this into number of days ,hours and minutes.
#include<stdio.h>
long seconds;
int days,hours,minutes;
void convert();
void getsec();
void output();
main()
{
getsec();
convert();
output();
}
long notenough4days;
long remainseconds;
long notenough4hours;
void convert()
{
days=seconds/86400;
notenough4days = seconds%86400;
remainseconds = notenough4days;
hours = remainseconds/3600;
notenough4hours = remainseconds%3600;
remainseconds = notenough4hours;
minutes = remainseconds/60;
}
void getsec()
{
printf("Input seconds: ");
scanf("%ld",&seconds);
}
void output()
{
printf("days = %i\n", days);
printf("hours = %i\n", hours);
printf("minutes = %i\n", minutes);
}
/*
output
student@ubuntu:~$ ./a.out
Input seconds: 3155150
days = 36
hours = 12
minutes = 25
*/
Write a c program to read a 6 digit number and print it in reverse
order
#include<stdio.h>
main()
{
long no,s=0;
int i,r;
printf("enter a 6 digit no");
scanf("%ld",&no);
while(no>0)
{
r=no%10;
s=s*10+r;
no=no/10;
}
printf("reverse=%ld",s);
}
/*output
enter a 6 digit no123456
reverse=654321
*/
Factorial of a number
#include<stdio.h>
int main()
{
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);
return 0;
}
Write a c program to read marks scored by 500 students and count
how many have passed(40% required to pass)
Using arrays:
#include<stdio.h>
main()
{
int a[700];
int i,n,count=0;
printf("enter the limit of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter marks of student %d\n",i+1);
scanf("%d",&a[i]);
if(a[i]>40)
count++;
}
printf("\ncount of students passed: %d",count);
}
/*output
enter the limit of students:5
enter marks of student 1
56
enter marks of student 2
56
enter marks of student 3
45
enter marks of student 4
3
enter marks of student 5
5
count of students passed: 3
*/
Without using arrays:
#include<stdio.h>
main()
{
int marks;
int i,n,count=0;
printf("enter the limit of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter marks of student %d\n",i+1);
scanf("%d",&marks);
if(marks>40)
count++;
}
printf("\ncount of students passed: %d",count);
}
Write a program that reads length, width of a rectangle from user
and compute its area perimeter and length of diagonal.
#include<stdio.h>
#include<math.h>
main()
{
float l,b,area,per,len;
printf("enter length and breadth of a rectangle:");
scanf("%f%f",&l,&b);
area=l*b;
per=2*(l+b);
len=sqrt(l*l+b*b);
printf("\n area=%f\n perimeter=%f\ndiagonal length=%f",area,per,len);
}
/*output
enter length and breadth of a
rectangle:18 19
area=342.000000
perimeter=74.000000
diagonal length=26.172504
*/
Comments
Post a Comment