Monday, 3 October 2016

Problem Statement:
finding sum of an array

#include<stdio.h>
int main()
{
        int sum=0,n,a[1000],i;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
              scanf("%d",&a[i]);
               sum=sum+a[i];
          }
         printf("the sum of elements is %d",sum);
return 0;
}


Problem Statement:
finding minimum of an array


#include<stdio.h>
int main()
{
        int i,n,a[1000],min=1000000;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                      scanf("%d",&a[i]);
                            if(a[i]<min)
                                {
                                 min=a[i];
                                  }
           }
            printf("the minimum element is %d\n",min);
}

Problem Statement:
finding whether a number is prime


#include<stdio.h>
#include<math.h>
int main()
{
        int i,count=0,n;
        scanf("%d",&n);
         for(i=2;i<sqrt(n);i++)
          {
                     if(n%i==0)
                      count=1;
                        if(count==1)
                        {
                        break;
                       }
             }

if(count==0)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
}



Problem Statement:
printing pattern
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4


#include<stdio.h>
int main()
{
int i,j,rows,columns,k=1;
printf("enter the number of columns\n");
scanf("%d",&rows);
printf("enter the number of rows\n");

scanf("%d",&columns);
for(i=0;i<columns;i++)
{
for(j=0;j<rows;j++)
{
if(j!=rows-1)
printf("%d ",k);
else
printf("%d",k);  //to neglect last space
}
k++;
printf("\n");
}
}







Friday, 30 September 2016

Ready for Lab exam !!!!!! 

    Gear up !!!!!


Here are some must know questions:


1. Problem on reversal of string or part of string

2. Binary hexa decimal conversion ( all similar conversion)

3. Problem on traversal of an array ( in easy question)

4. General file handling program (simple reading and writing of data )

5. Anagram string 

6. Printing of patterns ( practice diamond printing)

7. Printing armstrong,special  number ( simple follow the logic )

8. Finding frequency of given substring in a given string 

9.Standard problems on recursion like

     gcd,reversing string,printing max and min of array,searching of an element

10. all sorting technique taught.



Practice coding these questions from scratch , once you understand them properly you will be able to solve atleast the 80 marks question.


Please comment if you want anything to be discussed . Help yourself and your friends , learning from peers is the best.

Keep visiting for further updates. More problems will be discussed.

Friday, 2 September 2016

Question: Big integer addition with array of integer as input .


Solution :

// I have used different syntax for ip/op, you can use scanf / printf instead .
//big integer addition program  without test case and array input

#include<stdio.h>

#include<iostream>

using namespace std;


int main()

{

    int a[100],b[100];

    int c[100],m,n;

    cin>>m;

    cin>>n;

    int i,j;

    int newpos=0;

    for( i=0;i<m;i++)

    {

        cin>>a[i];

       

    }

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

    {

        cin>>b[j];

    }

   

    int sum,carry=0;

   

    for( i=m-1,j=n-1;i>=0,j>=0;i--,j--)

    {

        sum=a[i]+b[j]+carry;

        c[newpos++]=sum%10;

        carry=sum/10;

         

    }

    for(;j>=0;j--)

    {

        sum=b[j]+carry;

        c[newpos++]=sum%10;

        carry=sum/10;

       

    }

    for(;i>=0;i--)

    {

        sum=a[i]+carry;

        c[newpos++]=sum%10;

        carry=sum/10;

       

    }


    if(carry>0)

    {

        c[newpos++]=carry;

       

    }

    //reversing the array

    for(i=newpos-1;i>=0;i--)

    {

        cout<<c[i];

    }

    cout<<"\n";


}

Thursday, 1 September 2016

Welcome to the world of writing C program  on piece of paper .

Isn't it funny? 


But you have to do it now in your Mid semester exam.


Some Do's and dont's

 

1. Practice all programs given in tutorials by writing on piece of a paper.

2. Focus on syntax of the language . Never miss out the semicolons.

3. In theory paper marks are divided on both syntax and logic .

4. Always attempt the problem and write your logic even though it may be not correct on the paper.

5. The paper will consist of easy , moderate and difficult questions.

6. Be quick and accurate in easy programs ( these are problems directly related to tutorial problems).

7.Moderate questions are slight modification of easy problems .

8. Don't worry , only 1 or 2 hard questions are there  in which logic part is only tricky.

9.Try writing syntaxes on piece of paper.

 

and AT LAST ONLY HARD WORK PAYS FOR NOW.

 

Bye for now, next post will be about strings and 2 d array .

 

Comment for a topic which you want to discuss. (You can use pseudonames) .




Thursday, 25 August 2016

Problem : Count positive and negative number in an array.

 

 

Solution :


#include<stdio.h>
int main()
{
    int i,j,test,count,n;
    scanf("%d",&test);
    for(i=1;i<=test;i++)
    {
        int a[100];
        int c1=0,c2=0;
        scanf("%d",&n);
        for(j=1;j<=n;j++)
        {
            scanf("%d",&a[i]);
            if(a[i]>0)
            {
                c1++;
            }
            if(a[i]<0)
                c2++;
        }
                printf("%d %d\n",c1,c2);
   
    }
       
}


Problem 3:  Find whether given number is prime or not.

Solution:

#include<stdio.h>

int main()

{

    int i,j,test,count,n;

    scanf("%d",&test);

    for(i=1;i<=test;i++)

    {

        count=0;

        scanf("%d",&n);

        for(j=1;j<=n;j++)

        {

            if(n%j==0)

                count++;

        }

            if(count==2)

            printf("1\n");

            else

            printf("0\n");

    }

}

           

 

Problem 1 : Find a to power n without math library.


Solution :


#include<stdio.h>
int main()
{
    int i,j,test,n,a;
    scanf("%d",&test);
    for(i=1;i<=test;i++)
    {
        scanf("%d",&a);
        scanf("%d",&n);
       
        int product=1;
        for(j=1;j<=n;j++)
        {
            product*=a;
        }
        printf("%d\n",product);
    }
}

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

Some Important Guidelines for posting solutions on OJ:


1. Remember to do exactly what it is asked , dont write extra printf statements.

 

2. Be careful of "\n" usage . Its very important.


3. Give the exact output format.


4. Try looking up for some solutions but avoid copying without understanding.




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<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.