C Programming Tutorial Exercises

Exercise 1

Write a C program that reads a temperature in Fahrenheit and converts that temperature to the same temperature in Celsius. 


Solution 1

Let us use the formula: C/5=(F-32)/9.

#include <stdio.h>

int main()
{
   float F, C;

   printf("Enter a temperature in Fahrenheit : ");
   scanf("%f", &F);
   C = 5 * (F - 32) / 9;
   printf("%f F = %f C\n", F, C);
}


Exercise 2

Write a C program that reads an integer, computes the square root of the integer, and prints the square root in the following formats:
     a) an integer in decimal,
     b) an integer in hexadecimal,
     c) a floating point number,
     d) a floating point number in the scientific notation.


Solution 2

#include <stdio.h>
#include <math.h>

int main ()
{
   int n;
   double x;

   printf("Enter a positive integer : ");
   scanf("%d", &n);
   x = sqrt(n);
   printf("sqrt(%d) = %d\n", n, (int)x);
   printf("sqrt(%d) = %x\n", n, (int)x);
   printf("sqrt(%d) = %lf\n", n, x);
   printf("sqrt(%d) = %le\n", n, x);
}

Note: If you make math library calls, compile your program with the -lm option.


Exercise 3

Write a C program that produces the following formatted output:

%  one                        %
%%  two three                %%
%%%  four five six          %%%
%%%%  seven eight nine ten %%%%


Solution 3

#include <stdio.h>

int main ()
{
   printf("%%  one                        %%\n");
   printf("%%%%  two three                %%%%\n");
   printf("%%%%%%  four five six          %%%%%%\n");
   printf("%%%%%%%%  seven eight nine ten %%%%%%%%\n");
}


Exercise 4

Write a C program that reads the x and y coordinates of two points in the plane and computes the distance between them.


Solution 4

#include <stdio.h>
#include <math.h>

int main ()
{
   double x1, y1, x2, y2, d;

   printf("Enter the coordinates of the first point: ");
   scanf("%lf%lf",&x1,&y1);
   printf("Enter the coordinates of the second point: ");
   scanf("%lf%lf",&x2,&y2);
   d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   printf("Distance between (%lf,%lf) and (%lf,%lf) is %lf\n", x1,y1,x2,y2,d);
}


Exercise 5

Write a C program that reads the name of your birth place and prints the number of letters in it.


Solution 5

#include <stdio.h>
#include <string.h>

int main ()
{
   char name[100];

   printf("Enter your birth-place: ");
   scanf("%s", name);
   printf("Number of letters = %d\n", strlen(name));
}


Brain-teaser

I ran the following C program on my machine:

#include <stdio.h>

int main()
{
   float x = 9.81;

   if (x == 9.81)
      printf("Yep, x is equal to 9.81.\n");
   else
      printf("Wow, x is not equal to 9.81.\n");
}

I obtained the following output:

Wow, x is not equal to 9.81.

Try it on your machine. Compile your program by an ANSI-compliant compiler. You are expected to get the same result as I got.

How come? How can you rewrite the program so as to obtain the desired output?



netaji gandi Friday, March 25, 2022
Programming in Modern C++ Week 8 Programming Assignments [ Jan-2022 | NPTEL ]

 

Week 8: Programming Assignment 1

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
•at LINE-1 with appropriate template definition,
•at LINE-2 and LINE-3 with appropriate template and function header for add() function,
•at LINE-4 and LINE-5 with appropriate template and function header to overload function call operator,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:49 IST
Select the Language for this assignment. 
1
#include<iostream>
using namespace std;

template<class T>    //LINE-1
class ArrayList{
    private:
        T* list;
        int size;
        int idx;
    public:
        ArrayList(int size_){
            size = size_;
            list = new T[size];
            idx = -1;
        }
        void add(T item);
        void operator()();
};
template<class T>//LINE-2
void ArrayList<T>::add( T item ){    //LINE-3
    if(idx < size)
        list[++idx] = item;
}
template<class T>      //LINE-4 
void ArrayList<T>::operator()() {    //LINE-5
    for(int i = size -1; i >= 0; i--){
        cout << list[i] << " ";
    }
}
0
int main(){
1
    int n, m;
2
    char c;
3
    cin >> n;
4
    ArrayList<int> iList(n);
5
    for(int i = 0; i < n; i++){
6
        cin >> m;
7
        iList.add(m);
8
    }
9
    ArrayList<char> cList(n);
10
    for(int i = 0; i < n; i++){
11
        cin >> c;
12
        cList.add(c);
13
    }
14
    iList();
15
    cList();    
16
    return 0;
17
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   



InputOutput
Test Case 1
4
10 20 30 40
a b c d
40 30 20 10 d c b a 
Test Case 2
5
1 2 3 4 5
e f g h i
5 4 3 2 1 i h g f e 





Week 8: Programming Assignment 2

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below to
complete the definition of functor Maximum with local state:
•at LINE-1 with appropriate unary_function statement,
•at LINE-2 with constructor header with initialization list,
•at LINE-3 with overloading the function call operator,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:51 IST
Select the Language for this assignment. 
1
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;

struct Maximum : public unary_function<int, void>{    //LINE-1
    int count;
    int sum;    
    Maximum(): sum(0), count(0) {}    //LINE-2
  void operator() (int x) { sum+=x;count++; }//LINE-3
};
0
int main(){
1
    vector<int> v;
2
    int n, a;
3
    cin >> n;
4
    for(int i = 0; i < n; i++){
5
        cin >> a;
6
        v.push_back(a);
7
    }
8
    Maximum m = for_each(v.begin(), v.end(), Maximum());
9
    cout << "avg = " << (double)m.sum / m.count;
10
    return 0;   
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   



InputOutput
Test Case 1
4 10 20 30 40
avg = 25
Test Case 2
5 3 6 7 10 12
avg = 7.6




Week 8: Programming Assignment 3

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
•at LINE-1 with definition of pointer to function type F_ptr, which can points functions
 message_to_client1() and message_to_client2(),
•at LINE-2 with appropriate header of the function wrapper(),
•at LINE-3 with function call using the pointer to function,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:52 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Message{
5
    private:
6
        string greet;
7
    public:
8
        Message(string greet_) : greet(greet_){}
9
        void message_to_client1(string m){
10
            cout << "client-1: " << greet << " from " << m << endl;
11
        }
12
        void message_to_client2(string m){
13
            cout << "client-2: " << greet << " from " << m;
14
        }
15
};
16
void (Message::*F_ptr)(string);    //LINE-1

void wrapper(Message *op,void (Message::*F_ptr)(string), string n){ //LINE-2
    (op->*F_ptr)(n);         //LINE-3
}
0
int main(){
1
    string s, n1, n2;
2
    cin >> s >> n1 >> n2;
3
    Message *op = new Message(s);
4
    wrapper(op, &Message::message_to_client1, n1);
5
    wrapper(op, &Message::message_to_client2, n2);
6
    return 0;
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   



InputOutput
Test Case 1
hello bhaskar nalini
client-1: hello from bhaskar
client-2: hello from nalini
Test Case 2
welcome soumen anand
client-1: welcome from soumen
client-2: welcome from anand


netaji gandi Thursday, March 24, 2022

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...