Thursday, 25 August 2016
Wednesday, 24 August 2016
Problem : Calculate value of sin(x)
// I have made some changes in input output statements so that you dont copy this question in OJ but you can derieve motivation from it.
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
unsigned long long int fact(int a)
{
unsigned long long int f=1;
for(int i=1;i<=a;i++)
{
f=f*i;
}
cout<<"the fact "<<f<<"\n";
return f;
}
int main()
{
double x;
cin>>x;
double radian=3.14159265358979323846264*x/180;
int t=1;
double sum=0.0;
for(int i=1;i<=58;i+=2) //58 is the limit
{
sum=sum+((t)*pow(radian,i)/fact(i));
cout<<sum<<"\n";
t*=-1;
}
cout<<"the final sum "<<sum;
}
Problem : Calculate value of sin(x)
// I have made some changes in input output statements so that you dont copy this question in OJ but you can derieve motivation from it.
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
unsigned long long int fact(int a)
{
unsigned long long int f=1;
for(int i=1;i<=a;i++)
{
f=f*i;
}
cout<<"the fact "<<f<<"\n";
return f;
}
int main()
{
double x;
cin>>x;
double radian=3.14159265358979323846264*x/180;
int t=1;
double sum=0.0;
for(int i=1;i<=58;i+=2) //58 is the limit
{
sum=sum+((t)*pow(radian,i)/fact(i));
cout<<sum<<"\n";
t*=-1;
}
cout<<"the final sum "<<sum;
}
Saturday, 20 August 2016
Friday, 19 August 2016
Problem Statement 3:
Given two equations of lines , find whether they are coincident, parallel or intersecting lines.
For example :
a1*x+b1*y+c1=0
a2*x+b2*x+c2=0
If lines are coincident then print "coincident". If lines are parallel then print "parallel". If lines are
intersecting then print "intersecting".
Input:
First line contains t which specifies the number of test cases. Following t lines contain 2*t space
separated constants as a1,b1,c1 for first line and a2,b2,c2 for second line.
Output:
Print either "coincident","parallel" or "intersecting" based on the given pair of equations of the lines
in each of the t lines.
Constraints:
1<=t<=10000
0.0000001<=a1,b1,c1,a2,b2,c2<=1000000 or -0.0000001>=a1,b1,c1,a2,b2,c2>=-1000000
Sample test cases:
Input:
3
1 2 3
4 8 10
3 4 5
6 8 10
7 4 3
1 4 7
Output:
parallel
coincident
intersecting
Solution:
#include<stdio.h>
int main()
{
int i,test;
float a,b,c,p,q,r;
scanf("%d",&test);
for( i=1;i<=test;i++)
{
scanf("%f %f %f",&a,&b,&c);
scanf("%f %f %f",&p,&q,&r);
if(((a/p) == (b/q)) && ((c/r)==(a/p)))
printf("coincident");
else if (((a/b) == (p/q)))
{
printf("parallel");
}
else
printf("intersecting");
printf("\n");
}
}
Problem Statement 2:
Given three sides of triangle a,b and c, determine if the given triangle is equilateral, scalene or
isoceles triangle.
Constraints :
1<t<100
a, b and c are floating point numbers
0.0<a<1000.0
0.0<b<1000.0
0.0<c<1000.0
Input :
First line specifies the number of test cases t.
Each of the next t lines contains three real numbers denoting a, b and c respectively.
Assume that for all possible triplets of a,b and c, the triangle is always possible.
Output:
Each line contains either of these three strings : “Equilateral Triangle”, “Scalene Triangle”, or
“Isoceles Triangle” depending on type of traingle. Remember OJ is case sensitive.
Sample Input :
4
2 3 3
5 7 7
1 3.0 2.5
2.0 4.5 3.8
4 4 4.0
Sample Output :
Isoceles Triangle
Isoceles Triangle
Solution:
#include<stdio.h>
int main()
{
int i,test;
float a,b,c;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
scanf("%f %f %f",&a,&b,&c);
if ((a+b<=c) || (a+c<=b) || (b+c)<=a)
printf("no triangle")
else if(a==b && b==c)
printf("equileteral triangle\n");
if(a*a+b*b==c*c)
printf("right angled triangle")
else if( ((a==b) || (b==c)) || (a==c))
printf("isoceles triangle\n");
else
printf("scalene triangle\n");
}
}
Problem Statement :
Calculate the area of the triangle T with given breadth “b” and height “h” with precison of two
digits after decimal point.
Constraints :
1<t<100
b, h are floating point numbers
0.0<b<1000.0
0.0<h<1000.0
Input :
First line specifies the number of test cases t.
Each of the next t lines contains two real numbers denoting b and h respectively.
Output:
Each line contains area of triangle with precison upto two digits after decimal point.
Sample Input :
4
2 3
5 7
1 3
23 56.0
Sample Output :
3.00
17.50
1.50
644.00
Solution:
#include<stdio.h>
int main()
{
int i,test;
float b,h;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
scanf("%f %f",&b,&h);
float area=0.5*b*h;
printf("%0.2f\n",area);
}
}
Problem Statement 4 :
Given n, find the sum of cubes of non-negative integers upto n.
Input :
First line specifies the number of test cases 't'. Following lines contain a single integer n.
Output:
In each line print the sum of cubes of non-negative integers upto n.
Constraints :
1<=t<=1000000
0<=n<=500
Time limit : 1s
Example test cases:
Input
5
0
4
2
3
5
Output
0
100
9
36
225
Solution:
#include<stdio.h>
int main()
{
int i,test,n;
scanf("%d",&test);
for( i=1;i<=test;i++)
{
scanf("%d",&n);
int sum=((n*(n+1)/2)*(n*(n+1)/2));
printf("%d\n",sum);
}
}
Problem Statement:
Given a 4 digit positive number n print the digits of that number.
Input:
Input contains a single psitive integer n.
Output:
Print all the four digits of the number n separated by space.
Sample test cases:
Input:
3354
Output:
3 3 5 4
Solution:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
printf("%d ",n/1000);
printf("%d ",(n/100)%10);
printf("%d ",(n/10)%10);
printf("%d ",n%10);
}
Wednesday, 17 August 2016
Practice question for if -else concept :
Comment the question if you are facing any problem with its solution.
Write a C program to find maximum between two numbers.
Write a C program to find maximum between three numbers
.
Write a C program to check whether a number is even or odd.
Write a C program to check whether a year is leap year or not.
Write a C program to check whether a number is negative, positive or zero.
Write a C program to check whether a number is divisible by 5 and 11 or not.
Write a C program to count total number of notes in given amount.
Write a C program to check whether a character is alphabet or not.
Write a C program to input any alphabet and check whether it is vowel or consonant.
Write a C program to input any character and check whether it is alphabet or digit or special character.
Write a C program to check whether a character is uppercase or lowercase alphabet.
Write a C program to input week number and print week day.
Write a C program to input month number and print number of days in that month.
Write a C program to input angles of a triangle and check whether triangle is valid or not.
Write a C program to input all sides of a triangle and check whether triangle is valid or not.
Write a C program to check whether the triangle is equilateral, isosceles or scalene triangle.
Write a C program to find all roots of a quadratic equation.
Write a C program to calculate profit or loss.
Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:
Percentage > 90% : Grade A
Percentage > 80% : Grade B
Percentage > 70% : Grade C
Percentage > 60% : Grade D
Percentage > 40% : Grade E
Percentage < 40% : Grade F
Write a C program to input basic salary of an employee and calculate its Gross salary according to following:
Basic Salary >= 10000 : HRA = 20%, DA = 80%
Basic Salary >= 20000 : HRA = 25%, DA = 90%
Basic Salary >= 30000 : HRA = 30%, DA = 95%
Write a C program to input electricity unit charges and calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Friday, 12 August 2016
Introduction to if -else :
Suppose you go to market with 1000 rs and you have to buy chips ,cold drink , pen ,books.
So let us suppose that you think like a robot.
1. find price of chips.
2. find price of cold drink
3. find price of pen.
4. find price of books.
5. total the price and you remember the total.
6. check the total with amount which you have:
Now time to take a decision .
7. If total is less than 1000(your amount)'
you buy everything and get the change.
8. If total is more than 1000 :
then :
return the books.
now again check the total
if total is less than 1000:
purchase and get change.
if not then dont purchase anything
so this continues....let us write a c program using this.
#include<stdio.h>
int main()
{
int ch.co.p.b;
printf("enter the amount of chips , cold drink, pen books");
scanf("%d %d %d %d", &ch,&co,&p,&b);
int amount=1000;
int total=ch+co+p+b;
int change=0;
// now the comparison started ///////////
if(total<=1000)
{
printf("I will buy all");
change=1000-total;
printf(" I got the change %d",change);
}
else //it means total > 1000
{
printf("i will return the books ");
total=total-b;
if (total <=1000)
{
printf(" I will buy all except books ") ;
change=1000-total ;
printf("i got change %d",change);
}
else
{
printf(" I am leaving and I am angry");
}
}
}
Try reading the code using the flowchart given and comment your problems.
Happy coding.
How to take input from user in c program
Syntax:
scanf("%d %f %c", &a,&b,&c);
Here we have assumed that variable like a,b,c have been declared.
So what this does:
when you run the program , you will be asked three values " in order " , first you will enter value of a then value of b ,value of c .(Remember the order.)
Don't miss the "&" symbol before the variable , we will discuss some time later .
How to use mathematical functions like sin,sqrt .
Remember the first line of our program ,it was
#include<stdio.h> ---actually this is a header file which contains some standard function definition(dont worry about the big word , just remember that you are able to use printf and scanf because of this)
So to use mathematical functions remember to write
#include<math.h>
this contains function definition of mathematical functions.
Eg . sqrt(x) - for taking square root of a number.
sin(x),cos(x) and other trignometric functions.
pow(x,y)-- to calculate values like x raised to power of y.
Imp: These all function will give a floating number as an output( the result will not be integer but it will a decimal number so use %f instead of %d).
eg sqrt(25)=5.0 not 5.
Assignment 1:
//Program 1 to calculate simple interest given p,r,t:
#include<stdio.h>
int main()
{
int p= 100;
int r= 4;
int t= 12;
float simple_interest=(p*r*t)/100; //since we are dividing by 100 we might get decimal value.
printf("%f",simple_interest);
return 0;
}
logic map:(how to think)
assume three value principle ,rate , year
name each of them as different variables
write the mathematical formula and assign it to a variable
print the variable
Program 2:
#include<stdio.h>
int main()
{
int length=4;
int breadth=15;
int area;
area=length*breadth;
printf("%d",area);
}
Sunday, 7 August 2016
Sample program:
#include
#include<stdio.h> ///way to comment header file required
int main() ///main function
{
printf("Hello world"); //// print message on screen
return 0;
}
Explanation:
First line is necessary , it is header file to be included for functions like printf.
Second line is main function .Every C program starts from here .So fixed line in a program.
line 3: { this denotes a block will start from here. Just your task what you need to do will start and } denotes end of the block.
line 4: this will display Hello World on your screen.
return 0 : compulsory line , we will discuss it later.
line 6: } ends the program.
So in sem 1 you will learn this basic principles of c programming:
1.printing
2.taking input
3.if else
4.looping
5.file handling
practicing the syntax is the key so keep practicing the codes given to you.
How to write a c program and run it:
Open gedit.
write the code and save it with .cpp extension
1.open terminal and type---
g++ <yourfilename>.cpp
2.if no error is generated then type:
./a.out
tada, your program runs.
Hello UG1 programmers
Hello friends,
This is a blog purely dedicated to help completed their assignments especially their OJ questions.
Before this we will brush up your skills in c programming.
Hold your nerves, programming is not dreadful thing . If you are feeling the heat, we will help you to reduce the burden .
Follow these basic guidelines:
1. Visit the blog for any query , solution and understand them properly.
2. Don't directly try to copy, paste anything, understand them.
3. Don't take OJ lightly.Submit each and every code . It will fetch you some marks.
Best of luck in coding and keep visiting for OJ solutions and post your queries in comments.
I will post many solutions for every question in OJ assignments.
Subscribe to:
Comments (Atom)