C PROGRAMMING Solutions - SEE COMPUTER SCIENCE 2081
1) [Formula]Write a program in C language to find simple interest where user need to input Principle, Rate and Time.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
float p, t, r, si;
clrscr();
printf("Enter principal: ");
scanf("%f", &p);
printf("Enter time: ");
scanf("%f", &t);
printf("Enter rate: ");
scanf("%f", &r);
si = (p*t*r)/100;
printf("Simple Interest is %f", si);
getch();
}
Write C program to find average number of any three numbers.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
float avg;
clrscr();
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
printf("Enter third number: ");
scanf("%d",&c);
avg= (a+b+c)/3;
printf("Average of three number is %f",avg);
getch();
}
Write a C program to find the average of any two numbers given by the user. (HW)
Write C program to find area of a circle. (HW)
2) Write a program in C language that asks a number and check whether it is odd or even.
(Write a program in C language that asks for an integer value and checks whether it is divisible by 2 or not.)
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
if(n%2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
getch();
}
Write a program in C language that asks for an integer value and checks whether it is divisible by 7 or not.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
if(n%7==0)
{
printf("Divisible by 7");
}
else
{
printf("Not divisible by 7");
}
getch();
Write a C program to check whether the given number is divisible by 3 and 5 or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
if(n%3==0 && n%5==0)
{
printf("Divisible by 3 and 5");
}
else
{
printf("Not- Divisible by 3 and 5");
}
getch();
}
3) Write a C program that asks a number and check whether it is negative, positive or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
if(n>0)
{
printf("Positive number");
}
else if(n<0)
{
printf("Negative number");
}
else
{
printf("Zero number");
}
getch();
}
Write a C program that asks a number and check whether it is negative or positive. (HW)
4) Write a program in C language that asks any two numbers and displays the greatest among them.
Ans:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b;
clrscr();
printf("Enter 1st number:");
scanf("%d ", &a);
printf("Enter 2nd number:");
scanf("%d ", &b);
if(a>b)
{
printf("The greatest number is %d", a);
}
else
{
printf("The greatest number is %d", b);
}
getch();
}
Write a program in C language that asks any three numbers and displays the greatest among them. (HW)
Write a program in C language that asks any two numbers and displays the smallest among them. (HW)
Write a program in C language that asks any three numbers and displays the smallest among them.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter 1st number:");
scanf("%d ", &a);
printf("Enter 2nd number:");
scanf("%d ", &b);
printf("Enter 3rd number:");
scanf("%d ", &c);
if (a < b && a < c)
{
printf("The smallest number is %d", a);
}
else if (b < a && b < c)
{
printf("The smallest number is %d", b);
}
else
{
printf("The smallest number is %d", c);
}
getch();
}
‘OR’
5) [Natural number] Write a program in C language to display first 10 natural numbers. (Write a program in C language to display the series: 1, 2, 3, 4 up to 10th terms.)
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
a=a+1;
}
getch();
}
Write a program in C language to display the series with their sum: 1, 2, 3, 4 up to 10th terms.
(Write a program in C language to display first 10 natural numbers with their sum.)
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
s=s+a;
a=a+1;
}
printf("The sum is %d",s);
getch();
}
Write a program in C language to display the series: 1, 4, 9, 16.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
a=a+1;
}
getch();
}
Write a program in C language to display the series with their sum 1, 4, 9, 16.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
s=s+a;
a=a+1;
}
printf("The sum is %d",s);
getch();
}
Write a program in C language to display the series with their sum 40, 41, 42, 43…….10th terms.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=40,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
s=s+a;
a=a+1;
}
printf("The sum is %d",s);
getch();
}
6) [Even Number] Write a program in C-language to display the series 2,4, 6, 8 up to the 10th term.
(Write a program in C language to display first 10 even numbers.)
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=2;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
a=a+2;
}
getch();
}
Write a program in C-language to display the series with their sum 2,4, 6, 8 up to the 10th term.
(Write a program in C language to display first 10 even numbers with sum.)
Ans:
#include <stdio.h>
#include <stdio.h>
void main()
{
int i,a=2,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
s=s+a;
a=a+2;
}
printf("The sum is %d",s);
getch();
}
Write a program in C language to display the series: 2, 16, 36.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=2;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
a=a+2;
}
getch();
}
Write a program in C language to display the series with their sum 4, 16, 36.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=2,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
s=s+a;
a=a+2;
}
printf("The sum is %d",s);
getch();
}
7) [Odd Number] Write a program in C-language to display the series 1, 3, 5, 7 up to the 10th term.
(Write a program in C language to display first 10 odd numbers.)
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
a=a+2;
}
getch();
}
Write a C program to print the following series 9,7,5,…1. (HW)
Write a program in C-language to display the series with their sum: 1,3, 5, 7 up to the 10th term.
(Write a program in C language to display first 10 odd numbers with sum.)
Ans:
#include <stdio.h>
#include<conio.h>
void main()
{
int i,a=1,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a);
s=s+a;
a=a+2;
}
printf("The sum is %d",s);
getch();
}
Write a program in C language to display the series: 1, 9, 25.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
a=a+2;
}
getch();
}
Write a program in C language to display the series with their sum 1, 9, 25.... up to 10th term.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a=1,s=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d",a*a);
s=s+a;
a=a+2;
}
printf("The sum is %d",s);
getch();
}
Only for A+ Students:
1) Write a program in C language to display reverse of an integer input number (multi-digit number).
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev=0, original;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
original=n;
while(n>0)
{
rem=n%10;
rev=s*10+r;
n=n/10;
}
printf("Reverse of an integer is %d",rev);
getch();
}
Write a program in C language to find whether the given number is palindrome or not.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev=0, original;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
original=n;
while(n>0)
{
rem=n%10;
rev=s*10+r;
n=n/10;
}
printf("Reverse of an integer is %d",rev);
if (original == rev)
printf("The number is a palindrome.");
else
printf("The number is not a palindrome.");
getch();
}
Write a C program to print the sum of digits of a given number. (Write a program in C language to input a multi-digit number and calculate the sum of its individual digits.)
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, r;
s=0;
printf("Enter any number: ");
scanf("%d", &n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("Sum of digits= %d",s);
return 0;
}
2) Write a program in C language to display the odd numbers from 1000 to 500 with their sum.
#include <stdio.h>
#include <conio.h>
void main()
{
int a = 999, s = 0;
clrscr();
while (a >= 500)
{
printf("%d", a);
s = s+a;
a = a-2;
}
printf("The sum of odd numbers from 1000 to 500 is %d", s);
getch();
}
3) Write a program in C language to input a character and check whether it is vowel or consonant.
4) Write a program in C language that asks the value of 3 sides of a triangle then check whether triangle is Scalene or not.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter three sides of a triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a != b && b != c && a != c)
{
printf("The triangle is Scalene.");
} else {
printf("The triangle is not Scalene.\n");
}
getch();
}
2 5) Write a C program to print the series 1,1,2,3,5,8… upto ten terms.(Fibonacci Series)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i;
clrscr();
a=1;
b=1;
for(i=1;i<=10;i++)
{
printf("%d \n", a);
c=a+b;
a=b;
b=c;
}
getch();
}
6) Write a C program to check whether the given number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, c=0;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
for(i=2;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
printf("Prime number");
else
printf("Not-prime number");
getch();
}
7) Write a C program to print the multiplication table of any input number up to tenth terms.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, c=0;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
for(i=1;i<=10;i++)
{
c=n*i;
printf("%d X %d = %d \n", n,i,c);
}
getch();
}
Write a C program to get temperature in Celsius from the user and then print the temperature in fahrenheit. (HW)
Write a C program to calculate distance travelled by a body. (HW)
Write a C program to convert NC (NEPALI currency) into IC (Indian Currency). (HW)
Write a C program to display middle number among three different numbers.
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter any three numbers:\n ");
scanf("%d %d %d" , &a, &b, &c);
if((a>b && a<c) || (a<b && a>c))
printf("The middle number is %d", a);
else if((b>a && b<c) || (b<a && b>c))
printf("The middle number is %d", b);
else
printf("The middle number is %d", c);
return 0;
}
Write a C program to check whether the given year is leap year or not.
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int y;
printf("Enter the year: ");
scanf("%d", &y);
if((y%2==0) && (y%100 !=0) || (y%400==0))
printf("%d is leap year", y);
else
printf("%d is not leap year", y);
return 0;
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home